【问题标题】:Elasticsearch compound queryElasticsearch 复合查询
【发布时间】:2020-08-24 21:32:55
【问题描述】:

我正在使用如下复合查询查询包含 300 条记录的弹性索引:

GET my_index/_search
{
  "size": 10,
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "should": [
              {
                "multi_match": {
                  "query": "card",
                  "fields": [
                    "title^1.0"
                  ]
                }
              }
            ],
            "must": {
              "term": {
                  "_index": {
                    "value": "my_index"
                  }
                }
            }
          }
        }
      ]
    }
  }
}

must on index 是因为这可能是一个多索引查询,具体取决于某些业务逻辑(must 很可能应该是一个过滤器,我可以更改它,但这不是我的问题的一部分。我得到相同的结果也有过滤器)。

虽然我希望这会返回与 should 子句匹配的文档,但我会返回索引中的所有文档 (300)

为什么会这样?

【问题讨论】:

  • 你有没有机会浏览我的答案,期待得到你的反馈:)
  • 嗨 Bhavya,问题是我们应该使用 must 查询或 filter 查询。在您的示例中,没有 should 子句。我的问题的解决方案是在请求中使用 minimumShouldMatch 字段。

标签: elasticsearch elasticsearch-dsl


【解决方案1】:

解决方法是在查询中添加 minimumShouldMatch 字段。结果查询然后变为:

GET my_index/_search
{
  "size": 10,
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "multi_match": {
                  "query": "card",
                  "fields": [
                    "title^1.0"
                  ]
                }
              }
            ],
            "must": {
              "term": {
                  "_index": {
                    "value": "my_index"
                  }
                }
            }
          }
        }
      ]
    }
  }
}

我相信这背后的原因是 bool 查询经过调整以提供最大数量的匹配结果(更多匹配更好)。因此,如果 must/filter 子句匹配,则应该甚至不执行。通过添加 "minimum_should_match": 1 我们指示 elasticsearch 在返回文档之前匹配至少 1 个 should 子句。

弹性文档节选:

bool 查询采用更多匹配更好的方法,因此每个匹配的 must 或 should 子句的分数将被加在一起,以提供每个文档的最终 _score。

您可以使用 minimum_should_match 指定 should 子句的数量或百分比的参数 返回的文件必须匹配。

如果 bool 查询包含至少一个 should 子句并且没有 must or 过滤子句,默认值为1。否则,默认值 是 0。

有关其他有效值,请参阅 minimum_should_match 参数。

参考链接 - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#bool-min-should-match

【讨论】:

    【解决方案2】:

    使用索引数据和搜索查询添加一个工作示例

    索引数据:

    {
        "title":"card",
        "cost":"55"
    }
    {
        "title":"Card making",
        "cost":"55"
    }
    {
        "title":"elasticsearch",
        "cost":"55"
    }
    

    搜索查询:

    GET /_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "filter": [
                  {
                    "term": {
                      "_index": {
                        "value": "index-name"
                      }
                    }
                  }
                ],
                "must": [
                  {
                    "multi_match": {
                      "fields": [
                        "title^1.0"
                      ],
                      
                      "query": "card"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    搜索结果:

    "hits": [
          {
            "_index": "my_index",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.7549127,
            "_source": {
              "title": "card",
              "cost": "55"
            }
          },
          {
            "_index": "my_index",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.55654144,
            "_source": {
              "title": "Card making",
              "cost": "55"
            }
          }
        ]
    

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2020-12-21
      • 1970-01-01
      • 2014-10-22
      • 2014-11-06
      • 2018-12-11
      相关资源
      最近更新 更多