elasticsearch是否可以结合score_mode用于function_score查询 [英] elasticsearch Is it possible to combine score_mode for function_score query

查看:514
本文介绍了elasticsearch是否可以结合score_mode用于function_score查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个衰减函数(均为高斯)和script_score函数的function_score查询。在script_score函数中,我将几个分数相加并相乘。现在,我想将结果与on gauss函数(位置)相乘,然后将其与另一个gauss函数(creation_date)相加。我想这样做是为了提高新文档的质量。

I have a function_score query with 2 decay functions (both gauss) and script_score function. In the script_score function I add and multiply a few scores. Now I want to multiply the result with on gauss function (the location) and then add it together with the other gauss function (the creation_date). I want to do that to give the newer documents a boost.

如何实现?使用function_score查询的score_mode,我只能相乘或相加。

How can I achieve this? With the score_mode of the function_score query I can only multiply or sum.

{
  "query": {
    "function_score": {
      "functions": [
        {
          "gauss": {
            "location": {
              "origin": {
                "lon": 16.37,
                "lat": 48.21
              },
              "scale": "100km",
              "offset": "15km",
              "decay": 0.3
            }
          }
        },
        {
          "gauss": {
            "creation_date": {
              "scale": "30d",
              "offset": "20d",
              "decay": 0.1
            }
          }
        },
        {
          "script_score": {
            "lang": "expression",
            "script": "(((doc['value_a'].value + doc['value_b'] + 1) * boost_a) + (ln(sqrt(doc['value_c'].value + 1)) * boost_c))",
            "params": {
              "boost_a": 0.2,
              "boost_b": 0.5
            }
          }
        }
      ],
      "query": {
        "match_all": {}
      },
      "score_mode": "multiply",
      "boost_mode": "multiply"
    }
  },
  "sort": {
    "_score": "desc"
  }
}

预先感谢。

推荐答案

我找到了解决方案。

{
  "query": {
    "function_score": {
      "functions": {
        "gauss": {
          "creation_date": {
            "scale": "30d",
            "offset": "20d",
            "decay": 0.1
          }
        }
      },
      "query": {
        "function_score": {
          "functions": [
            {
              "gauss": {
                "location": {
                  "origin": {
                    "lon": 16.37,
                    "lat": 48.21
                  },
                  "scale": "100km",
                  "offset": "15km",
                  "decay": 0.3
                }
              }
            },
            {
              "script_score": {
                "lang": "expression",
                "script": "(((doc['value_a'].value + doc['value_b'] + 1) * boost_a) + (ln(sqrt(doc['value_c'].value + 1)) * boost_c))",
                "params": {
                  "boost_a": 0.2,
                  "boost_b": 0.5
                }
              }
            }
          ],
          "query": {
            "match_all": {}
          },
          "score_mode": "multiply",
          "boost_mode": "multiply"
        }
      }
    },
    "score_mode": "multiply",
    "boost_mode": "sum"
  },
  "sort": {
    "_score": "desc"
  }
}

这篇关于elasticsearch是否可以结合score_mode用于function_score查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆