Elasticsearch函数得分查询未达到零分

时间:2016-11-14 10:40:44

标签: elasticsearch

我有一个查询,我已将其简化为:

GET /foos-33/_search
{
  "from" : 0,
  "size" : 25,
  "query" : {
    "function_score" : {
      "query" : {
        "bool" : {
          "filter" : {
            "bool" : {
              "must" : [ {
                "bool" : {
                  "must_not" : {
                    "terms" : {
                      "foo.id" : [ ]
                    }
                  }
                }
              } ]
            }
          }
        }
      },
      "functions" : [ {
        "field_value_factor" : {
          "field" : "foo.strategicBoost",
          "missing" : 1.0
        }
      } ],
      "score_mode" : "sum"
    }
  },
  "explain" : true,
  "sort" : [ {
    "counts.barsPerDay" : {
      "order" : "desc"
    }
  } ]
}

命中分数始终为零。解释输出类型显示了为什么会发生这种情况,但我不完全理解发生了什么:

        "_explanation": {
           "value": 0,
           "description": "function score, product of:",
           "details": [
              {
                 "value": 0,
                 "description": "ConstantScore(-() +*:*), product of:",
                 "details": [
                    {
                       "value": 0,
                       "description": "boost",
                       "details": []
                    },
                    {
                       "value": 1,
                       "description": "queryNorm",
                       "details": []
                    }
                 ]
              },
              {
                 "value": 10,
                 "description": "min of:",
                 "details": [
                    {
                       "value": 10,
                       "description": "field value function: none(doc['foo.strategicBoost'].value?:1.0 * factor=1.0)",
                       "details": []
                    },
                    {
                       "value": 3.4028235e+38,
                       "description": "maxBoost",
                       "details": []
                    }
                 ]
              }
           ]
        }
     },

我尝试将它包装在一个constant_score中,将常量分数从0更改为1,如下所示:

GET /foos-33/_search
{
  "from" : 0,
  "size" : 25,
  "query" : {
    "function_score" : {
      "query" : {
        "bool" : {
          "constant_score": {
              "boost": 1,
          "filter" : {
            "bool" : {
              "must" : [ {
                "bool" : {
                  "must_not" : {
                    "terms" : {
                      "foo.id" : [ ]
                    }
                  }
                }
              } ]
            }
          }
          }
        }
      },
      "functions" : [ {
        "field_value_factor" : {
          "field" : "foo.strategicBoost",
          "missing" : 1.0
        }
      } ],
      "score_mode" : "sum"
    }
  },
  "explain" : true,
  "sort" : [ {
    "counts.barsPerDay" : {
      "order" : "desc"
    }
  } ]
}

但是这给了我一条错误信息:

"failed_shards": [
     {
        "shard": 0,
        "index": "foos-33",
        "node": "A9s2Ui3mQE2SBZhY2VkZGw",
        "reason": {
           "type": "query_parsing_exception",
           "reason": "[bool] query does not support [constant_score]",
           "index": "foos-33",
           "line": 8,
           "col": 29
        }
     }
  ]

还有另一种方法可以尝试解决此问题 - 我可以尝试将product更改为sum或其他 - 但我无法弄清楚product的位置来自。

1 个答案:

答案 0 :(得分:1)

顶级“产品”来自boost_mode,默认为multiply。在这种情况下,将boost_mode设置为replace是正确的解决方案 - 查询得分始终为零,因此我们不关心它。在这种情况下,将boost_mode设置为sum也同样有效。

相关问题