elasticsearch是否可以将score_mode与function_score查询结合使用

时间:2016-07-21 08:25:46

标签: elasticsearch

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

我怎样才能做到这一点?使用function_score查询的score_mode,我只能乘以或求和。

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

提前致谢。

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。

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