zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

Elasticsearch 基本查询语法

elasticsearch 查询 基本 语法
2023-09-11 14:20:01 时间

基本操作

操作索引
1.新建索引
curl -XPUT localhost:9200/index01

2.查看索引
curl -XGET http://192.168.168.101:9200/index01/_settings

curl -XGET http://192.168.168.101:9200/index01,blog/_settings

3.删除索引
curl -XDELETE http://192.168.168.101:9200/index02

4.打开关闭索引
curl -XPOST http://192.168.168.101:9200/index01/_close

curl -XPOST http://192.168.168.101:9200/index01/_open

文档管理
1.新建文档
curl -XPUT -d ‘{‘id’:1,‘title’:‘es简介’}’ http://localhost:9200/index01/article/1

2.获取文档
curl -XGET http://192.168.168.101:9200/index01/article/1

3.删除文档
curl -XDELETE http://192.168.168.101:9200/index01/article/1

查询操作

基本查询
指定请求头
–header “content-Type:application/json”

准备数据
curl -XPUT -d '{"id":1,"title":"es简介","content":"es好用好用真好用"}' http://192.168.168.101:9200/index01/article/1
curl -XPUT -d '{"id":1,"title":"java编程思想","content":"这就是个工具书"}' http://192.168.168.101:9200/index01/article/2
curl -XPUT -d '{"id":1,"title":"大数据简介","content":"你知道什么是大数据吗,就是大数据"}' http://192.168.168.101:9200/index01/article/3
 
term query
curl -XGET http://192.168.168.101:9200/index01/_search -d {'query':{'term':{'title':'你好'}}}
 
查询的字段只有一个值得时候,应该使用term而不是terms,在查询字段包含多个的时候才使用terms,使用terms语法,json中必须包含数组

match在匹配时会对所查找的关键词进行分词,然后按分词匹配查找,而term会直接对关键词进行查找。一般**模糊查找的时候,多用match,而精确查找时可以使用term

terms query
{
    'query':{
        'terms':{
            'tag':["search",'nosql','hello']
        }
    }
}


match query
 

{'query':{'match':{'title':'你好'}}}

{
   "query": {
     "match": {
       "__type": "info"
     }
   },
   "sort": [
     {
       "campaign_end_time": {
         "order": "desc"
       }
     }
   ]
}
 


match_all

{'query':{'match_all':{'title':'标题一样'}}}


 
multi match
多值匹配查询

{
  "query": {
    "multi_match": {
      "query": "运动 上衣",
      "fields": [
        "brandName^100",
        "brandName.brandName_pinyin^100",
        "brandName.brandName_keyword^100",
        "sortName^80",
        "sortName.sortName_pinyin^80",
        "productName^60",
        "productKeyword^20"
      ],
      "type": <multi-match-type>,
      "operator": "AND"
    }
  }
}

 
Bool query(常用)
bool查询包含四个子句,must,filter,should,must_not

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "title": {
              "value": "20"
            }
          }
        },
        {
          "term": {
            "content": {
              "value": "81909843976216819028"
            }
          }
        }
      ]
    }
  }
}

{
    "bool":{
            "must":{
                "term":{"user":"lucy"}
            },
            "filter":{
                "term":{"tag":"teach"}    
            },
            "should":[
                  {"term":{"tag":"wow"}},
                {"term":{"tag":"elasticsearch"}}
            ],
               "mininum_should_match":1,
               "boost":1.0                      
        }
}
 


Filter query
query和filter的区别:query查询的时候,会先比较查询条件,然后计算分值,最后返回文档结果;而filter是先判断是否满足查询条件,如果不满足会缓存查询结果(记录该文档不满足结果),满足的话,就直接缓存结果

filter快在:对结果进行缓存,避免计算分值

{
    "query": {
      "bool": {
        "must": [
          {"match_all": {}}
        ],
        "filter": {
          "range": {
            "create_admin_id": {
              "gte": 10,
              "lte": 20
            }
          }
        }
      }
    }
}


range query
 

{
    'query':{
        'range':{
            'age':{
                'gte':'30',
                'lte':'20'
            }
        }
    }
}
 


通配符查询

{
    'query':{
        'wildcard':{
            'title':'cr?me'
        }
    }
    
}

 
正则表达式查询
 

{
    'query':{
        'regex':{
            'title':{
                'value':'cr.m[ae]',
                'boost':10.0
            }
        }
    }
}


 
前缀查询
 

{
    'query':{
        'match_phrase_prefix':{
            'title':{
                'query':'crime punish',
                'slop':1
            }
        }
    }
}


 
query_string
{
    'query':{
        'query_string':{
            'query':'title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)'
        }
    }
}

小技巧:对es语法不熟悉的可以用head插件自动生成