Robin Green
Robin Green

Reputation: 33033

Elasticsearch function score query stuck at zero score

I have a query which I've simplified down to this:

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

The scores of the hits are always zero. The explain output sort of shows why this is happening, but I don't completely understand what's going on:

        "_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": []
                    }
                 ]
              }
           ]
        }
     },

I tried to wrap it in a constant_score to change the constant score from 0 to 1, like this:

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

but that gave me an error message:

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

There is another way I could try to solve this problem - I could try to change the product to a sum or something - but I can't figure out where the product is coming from.

Upvotes: 2

Views: 2362

Answers (1)

Robin Green
Robin Green

Reputation: 33033

The top-level "product of" comes from the boost_mode, which defaults to multiply. Setting boost_mode to replace is the right fix in this case - the query score is always zero, so we don't care about it. Setting boost_mode to sum would be an equally valid fix in this case, too.

Upvotes: 4

Related Questions