略微加速

Elasticsearch权威指南 - 互联网笔记

更复杂的搜索 | Elasticsearch: 权威指南 | Elastic

2024-03-29

更复杂的搜索编辑

现在尝试下更复杂的搜索。 同样搜索姓氏为 Smith 的雇员,但这次我们只需要年龄大于 30 的。查询需要稍作调整,使用过滤器 filter ,它支持高效地执行一个结构化查询。

GET /megacorp/employee/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}

这部分与我们之前使用的 match 查询 一样。

这部分是一个 range 过滤器 它能找到年龄大于 30 的文档,其中 gt 表示_大于(_great than)。

目前无需太多担心语法问题,后续会更详细地介绍。只需明确我们添加了一个 过滤器 用于执行一个范围查询,并复用之前的 match 查询。现在结果只返回了一个雇员,叫 Jane Smith,32 岁。

{
   ...
   "hits": {
      "total":      1,
      "max_score":  0.30685282,
      "hits": [
         {
            ...
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}

官方地址:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_more_complicated_searches.html

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3