【问题标题】:Elastic Search query with should returning 10.000 results but nothing matches应返回 10.000 个结果但没有匹配项的弹性搜索查询
【发布时间】:2021-08-15 13:58:18
【问题描述】:

所以我有一个大约 60GB 数据的索引,基本上我想根据参考号进行查询以检索 1 个特定产品。

这是我的查询:

GET myindex/_search
{
  "_source": [
    "product.ref",
    "product.urls.*",
    "product.i18ns.*.title",
    "product_sale_elements.quantity",
    "product_sale_elements.prices.*.price",
    "product_sale_elements.listen_price.*",
    "product.images.image_url",
    "product.image_count",
    "product.images.visible",
    "product.images.position"
  ],
  "size": "6",
  "from": "0",
  "query": {
    "function_score": {
      "functions": [
        {
          "field_value_factor": {
            "field": "product.sales_count",
            "missing": 0,
            "modifier": "log1p"
          }
        },
        {
          "field_value_factor": {
            "field": "product.image_count",
            "missing": 0,
            "modifier": "log1p"
          }
        },
        {
          "field_value_factor": {
            "field": "featureCount",
            "missing": 0,
            "modifier": "log1p"
          }
        }
      ],
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "product.is_visible": true
              }
            }
          ],
          "should": [
            {
              "query_string": {
                "default_field": "product.ref",
                "query": "13141000",
                "boost": 2
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "by_categories": {
      "terms": {
        "field": "categories.i18ns.de_DE.title.raw",
        "size": 100
      }
    }
  }
}

因此,我的问题是,为什么这个查询会返回 10k 个结果,而我只想要具有该参考号的 1 个单一产品。

如果我这样做:

GET my-index/_search
{
  "query": {
    "match": {
      "product.ref": "13141000"
    }
  }
}

匹配正确。 should 与普通的 match 查询有何不同?

【问题讨论】:

  • 为什么要在should中加上product.ref的约束?如果必须匹配,则需要进入filter 数组

标签: elasticsearch lucene matching booleanquery


【解决方案1】:

如果您有 mustfilter 子句,那么与 mustfilter 匹配的任何内容都不必匹配您的 should 子句,因为它被视为“可选”

您可以将 should 子句中的 query_string 移动到 filter 或像这样将 minimum_should_match 设置为 1

...
"should": [
  {
    "query_string": {
      "default_field": "product.ref",
      "query": "13141000",
      "boost": 2
    }
  }
],
"minimum_should_match" : 1,
...

【讨论】:

    【解决方案2】:

    必须 - 条件必须匹配。

    应该 - 如果条件匹配,那么它将提高非过滤上下文中的分数。 (如果 minimum_should_match 没有明确声明)

    如您所见,must 类似于过滤器,但也提供评分。过滤器不会提供任何评分。

    你可以把这个子句放在一个新的 must 子句中:

    {
                  "query_string": {
                    "default_field": "product.ref",
                    "query": "13141000",
                    "boost": 2
                  }
                }
    

    如果您将上述内容放在过滤子句中,则提升不会影响评分。

    阅读有关布尔查询的更多信息here

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多