我有一个要求,需要对名称进行自定义评分。为了简单起见,可以说,如果我在索引中针对名称搜索“Smith”,逻辑应该是:
if input = exact 'Smith' then score = 100%
else
if input = phonetic match then
score = <depending upon fuzziness match of input with name>%
end if
end if;
我可以搜索模糊度为 1 的文档,但我不知道如何根据它的模糊程度给它自定义分数。谢谢!
更新:
我浏览了一篇与我有相同要求的帖子,并提到该人通过使用 native 脚本解决了它。我的问题仍然存在,如何根据相似距离实际获得分数,以便可以在 native 脚本中使用:
供引用的帖子:
https://discuss.elastic.co/t/fuzzy-query-scoring-based-on-levenshtein-distance/11116
要在帖子中查找的文本:
“对于 future 的读者,我通过创建自定义分数查询和
编写( native )脚本来处理评分。”
最佳答案
您可以使用 rescore 函数查询 (docs here) 来实现此搜索逻辑。
这里有一个可能的例子:
{
"query": {
"function_score": {
"query": { "match": {
"input": "Smith"
} },
"boost": "5",
"functions": [
{
"filter": { "match": { "input.keyword": "Smith" } },
"random_score": {},
"weight": 23
}
]
}
}
}
在这个例子中,我们有一个输入字段作为文本和关键字索引的映射(input.keyword 用于精确匹配)。对于与第一个查询匹配的所有文档(在示例中是匹配项,但在您的情况下将是模糊查询),我们对与术语“Smith”完全匹配的文档重新评分,得分更高。
您可以通过调整权重参数来控制重新评分效果。
关于elasticsearch - elasticsearch中精确、语音和模糊匹配的自定义分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472846/