elasticsearch - 分数结果更正

标签 elasticsearch elasticsearch-scripting

我想知道是否有可能以 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/

相关文章:

elasticsearch - Elasticsearch:按嵌套对象中集合的字段上的最大值过滤

elasticsearch - 在Elasticsearch集群中索引文档时出错

java - spring boot 2 与 elasticsearch 2.4 通过 spring data

python - 如何使用python将数据从Kafka发送到ElasticSearch

elasticsearch - 如何汇总几个替代术语?

elasticsearch - 如何在嵌套 script_score 的过滤器之间添加?

elasticsearch - Elasticsearch轻松更新唯一元素列表

elasticsearch - 从ElasticSearch查询的日期字段中提取日期(yyyyMMdd)