Constant score query

Updated at:

Use a constant score query (ConstScoreQuery) when you need to filter documents without letting the filter affect relevance ranking. Every matching document receives a fixed score of 1.0 instead of a BM25-calculated score.

Use cases

  • When ranking by relevance is not required, ConstScoreQuery eliminates score differences caused by factors such as term frequency and text length. You can then apply custom sort logic — such as FieldSort or PrimaryKeySort — to control result order directly.

  • When certain clauses in the mustQueries of a BoolQuery should act as filters only, wrap them with ConstScoreQuery to exclude their score contribution. Only the remaining scored clauses determine the final ranking.

  • When a condition is used purely for filtering, ConstScoreQuery skips relevance scoring entirely.

Comparison with BoolQuery filterQueries

Unlike BoolQuery filterQueries, which contribute no score, ConstScoreQuery contributes a fixed score of 1.0. This makes ConstScoreQuery useful when you need a scoring placeholder — for example, to provide a uniform baseline when combining it with other scored queries. The table below summarizes the key differences.

Aspect

BoolQuery filterQueries

ConstScoreQuery

Score contribution

Contributes no score

Contributes a fixed score of 1.0

Where to use

Can be used only as a filterQueries clause of a BoolQuery

Can be used independently, or placed in the mustQueries or shouldQueries clause of a BoolQuery

Typical use

Pure filtering that does not affect ranking

Scenarios that require a scoring placeholder, such as setting a uniform baseline when combined with other scored queries

Operations

A constant score query is available through the Search and ParallelScan operations, with the query type set to ConstScoreQuery.

Parameters

Parameter

Description

filter

The subquery condition to evaluate. Supports any query type, such as MatchQuery, TermQuery, or BoolQuery. ConstScoreQuery only checks whether a document matches this condition — matching documents receive a fixed score of 1.0 and are excluded from BM25 relevance scoring.

getTotalCount

Specifies whether to return the total number of matched rows. Default: false. Enabling this option affects query performance.

tableName

The name of the data table.

indexName

The name of the Search Index.

columnsToGet

Specifies the columns to return. Set returnAll to true to return all columns. When returnAll is false (default), use columns to specify which columns to return. If columns is empty, only primary key columns are returned.

References

Tablestore Search Index supports ConstScoreQuery in the Java SDK and Go SDK.