【问题标题】:Search for two fields but only score once in Elasticsearch在 Elasticsearch 中搜索两个字段但只得分一次
【发布时间】:2020-10-29 08:35:29
【问题描述】:

假设我在 Elasticsearch 中有这些文档:

{
    "display_name": "Jose Cummings",
    "username": "josecummings"
},
{
    "display_name": "Jose Ramirez",
    "username": "elite_gamer"
},
{
    "display_name": "Lance Abrams",
    "username": "abrams1"
},
{
    "display_name": "Steve Smith",
    "username": "josesmose"
}

我想针对display_nameusername 字段运行“键入时”搜索Jose,我可以这样做:

{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "fields": [
                        "display_name",
                        "username"
                    ],
                    "query": "Jose",
                    "type": "bool_prefix",
                    "fuzziness": "AUTO",
                    "boost": 50
                }
            }
        }
    }
}

这里的问题是,当我搜索Jose 时,Jose Cummings 得到 100 分,而 Jose Ramirez 和 Steve Smith 只得到 50 分,因为这似乎是两个字段的得分相加。这实质上是奖励拥有与username 相同的display_name 的用户,这是我们不希望发生的。

有没有办法只从两个字段中获取最高分数?我现在使用function_scoreboost_mode/score_modeconstant_score 尝试了几十种不同的组合,尝试使用多个match_bool_prefix 查询等进行should 匹配。我没有尝试过似乎实现这一目标。

【问题讨论】:

    标签: elasticsearch relevance


    【解决方案1】:

    试试这个:

    {
      "query": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "fields": [
                  "display_name^50",
                  "username^50"
                ],
                "query": "Jose",
                "type": "bool_prefix",
                "fuzziness": "AUTO",
                "tie_breaker": 0.3
              }
            }
          ]
        }
      }
    }
    

    注意tie_breaker 设置为 0.0 而不是 0


    另请注意,您的 bool_prefix

    评分的行为类似于most_fields,但使用match_bool_prefix 查询而不是match 查询。

    也许您确实希望字段以jose 为前缀。但是,如果用户名是 cool_jose,它将被忽略(除非您使用非标准的 analyzer)...

    【讨论】:

    • 我认为 tie_breaker 设置为 0 符合我的预期。至少在最初的测试中。我稍后会实施并假设它有效,在一两天内将其标记为正确。谢谢你。对于您的第二个说明,这是针对您键入时搜索的交易。我确实想使用前缀。我有一个更通用的匹配的第二个端点,前端人员不喜欢它在用例中的行为方式。
    • 我听到了。好的,酷,让我知道测试进展如何。
    • 在应用程序中实现它之后,它似乎完全符合我的期望。感谢您的帮助!
    • 很高兴能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    相关资源
    最近更新 更多