【问题标题】:How to apply custom score to a search filed in Elastic Search如何将自定义分数应用于 Elasticsearch 中的搜索字段
【发布时间】:2022-11-01 22:18:10
【问题描述】:

我正在 Elastic Search 中进行搜索查询,并且我想在它们匹配时将它们视为相同的字段。例如,如果我搜索字段字段1并且匹配,则 _score 增加 10(例如),对于字段2.

我试过 function_score 但它不起作用。它抛出一个错误。

"caused_by": {
    "type": "class_cast_exception",
    "reason": "class 
               org.elasticsearch.index.fielddata.plain.SortedSetDVOrdinalsIndexFieldData 
               cannot be cast to class 
               org.elasticsearch.index.fielddata.IndexNumericFieldData 
               (org.elasticsearch.index.fielddata.plain.SortedSetDVOrdinalsIndexFieldData 
               and org.elasticsearch.index.fielddata.IndexNumericFieldData are in unnamed 
               module of loader 'app')"
}

查询:

{
  "track_total_hits": true,
  "size": 50,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "field1": {
                  "value": "Value 1"
                }
              }
            },
            {
              "term": {
                "field2": {
                  "value": "value 2"
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "field1",
            "factor": 10,
            "missing": 0
          }
        },
        {
          "field_value_factor": {
            "field": "field2",
            "factor": 10,
            "missing": 0
          }
        }
      ],
      "boost_mode": "multiply"
    }
  }
}

【问题讨论】:

    标签: amazon-web-services elasticsearch


    【解决方案1】:

    您可以使用功能分数with filter function 来提升。

    假设您的映射如下所示

    {
      "mappings": {
        "properties": {
          "field_1": {
            "type": "keyword"
          },
          "field_2": {
            "type": "keyword"
          }
        }
      }
    }
    

    有文件

    {"index":{}}
    {"field_1": "foo", "field_2": "bar"}
    {"index":{}}
    {"field_1": "foo", "field_2": "foo"}
    {"index":{}}
    {"field_1": "bar", "field_2": "bar"}
    

    您可以使用weight 参数来提升与每个查询匹配的文档。

    {
      "query": {
        "function_score": {
          "query": {
            "match_all": {}
          },
          "functions": [
            {
              "filter": {
                "term": {
                  "field_1": "foo"
                }
              },
              "weight": 10
            },
            {
              "filter": {
                "term": {
                  "field_2": "foo"
                }
              },
              "weight": 20
            }
          ],
          "score_mode": "multiply"
        }
      }
    }
    

    【讨论】:

    • 据我所知,权重乘以某个匹配的分数,所以即使权重相等,分数仍然可能不同,我想要的是将分数设置为相等。我错了?
    • 如果您在 must 查询中使用 boost 参数[1],就会发生这种情况。我发布的查询的关键点与必须查询不同的是计算每个查询的分数,过滤查询返回常量分数(1.0),无论其中的查询是什么。 [2] 因此,如果权重相同,查询将返回相同的分数。 *1:elastic.co/guide/en/elasticsearch/reference/current/… *2:elastic.co/guide/en/elasticsearch/reference/current/…
    【解决方案2】:

    如果您想为查询中的不同字段提供手动权重,可以参考以下解决方案。这将始终替换查询响应顶部的最高权重字段 -

    Elasticsearch query different fields with different weight

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-21
      • 2021-06-05
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多