【问题标题】:Query with term filter and function_score always gives score 0使用术语过滤器和 function_score 的查询总是给出分数 0
【发布时间】:2019-01-03 22:53:14
【问题描述】:

我需要一个执行以下操作的查询:

  • 过滤文档
  • 地理距离得分
  • 使用增强模式“倍增”(这是默认设置)

我稍微简化了我的真实查询来演示它:

"query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [],
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "is_good": true
                  }
                }
                // here could be another filter
              ]
            }
          }
        }
      },
      "functions": [
        {
          "linear": {
            "locations.geo_point": {
              "origin": {
                "lat": 52.518611,
                "lon": 13.408333
              },
              "offset": "3km",
              "scale": "15km"
            }
          }
        }
      ]
    }
  }

问题:每个文档的分数都是0。

解释来自elasticsearch:

"_explanation": {
          "value": 0,
          "description": "function score, product of:",
          "details": [
            {
              "value": 0,
              "description": "ConstantScore(is_good:T)^0.0",
              "details": []
            },
            {
              "value": 1,
              "description": "min of:",
              "details": [
                {
                  "value": 1,
                  "description": "Function for field locations.geo_point:",
                  "details": [
                    {
                      "value": 1,
                      "description": "max(0.0, ((30000.0 - MIN of: [0.0])/30000.0)",
                      "details": []
                    }
                  ]
                },
                {
                  "value": 3.4028235e+38,
                  "description": "maxBoost",
                  "details": []
                }
              ]
            }
          ]
        }

您会看到,术语过滤器的分数为 0。通过乘法,总分变为 0。我一直试图给术语过滤器评分 1 或以某种方式摆脱它对分数的影响,但是没有找到有效的解决方案。所以我想,查询必须进行大量重组,但是如何?

【问题讨论】:

  • 过滤器不计算分数,所以它总是为零。

标签: elasticsearch


【解决方案1】:

我可能迟到了,但万一这对别人有帮助:

如果您想添加实际影响分数的必须/应该子句,则任何一种解决方案都可能会出现问题。然后,您可能希望在您的代码(或 search template)中包含/排除 match_all 或 constant_score。

【讨论】:

  • 将 bool 查询包装在常量分数中会导致解析异常:“[constant_score] 查询不支持 [bool]”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 2019-09-10
  • 1970-01-01
  • 2020-09-01
  • 2022-10-07
相关资源
最近更新 更多