zoukankan      html  css  js  c++  java
  • 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}}
         }
    }


    查询语句语法检查:

    #----
    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"
      } ]
    }
  • 相关阅读:
    C语言中结构体变量之间赋值
    ZOJ
    【微服务干货系列】使用微服务架构之前,你必须知道的
    使用heartbeat+monit实现主备双热备份系统
    rsync 3.1.1源代码编译安装配置
    oracle 11g GRID 中 关于 OLR 须要知道的一些内容
    字母游戏
    移动开发人员应避免的 4 大陷阱
    看看这个经常被0基础程序猿弄不懂的 “事件”
    【剑指offer】和为定值的连续正数序列
  • 原文地址:https://www.cnblogs.com/weiyiming007/p/12749146.html
Copyright © 2011-2022 走看看