【问题标题】:elasticsearch Is it possible to combine score_mode for function_score queryelasticsearch 是否可以结合 score_mode 进行 function_score 查询
【发布时间】:2016-11-24 17:31:21
【问题描述】:

我有一个带有 2 个衰减函数(高斯)和 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"
  }
}

提前致谢。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我找到了解决办法。

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

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 2014-10-11
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 2019-12-11
      • 2018-02-12
      • 1970-01-01
      相关资源
      最近更新 更多