ElasticSearch常用查询

删除索引
DELETE /indexname

删除多个索引
DELETE /index_one,index_two
DELETE /index_*

删除全部索引
DELETE /_all

查看分词
GET _analyze
{
  "tokenizer" : "ik_smart",
  "text" : "北京冬奥会开幕式"
}

查看分词,加过滤器
GET _analyze
{
  "tokenizer" : "ik_smart",
  "filter": [{"type": "length", "min":1, "max":3 }],  
  "text" : "北京冬奥会开幕式"
}

查看分词
POST _analyze
{
  "char_filter": [], 
  "tokenizer": "ik_smart",
  "filter": [
    "stop"
  ],
  "text": "北京冬奥会开幕式"
}


查看某个索引的字段类型
GET indexname/_mapping

查看所有索引信息
GET _cat/indices?v

修改字段映射,将title字段修改为可以创建分词,detail字段修改为不可以创建分词
PUT indexname
{
  "mappings":{
    "properties":{
      "title":{
        "type":"text"
      },
      "detail":{
        "type":"keyword"
      }
    }
  }
}

添加数据
put /indexname/_doc/1
{
   "title":'全文索引',
   "description":"描述",
    "createtime":"2022-03-02"
}

添加数据(相同ID重复添加就是替换)
post /indexname/_doc/1
{
   "title":'全文索引',
   "description":"描述",
    "createtime":"2022-03-02"
}

局部更新,替换
post /indexname/_doc/1/_update
{
  "doc":{
      "title":"修改全文索引"
    }
}

局部更新,在字段原值的基础上增加计数器,此方式打开on_conflict可以避免多线程同时修改
post /indexname/_doc/1/_update
{
  "script":{
      "source":"ctx._source.counter +=  params.count",
       params:{
          count =1
      }
    }
}

删除单条数据
delete /indexname/_doc/3011

获取全部数据
GET /indexname/_search
{
  "query": {"match_all": {}}
}

获取全部数据,返回前20条数据
GET /indexname/_search
{
  "query": {"match_all": {}},
  "size": 20,
}

获取全部数据,返回第20到第30条数据
GET /indexname/_search
{
  "query": {"match_all": {}}
  "from": 20,
  "size": 10,
}

根据price字段降序
GET /indexname/_search
{
  "query": {"match_all": {}}
  "from": 20,
  "size": 10,
  "sort": {"price": {"order": "desc"}}
}

只返回 price和title两个字段,并按价格排序,分页获取文档
GET /indexname/_search
{
  "query": {"match_all": {}}
  "from": 20,
  "size": 10,
  "sort": {"price": {"order": "desc"}},
  "_source": ["price", "title"]
}

按title搜索,只返回 price和title两个字段,并按价格排序,分页获取文档
GET /indexname/_search
{
  "query": {
     "match": {
        "title":"旗靓手机"
      }
    }
  "from": 20,
  "size": 10,
  "sort": {"price": {"order": "desc"}},
  "_source": ["price", "title"]
}


返回price=100的数据
GET /indexname/_search
{
  "query": {"match": {"price": 100}}
  "from": 20,
  "size": 10,
  "sort": {"price": {"order": "desc"}}
}

返回info包含北京的数据
GET /indexname/_search
{
  "query": {"match": {"title": "北京"}}
  "from": 20,
  "size": 10,
  "sort": {"price": {"order": "desc"}}
}

返回info包含北京 or 奥运会的数据
GET /indexname/_search
{
  "query": {"match": {"title": "北京 奥运会"}}
  "from": 20,
  "size": 10,
  "sort": {"price": {"order": "desc"}}
}



查询匹配短语="北京 冬奥会"
GET /indexname/_search
{
  "query": {"match": {"title": "北京 奥运会"}}
  "from": 20,
  "size": 10,
  "match_phrase": {"title": "北京 冬奥会"}
}

布值值查,指定ik分词查询title包含北京的数据
GET /indexname/_search
{
  "query": {
    "bool": {
       "must": [
            {"match": {"title" : {"query":"北京", "analyzer": "ik_max_word"}} } 
        ]
    }
  }
}

布值值查询同时满足多条件
GET /indexname/_search
{
  "query": {
    "bool": {
       "must": [
            {"match": {"title" : "北京"} } ,
            {"match": {"age" : 20} } 
        ]
    }
  }
}

布值值查询满足其中任意一个条件
GET /indexname/_search
{
  "query": {
    "bool": {
       "should": [
            {"match": {"title" : "北京"} } ,
            {"match": {"age" : "20"} } 
        ]
    }
  }
}

不区配info=北京 & info=冬奥会
GET /indexname/_search
{
  "query": {
    "bool": {
       "must_not": [
            {"match": {"info" : "北京"} } ,
            {"match": {"info" : "奥运会"} } 
        ]
    }
  }
}


不区配title=北京 & id!=50
GET /indexname/_search
{
  "query": {
    "bool": {
       "must_not": [
            {"match": {"title" : "北京"} } 
        ],
       "must_not": [
            {"match": {"id" : 50} } 
        ]
    }
  }
}

范围查询
POST /indexname/_search
{
  "query": {
    "bool": {
        "must":     { "match": { "age": 20 }},
        "filter": {
          "range": {
            "balance": {
                "gte": 20000,
                "lte": 30000
              }
            } 
        }
    }
  }
}


范围查询
POST /indexname/_search
{
  "query": {
    "bool": {
        "must":     { "match": { "age": 39 }},
        "must_not": { "match": { "employer":"Digitalus" }},
        "filter": {
          "range":  { 
            "balance":  {
                "gte": 20000,
                "lte": 30000
              }
            } 
        }
    }
  }
}

精确查询,必须完全匹配
GET /indexname/_search
{
    "query":{
      "term":{
        "title":"我爱你枣500克装"
      }
    }
}

多条件精确查询,任意条件精确匹配
GET /indexname/_search
{
    "query":{
      "bool":{
        "should":[
          {
            "term":{
               "title":"我爱你枣500克装"
            }
          },{
            "term":{
               "price":"26.5"
            }
          }
        ]
      }
    }
}

多条件精确查询,多条件必须完全匹配
GET /indexname/_search
{
    "query":{
      "bool":{
        "must":[
          {
            "term":{
               "title":"我爱你枣500克装"
            }
          },{
            "term":{
               "price":"26.5"
            }
          }
        ]
      }
    }
}

 

posted @ 2022-02-25 15:46  民工黑猫  阅读(62)  评论(0编辑  收藏  举报