1
votes

Following is my function_score query. I want to give additional score to documents where the product quality is better.

But _score in the search response is always 0. What am I missing? Thx.

When I remove bool query and replace it with just a term filter, the score is non zero. I am guessing it is about the query bool but can not figure out why.

Elasticsearch version is 2.4

{
  "from": 0,
  "size": 20,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "should": {
                  "terms": {
                    "categories.category1Id": [
                      63
                    ]
                  }
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "updatedDate": {
              "origin": "2016-10-03 05:10:18",
              "scale": "0.5h",
              "decay": 0.1,
              "offset": "1h"
            }
          }
        },
        {
          "filter": {
            "term": {
              "productQuality": "EXCELLENT"
            }
          },
          "weight": 7
        },
        {
          "filter": {
            "term": {
              "productQuality": "HIGH"
            }
          },
          "weight": 5
        },
        {
          "filter": {
            "term": {
              "productQuality": "MEDIUM"
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "term": {
              "productQuality": "LOW"
            }
          },
          "weight": 1
        }
      ],
      "score_mode": "sum"
    }
  }
}
1
The very first part of your query is a filter. Filters only return a Constant Score. You can use the explain api to see how the query works: elastic.co/guide/en/elasticsearch/reference/current/… - jay

1 Answers

4
votes

As what @Val said.

bool.filter assigns a score of 0 to all documents, as no scoring query has been specified (link).

If you need the score, you can add "must": {"match_all": {}} in your query. match_all will assign 1.0 to all documents (link).

Here is your query with match_all:

{
  "from": 0,
  "size": 20,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": { 
              "match_all": {}
          },
          "filter": [
            {
              "bool": {
                "should": {
                  "terms": {
                    "categories.category1Id": [
                      63
                    ]
                  }
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "updatedDate": {
              "origin": "2016-10-03 05:10:18",
              "scale": "0.5h",
              "decay": 0.1,
              "offset": "1h"
            }
          }
        },
        {
          "filter": {
            "term": {
              "productQuality": "EXCELLENT"
            }
          },
          "weight": 7
        },
        {
          "filter": {
            "term": {
              "productQuality": "HIGH"
            }
          },
          "weight": 5
        },
        {
          "filter": {
            "term": {
              "productQuality": "MEDIUM"
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "term": {
              "productQuality": "LOW"
            }
          },
          "weight": 1
        }
      ],
      "score_mode": "sum"
    }
  }
}