ES|QL RERANK command
The RERANK command uses an inference model to compute a new relevance score
for an initial set of documents, directly within your ES|QL queries.
Starting in version 9.3.0, RERANK automatically limits processing to 1000
rows by default to prevent accidental high consumption. This limit is applied
before the RERANK command executes.
If you need to process more rows, you can adjust the limit using the cluster setting:
PUT _cluster/settings
{
"persistent": {
"esql.command.rerank.limit": 5000
}
}
You can also disable the command entirely if needed:
PUT _cluster/settings
{
"persistent": {
"esql.command.rerank.enabled": false
}
}
No automatic row limit is applied. You should always use LIMIT before or after RERANK to control the number of documents processed, to avoid accidentally reranking large datasets which can result in high latency and increased costs.
For example:
FROM books
| WHERE title:"search query"
| SORT _score DESC
| LIMIT 100
| RERANK "search query" ON title WITH { "inference_id" : "my_rerank_endpoint" }
- Limit to top 100 results before reranking
RERANK [column =] query ON field [, field, ...] [WITH { "inference_id" : "my_inference_endpoint" [, "timeout" : "<timeout_duration>"] }]
RERANK [column =] query ON field [, field, ...] [WITH { "inference_id" : "my_inference_endpoint" }]
column- (Optional) The name of the output column containing the reranked scores.
If not specified, the results will be stored in a column named
_score. If the specified column already exists, it will be overwritten with the new results. query- The query text used to rerank the documents. This is typically the same query used in the initial search.
field- One or more fields to use for reranking. These fields should contain the text that the reranking model will evaluate.
inference_id- (Optional) The ID of
the inference endpoint
to use for the task.
The inference endpoint must be configured with the
reranktask type. If not specified, defaults to the preconfigured.rerank-v1-elasticsearchendpoint. timeout- (Optional) Timeout for the inference request (for example,
"30s","1m"). If not specified, the default search timeout applies. Use this to set a per-call timeout independent of the cluster-wide search timeout.
Use RERANK to re-score search results using a machine learning model for improved relevance.
Typically, you first use a WHERE clause with a function like MATCH to
retrieve an initial set of documents. This set is often sorted by _score and
reduced to the top results (for example, 100) using LIMIT. The RERANK
command then processes this smaller, refined subset, which is a good balance
between performance and accuracy.
When using RERANK with a multivalue column, each value is ranked individually.
The score column is then assigned the maximum score resulting from ranking the
individual values.
The RERANK command requires an
inference endpoint
configured with the rerank task type. If you omit the inference_id option,
RERANK uses the preconfigured .rerank-v1-elasticsearch endpoint, which is
available by default.
For improved relevance, you can use the preconfigured
.jina-reranker-v3 endpoint, powered by the
Elastic Inference Service (EIS).
To use a different model, create a rerank inference endpoint and specify
its ID in the WITH clause. Refer to
semantic reranking
for a full list of supported reranking models.
RERANK commands may time out when processing large datasets or complex
queries. The default timeout is 30 seconds.
You can set per-call timeout using the "timeout" option in the WITH clause:
RERANK "search query" ON title WITH { "inference_id": "my_inference_endpoint", "timeout": "1m" }
If you can't modify your timeout limits, try the following:
- Reduce data volume with
LIMITor more selective filters before theRERANKcommand - Split complex operations into multiple simpler queries
- Configure your HTTP client's response timeout (Refer to HTTP client configuration)
FROM books METADATA _score
| WHERE MATCH(description, "hobbit")
| SORT _score DESC
| LIMIT 100
| RERANK "hobbit" ON description WITH { "inference_id" : "test_reranker" }
| LIMIT 3
| KEEP title, _score
| title:text | _score:double |
|---|---|
| Poems from the Hobbit | 0.0015673980815336108 |
| The Lord of the Rings - Boxed Set | 0.0011135857785120606 |
| Letters of J R R Tolkien | 0.0024999999441206455 |
FROM books METADATA _score
| WHERE MATCH(description, "hobbit") OR MATCH(author, "Tolkien")
| SORT _score DESC
| LIMIT 100
| RERANK rerank_score = "hobbit" ON description, author WITH { "inference_id" : "test_reranker" }
| SORT rerank_score DESC, book_no DESC
| LIMIT 3
| title:text | _score:double | rerank_score:double |
|---|---|---|
| The Lord of the Rings Poster Collection: Six Paintings by Alan Lee (No. 1) | 0.7976278066635132 | 0.125 |
| A Middle English Reader and Vocabulary | 1.043297290802002 | 0.07692307978868484 |
| FINAL WITNESS | 1.4058171510696411 | 0.07692307978868484 |
FROM books METADATA _score
| WHERE MATCH(description, "hobbit") OR MATCH(author, "Tolkien")
| SORT _score DESC
| LIMIT 100
| RERANK rerank_score = "hobbit" ON title, description WITH { "inference_id" : "test_reranker" }
| EVAL original_score = _score, _score = rerank_score + original_score
| SORT _score
| LIMIT 3
| title:text | _score:double | rerank_score:double |
|---|---|---|
| The Lord of the Rings Poster Collection: Six Paintings by Alan Lee (No. 1) | 0.7976278 | 0.125 |
| A Middle English Reader and Vocabulary | 1.043297 | 0.07692308 |
| FINAL WITNESS | 1.405817 | 0.07692308 |
FROM books
| WHERE MATCH(title, "return")
| RERANK "Tolkien" ON TOP_SNIPPETS(description, "Tolkien", { "num_snippets": 3, "num_words": 25 }) WITH { "inference_id" : "test_reranker" }
| book_no:keyword | title:text | _score:double |
|---|---|---|
| 2714 | Return of the King Being the Third Part of The Lord of the Rings | 0.007092198356986046 |
| 7350 | Return of the Shadow | 0.012500000186264515 |