如何在ElasticSearch中使用match_phrase查询的单词顺序得分更高?

问题描述 投票:0回答:1

假设我的索引有两个文档:

  1. “拿钱”
  2. “我的钱到了这里”

[当我对“获取我的钱”进行常规匹配查询时,两个文档都正确匹配,但它们得到的分数相等。但是,我希望评分过程中单词的顺序重要。换句话说,我希望“赚钱”获得更高的分数。

因此,我尝试将我的匹配查询放入bool查询的must子句中,并包含一个match_phrase(具有相同的查询字符串)。在我搜索“我如何获得我的钱”之前,这似乎可以正确打出匹配。在这种情况下,match_phrase查询似乎不匹配,并且匹配结果再次以相等的分数返回。

如何构造索引/查询,以便考虑单词顺序,但不要求所有搜索到的单词都存在于文档中?

与测试数据的索引映射

PUT test-index
{
  "mappings": {
      "properties" : {
        "keyword" : {
          "type" : "text",
          "similarity": "boolean"
        }
      }
    }
}
POST test-index/_doc/
{
    "keyword" : "get my money"
}
POST test-index/_doc/
{
    "keyword" : "my money get here"
}

查询“我如何获得我的钱”-无法按需使用

GET /test-index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "keyword": "how do i get my money"
          }
        }
      ],
      "should": [
        {
          "match_phrase": {
            "keyword": {
              "query": "how do i get my money"
            }
          }
        }
      ]
    }
  }
}

结果(两个文件得分均相同)

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 3.0,
    "hits" : [
      {
        "_index" : "test-index",
        "_type" : "_doc",
        "_id" : "6Xy8wXIB3NtI_ttPGBoV",
        "_score" : 3.0,
        "_source" : {
          "keyword" : "get my money"
        }
      },
      {
        "_index" : "test-index",
        "_type" : "_doc",
        "_id" : "6ny8wXIB3NtI_ttPGBpV",
        "_score" : 3.0,
        "_source" : {
          "keyword" : "my money get here"
        }
      }
    ]
  }
}

谢谢你。

elasticsearch elastic-stack match-phrase
1个回答
0
投票

问题是由于您的similarity参数。

一个简单的布尔相似性,当不需要全文排名时使用,分数应仅基于查询词是否匹配。布尔相似度使术语的得分等于其查询的提升]

Reference

您应该使用其他相似性参数(BM25)以获得更好的分数。

© www.soinside.com 2019 - 2024. All rights reserved.