ES学习二-查询语句

221 阅读2分钟

搜索文档

返回特定字段

GET /test/_search
{
    "query":{"match_all":{}},
    "_source":["name"]
}

_source字段表示数据“源”

匹配查询

GET /test/_search
{
    "query":{
        "match":{
            "name":"bulk"
        }
    }
}

Elasticsearch 执行上面这个 match 查询的步骤是:

  1. 检查字段类型 。

标题name字段是一个 string 类型( analyzed )已分析的全文字段,这意味着查询字符串本身也应该被分析。

  1. 分析查询字符串 。

将查询的字符串bulk传入标准分析器中,输出的结果是单个项bulk。因为只有一个单词项,所以 match 查询执行的是单个底层 term 查询。

  1. 查找匹配文档 。

term查询在倒排索引中查找 bulk 然后获取一组包含该项的文档。

  1. 为每个文档评分 。

term 查询计算每个文档相关度评分 _score ,这是种将词频(term frequency,即词 quick 在相关文档的 name 字段中出现的频率)和反向文档频率(inverse document frequency,即词 bulk 在所有文档的 name 字段中出现的频率),以及字段的长度(即字段越短相关度越高)相结合的计算方式。参见 相关性的介绍 。

match 匹配会走分词器。默认分词器standard。中文常用分词器ik

GET /test/_search
{
 "query": {
   "match": {
     "name": {
       "query": "bulk",
       "analyzer": "standard"
     }
   }
 }
}

布尔查询

GET /test/_search
{
    "query":{
        "bool":{
            "must":[
                {"match":{"name":"bulk"}},
                {"match":{"age":10}}
            ]
        }
    }
}

上面的示例中,must表示必须满足为真的所有条件。如果有多个,必须同时满足。

GET /test/_search
{
    "query":{
        "bool":{
            "should":[
                {"match":{"name":"bulk"}},
                {"match":{"age":10}}
            ]
        }
    }
}

上面的示例中,should表示满足任意一个条件即可。

GET /test/_search
{
    "query":{
        "bool":{
            "must_not":[
                {"match":{"name":"bulk"}},
                {"match":{"age":20}}
            ]
        }
    }
}

上面的示例中,must_not表示任意的条件都不能为真,才视为匹配成功。

条件过滤

_score成为文档得分,是返回结果中表示结果与搜索匹配程度的相似性度量。

GET /test/_search
{
    "query":{
        "bool":{
            "must":{
                "match_all":{}
            },
            "filter":{
                "range":{
                    "age":{
                        "gte":20,
                        "lt":40
                    }
                }
            }
        }
    }
}

聚合查询

GET /test/_search
{
    "size":0,
    "aggs":{
        "group_by_name":{
            "terms":{
                "field":"name.keyword"
            }
        }
    }
}

size=0表示不显示搜索结果(具体每个数据),只显示聚合结果

GET /test/_search
{
    "size":0,
    "aggs":{
        "group_by_name":{
            "terms":{
                "field":"name.keyword",
                "order":{"sum_age":"desc"}
            },
            "aggs":{
                "avg_age":{
                    "avg":{"field":"age"}
                },
                "sum_age":{
                    "sum":{"field":"age"}
                }
            },
            "range":{
                "age":{
                        "gte":20,
                        "lt":40
                }
            }
        }
    }
}
GET /test/_search
{
  "size": 0,
  "aggs": {
    "group_by_name": {
      "aggs": {
        "avg_age": {
          "avg": {
            "field": "age"
          }
        },
        "sum_age": {
          "sum": {
            "field": "age"
          }
        }
      },
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 10,
            "to": 20
          },
          {
            "from": 20,
            "to": 30
          }
        ]
      }
    }
  }
}
  • order排序
  • aggs聚合计算
  • range 根据范围分组