liveBook · Manning

Constant_score query

As explained in Elasticsearch in Action, Second Edition

An Elasticsearch scoring mode that assigns the same score to every document matching a query.

Suppose a product search should find TVs rated between 4 and 5, then give every matching product the same score instead of ranking by relevance. Use constant_score for that controlled result ordering. section 11.4 section 11.2

constant_score wraps a filter query and assigns its matches a user-selected score through boost. A filter alone produces no meaningful relevance score; constant_score restores a nonzero, identical score for every match. section 11.4 section 8.4.2

Listing 11.17 A constant_score query that produces a static score
GET products/_search
{
  "query": {
    "constant_score": {     #1
      "filter": {           #2
          "range": {
            "user_ratings": {
              "gte": 4,
              "lte": 5
            }
          }
        },
      "boost": 5.0          #3
    }
  }
}

Use the range filter to select products rated from 4 through 5, then set every matching document's score to 5.0 with boost. section 11.4

Combine constant_score with ordinary relevance when a structured condition should influence ranking:

  • Place a match query for the main search term in a bool must clause. section 11.4
  • Wrap a term or range filter in constant_score to reward matching documents with a fixed boost. section 11.4
  • For example, match TVs and add 3.5 to the score of black TVs. section 11.4
Listing 11.18 A bool query with a constant score
GET products/_search
{
  "query": {
    "bool": {
      "must": [{                 #1
          "match": {             #2
            "product": "TV"
          }
        },
        {
          "constant_score": {    #3
            "filter": {
              "term": {
                "colour": "black"
              }
            },
            "boost": 3.5
          }
        }
      ]
    }
  }
}

This bool query combines text relevance for TVs with a constant 3.5 boost for documents whose colour is black. section 11.4

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines filter wrapping and boost, then demonstrates standalone scoring and combination with bool and match queries.
Examples
Demonstrates product ratings from 4 to 5 and black televisions receiving a fixed 3.5 boost beside a TV match.
What it assumes
Assumes basic leaf queries and filter context, while establishing how compound queries combine those building blocks.
What you can do after
Apply it via Elasticsearch Query DSL to give filtered documents identical scores or add a fixed ranking boost.
How it connects
Connects filter context, bool queries, match queries, term queries, range queries, relevance scoring, and boosting queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.