提问者:小点点

使用术语过滤器和function_score的查询总是给出0分


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

  • 筛选文档
  • 按地理距离划分的分数
  • 使用升压模式“乘法”(这是默认设置)

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

"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分,或者设法消除它对分数的影响,但没有找到有效的解决方案。所以我想,查询必须进行大规模的重构,但是如何重构呢?


共1个答案

匿名用户

我可能迟到了,但万一它对其他人有帮助:

    < li >您可以在must子句中使用匹配所有查询 < li >您可以将整个bool查询包装在常量分数查询中

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