0
0
Elasticsearchquery~5 mins

Constant score query in Elasticsearch

Choose your learning style9 modes available
Introduction

A constant score query lets you find documents that match a condition and gives them all the same score. This is useful when you want to filter results without changing their ranking.

When you want to filter documents by a condition but don't care about scoring differences.
When you want to improve search speed by skipping score calculations.
When you want to combine filters with other queries but keep scores constant.
When you want to boost documents equally based on a filter.
When you want to apply a filter inside a query without affecting relevance scores.
Syntax
Elasticsearch
{
  "constant_score": {
    "filter": {
      "term": { "field": "value" }
    },
    "boost": 1.2
  }
}

The filter part defines which documents to match.

The boost sets the score for all matched documents equally.

Examples
Finds all documents where status is active and gives them the same score.
Elasticsearch
{
  "constant_score": {
    "filter": {
      "term": { "status": "active" }
    }
  }
}
Finds documents with age greater or equal to 30 and boosts their score by 2.
Elasticsearch
{
  "constant_score": {
    "filter": {
      "range": { "age": { "gte": 30 } }
    },
    "boost": 2.0
  }
}
Sample Program

This query finds all documents where the category is books. All matched documents get the same score boosted by 1.5.

Elasticsearch
{
  "query": {
    "constant_score": {
      "filter": {
        "term": { "category": "books" }
      },
      "boost": 1.5
    }
  }
}
OutputSuccess
Important Notes

Constant score queries are faster because they skip scoring calculations.

You can use any filter inside the filter part, like term, range, or bool.

If you don't set boost, the default score is 1.

Summary

Constant score queries find documents by a filter and give them all the same score.

This helps when you want to filter results without changing their order.

You can add a boost to increase the score of all matched documents equally.