Leif
Leif

Reputation: 2170

Query with term filter and function_score always gives score 0

I need a query that does the following:

I simplified my real query a little to demonstrate it:

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

Problem: The score of each document is 0.

Explanation by 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": []
                }
              ]
            }
          ]
        }

You see, that the term filter has a score of 0. With multiplication the overall score becomes 0. I've been trying to give the term filter a score of 1 or get rid of its influence on the score somehow, but did not find a valid solution. So I guess, the query has to be restructured heavily, but how?

Upvotes: 1

Views: 1870

Answers (1)

Radu Gheorghe
Radu Gheorghe

Reputation: 1124

I'm probably late to the party, but in case it helps someone else:

Either solution may become problematic if, down the road, you want to add must/should clauses that actually affect the score. Then you will probably want to include/exclude that match_all or constant_score from your code (or from a search template).

Upvotes: 2

Related Questions