ELK之ES查询

1、Elasticsearch查询语法

Elasticsearch查询分为两类:

分成两类:
      query dsl: 执行full-text查询时,基于相关度来评判其匹配结果;
          查询执行过程复杂,且不会被缓存;

      filter dsl: 执行exact查询时,基于其结果为“yes“或“no”进行评判;
          速度快,且结果缓存;


其中:

filter dsl:

term filter:精确匹配包含指定tera的文档:{“term”:{“name”:“Guo"}}
curl -XGET 'localhost:9200/students/_search?pretty' -d {
  "query":{
      "term":{
           "name""Guo"
       }
   }
}


terms filter:用于多值精确匹配:{ "terms":{ "name";["Guo""Rong"] }}


range filters:用于在指定的范围内查找数值或时间。
{ "range""age":{
        "gte"15,
        "lte"25
    }
}


exists and missing filters:
{
    "exists": {
    "age": 25
    }
}


boolean filter:基于boolean的逻辑来合并多个filter子句:
    must:其内部所有的子句条件必须同时匹配,即and;
        must: {
            "term":{ "age"25 }
            "term":{ "gender""Female" }
        }

    must_not:其所有子句必须不匹配,即not 
        must_not: {
            "term":{ "age"25 }
        }

    should:至少有一个子句匹配,即or 
        should: {
           "term": { "age": 25 }
           "term":{ "gender": "Female" }
        }


QUERY DSL:

match_all Query:
    用于匹配所有文档,没有指定任何query,默认即为match_all query.
    { "match_al1":{} }


match Query:
    在几乎任何域上执行full-text或exact-value查询:
          如果执行full-text查询:首先对查询时的语句做分析:
                { "match":{ "students""Guo" }}
          如果执行exact-value查询:搜索精确值;此时,建议使用过滤,而非查询:
                { "match":{ "name""Guo" }}


multi_match Query:用于在多个域上执行相同的查询:
    {
        "multi_match""query":full-text search
            "field":{ "field1"'fieldz' }
    }

    {
        "multi_match":
            "query": {
                 "students": "Guo" 
            }
           "field":
               {
                    "name",
                    "description"
               }
    }


bool query:
基于boolean逻辑合并多个查询语句;与bool filter不同的是,查询子句不是返回"yes""no",而是其计算出的匹配度分值。
因此,boolean Query会为各子句合并其score;

关键词:
    must: 
    must_not: 
    should:


合并filter和query查询语句(组合查询):

一般来讲可以filter语句嵌套到query语句用来过滤,但很少把query语句嵌套到filter语句上。

{
    "filterd": {
         query: { "match": {"gender": "Female"} }
         filter: { "term": {"age": 25}}
     }
}


扫描二维码关注公众号,回复: 11027814 查看本文章

查询语句语法检查:

#----
GET  /INDEX/_validate/query?pretty
 {
     #查询语句
}

例子:
[root@node1 ~]# curl -XGET 'localhost:9200/students/_validate/query?pretty' -d '{"query": {"term": {"name": "Guo"}}}'
{
  "valid" : true,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  }
}


#----
如果想获取详细信息,可以使用以下格式:
GET  /INDEX/_validate/query?explain&pretty
{
    #查询语句
}

例子:
[root@node1 ~]# curl -XGET 'localhost:9200/students/_validate/query?explain&pretty' -d '{"query": {"term": {"name": "Guo"}}}'
{
  "valid" : true,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "explanations" : [ {
    "index" : "students",
    "valid" : true,
    "explanation" : "name:Guo"
  } ]
}

猜你喜欢

转载自www.cnblogs.com/weiyiming007/p/12749146.html