【问题标题】:Is there different between multiple `match` with single term in `should` vs a single `match` with multiple term in `should`?在“应该”中使用单个术语的多个“匹配”与在“应该”中使用多个术语的单个“匹配”之间是否有区别?
【发布时间】:2021-02-23 17:34:45
【问题描述】:

下面2个有区别吗?

GET books/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "description": {
              "query": "cat dog"
            }
          }
        }       
      ]
    }
  }
}

和

GET books/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "description": {
              "query": "cat"
            }
          }
        },
        {
          "match": {
            "description": {
              "query": "dog"
            }
          }
        }       
      ]
    }
  }
}

从结果来看,我认为它们是相同的。我使用的索引文档如下

POST books/_bulk
{"index":{}}
{ "title": "Dogs", "description": "A fox jump over the lazy dog" }
{"index":{}}
{ "title": "Cats", "description": "Garfield the sleepy cat" }
{"index":{}}
{ "title": "Sleepy", "description": "Rest is important" }
{"index":{}}
{ "title": "Rich", "description": "Money is never enough" }
{"index":{}}
{ "title": "Pets", "description": "Dog and cat are friends" }
{"index":{}}
{ "title": "Character", "description": "Are you really a dog person?" }

映射是默认映射

{
  "books" : {
    "mappings" : {
      "properties" : {
        "description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

  • 你能分享你的索引映射吗?
  • 我没有显式设置任何映射,它是默认的动态设置映射。
  • 当然,这样您的文本字段将使用标准分析器,我将重现您的问题,并添加示例,同时请仔细阅读我的答案,这也会使事情变得清晰

标签: elasticsearch


【解决方案1】:

这取决于您的description 字段使用的是什么分析器,如果您使用的是标准或空白分析器,它会破坏whitespace 上的文本,它将是相同的。

您可以使用Explain API 检查为您的搜索词生成的搜索时间标记。

您还应该注意match query 的默认运算符是OR,并在同一官方文档中进行了说明

operator (可选,字符串)用于解释文本的布尔逻辑 查询值。有效值为:

OR(默认)例如查询匈牙利首都的值为 解释为 OR 匈牙利的大写 OR。

AND 例如,查询 匈牙利的资本价值被解释为 AND 的资本 AND 匈牙利。

当文本字段使用默认分析器即标准时,解释 API 返回下面的搜索查询

POST /{your-index}/_search?explain=true

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "description": {
                            "query": "cat dog"
                        }
                    }
                }
            ]
        }
    }
}

并使用解释参数搜索查询

 "_explanation": {
                    "value": 0.6931471,
                    "description": "sum of:",
                    "details": [
                        {
                            "value": 0.6931471,
                            "description": "weight(description:cat in 0) [PerFieldSimilarity], result of:",
                            "details": [
                                {
                                    "value": 0.6931471,
                                    "description": "score(freq=1.0), computed as boost * idf * tf from:",
                                    "details": [
                                        {
                                            "value": 2.2,
                                            "description": "boost",
                                            "details": []
                                        },
                                        {
                                            "value": 0.6931472,
                                            "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                                            "details": [
                                                {
                                                    "value": 1,
                                                    "description": "n, number of documents containing term",
                                                    "details": []
                                                },
                                                {
                                                    "value": 2,
                                                    "description": "N, total number of documents with field",
                                                    "details": []
                                                }
                                            ]
                                        },
                                        {
                                            "value": 0.45454544,
                                            "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                                            "details": [
                                                {
                                                    "value": 1.0,
                                                    "description": "freq, occurrences of term within document",
                                                    "details": []
                                                },
                                                {
                                                    "value": 1.2,
                                                    "description": "k1, term saturation parameter",
                                                    "details": []
                                                },
                                                {
                                                    "value": 0.75,
                                                    "description": "b, length normalization parameter",
                                                    "details": []
                                                },
                                                {
                                                    "value": 4.0,
                                                    "description": "dl, length of field",
                                                    "details": []
                                                },
                                                {
                                                    "value": 4.0,
                                                    "description": "avgdl, average length of field",
                                                    "details": []
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            },
            {
                "_shard": "[66329708][0]",
                "_node": "tMk7cyaBRSeJ8dxuSQdHRg",
                "_index": "66329708",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.6931471,
                "_source": {
                    "description": "Garfield the sleepy dog"
                },
                "_explanation": {
                    "value": 0.6931471,
                    "description": "sum of:",
                    "details": [
                        {
                            "value": 0.6931471,
                            "description": "weight(description:dog in 0) [PerFieldSimilarity], result of:",
                            "details": [
                                {
                                    "value": 0.6931471,
                                    "description": "score(freq=1.0), computed as boost * idf * tf from:",
                                    "details": [
                                        {
                                            "value": 2.2,
                                            "description": "boost",
                                            "details": []
                                        },
                                        {
                                            "value": 0.6931472,
                                            "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                                            "details": [
                                                {
                                                    "value": 1,
                                                    "description": "n, number of documents containing term",
                                                    "details": []
                                                },
                                                {
                                                    "value": 2,
                                                    "description": "N, total number of documents with field",
                                                    "details": []
                                                }
                                            ]
                                        },
                                        {
                                            "value": 0.45454544,
                                            "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                                            "details": [
                                                {
                                                    "value": 1.0,
                                                    "description": "freq, occurrences of term within document",
                                                    "details": []
                                                },
                                                {
                                                    "value": 1.2,
                                                    "description": "k1, term saturation parameter",
                                                    "details": []
                                                },
                                                {
                                                    "value": 0.75,
                                                    "description": "b, length normalization parameter",
                                                    "details": []
                                                },
                                                {
                                                    "value": 4.0,
                                                    "description": "dl, length of field",
                                                    "details": []
                                                },
                                                {
                                                    "value": 4.0,
                                                    "description": "avgdl, average length of field",
                                                    "details": []
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }

您可以注意到用于搜索的搜索词是cat 和dog。

如果您使用不同的分析器,如 keyword analyzer 和索引映射,如下所示

{
    "mappings": {
        "properties": {
            "description": {
                "type": "text",
                "analyzer": "keyword" // Note this
            }
        }
    }
}

索引样本数据

{
    "description" : "cat dog"
}

{
    "description" : "Garfield the sleepy dog"
}

并进行以下搜索调用

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "description": {
                            "query": "cat dog"
                        }
                    }
                }
            ]
        }
    }
}

您会在解释 API 中注意到以下标记用于搜索

"weight(description:cat dog in 0) [PerFieldSimilarity], result of:", 如 您现在可以看到 cat 和 dog 未拆分,因为匹配查询使用在索引时应用的​​相同分析器,在示例中使用 keyword 分析器并且不会破坏标记空格。

【讨论】:

  • 感谢 Opster。我理解“应该”中的多个“匹配”也是“或”。您能否提供一个使用不同分析器的示例,说明它们有何不同?
  • @Elye,使用示例进行了解释,请看一下它变得非常大,因此省略了最后一次搜索呼叫响应,但您应该可以自己尝试一下,希望这会有所帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多