我想知道是否有可能以 flex 方式实现。假设我有这种结构的文档。
{
"title": "text1 text2 text3",
"score_adj":[
{"text":"text1", "adj": -2},
{"text":"text3", "adj": -4}
]
}
我将查找title
包含单词text1
的文档,找到该文档,并且,由于该单词在score_adj
中并且具有-2
,我将以此数字降低最终分数。可以实现这种逻辑吗?我知道有script_score
可以更改分数,但是我不知道如何重复这种情况。
最佳答案
这是一种可能性:
{
"query": {
"bool": {
"must": [
{
"function_score": {
"query": {
"match": {
"title": "text1"
}
},
"functions": [
{
"script_score": {
"script": {
"source": """
def found = params._source['score_adj'].find(item -> item.text==params.text);
if (found != null) {
// adj is always negative so use plus
return _score + found.adj
}
return _score;
""",
"params": {
"text": "text1"
}
}
}
}
],
"boost_mode": "replace"
}
}
]
}
}
}
但这可能会导致错误script score function must not produce negative scores, but got [-1.7123179137706757]
因此您必须考虑到这一点。也许先将原始
_score
乘以10左右,这样就可以确保不会成为负数。我认为将
adj
转换为百分比而不是具体值会更合理...
关于elasticsearch - 分数结果更正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63396466/