Elasticsearch 中的 match 和 bool 必须匹配查询有什么区别

Joh*_*son 7 elasticsearch booleanquery elasticsearch-query

ES中Only match和bool必须匹配查询有什么区别?

首先,仅使用匹配查询

{
   "query":{
      "match":{
         "address":"mill"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

二、使用复合查询

{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } }
      ]
     }
   }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

你能告诉我一切吗?他们之间有什么区别?

Ami*_*wal 10

match当您在子句中仅使用一个bool must时,没有区别,当您想要组合多个(布尔)条件时,bool 子句很有用,更多信息请参阅官方 ES 文档。它支持以下标准。

  1. 必须
  2. 一定不
  3. 筛选
  4. 应该

让我从你的问题中举一个小例子来说明。

仅包含地址和名字的索引映射

{
    "mappings": {
        "properties": {
            "address": {
                "type": "text"
            },
            "first_name" :{
                "type" : "text"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

索引 3 文档,全部具有相同的地址mill,但不同first_name

{
   "address" : "mill",
   "first_name" : "Johnson"
}

{
   "address" : "mill",
   "first_name" : "Parker"
}

{
   "address" : "mill",
   "first_name" : "opster"
}
Run Code Online (Sandbox Code Playgroud)

搜索查询以显示以下地址的所有地址,mill但不得包含名字:parker

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "address": "mill"
                    }
                },
                {
                    "must_not": {
                        "first_name": "parker"
                    }
                }
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果只有2个地址

"hits": [
         {
            "_index": "so-60620921-bool",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.13353139,
            "_source": {
               "address": "mill",
               "first_name": "opster"
            }
         },
         {
            "_index": "so-60620921-bool",
            "_type": "_doc",
            "_id": "3",
            "_score": 0.13353139,
            "_source": {
               "address": "mill",
               "first_name": "Johnson"
            }
         }
      ]
Run Code Online (Sandbox Code Playgroud)

基于OP注释,提供查询和过滤上下文,以详细了解性能方面。