分数结果更正

问题描述

我想知道是否有可能以弹性方式实施。假设我有这种结构的文件

{
 "title": "text1 text2 text3","score_adj":[
  {"text":"text1","adj": -2},{"text":"text3","adj": -4}
 ]
}

然后我将查找title包含单词text1的文档,我也会找到此文档,并且,因为该单词在score_adj中并且包含{{1 }},我将最终得分降低此数字。可以实现这种逻辑吗?我知道有-2可以更改分数,但是我不知道如何重复这种情况。

解决方法

这是一种可能性:

{
  "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"
          }
        }
      ]
    }
  }
}

但这可能会导致错误

脚本分数函数不能产生负分数,但是得到 [-1.7123179137706757]

所以您必须考虑到这一点。也许先将原始的_score乘以10左右,这样就可以确保不会变成负数。

我认为将adj转换为百分比而不是具体值会更合理...