Deprecated: Optional parameter $keys declared before required parameter $cms_id is implicitly treated as a required parameter in /home/www/dev/work/class/blog/CmsKey.php on line 75

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\es::$title is deprecated in /home/www/dev/work/website/lvesu/class/controller/blog/es.php on line 28

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\es::$outlink is deprecated in /home/www/dev/work/website/lvesu/template/blog/cms/es.manual.tpl on line 2

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\es::$status is deprecated in /home/www/dev/work/website/lvesu/template/blog/index.head.php on line 2
更复杂的搜索 | Elasticsearch: 权威指南 | Elastic - 互联网笔记

略微加速

Elasticsearch权威指南 - 互联网笔记

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

2025-04-19

更复杂的搜索编辑

现在尝试下更复杂的搜索。 同样搜索姓氏为 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

冷却塔厂家 广告
中文GPT4.0无需注册 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3