From 27342af2ffd8d3eb0c7101e78e7abc8d5712dd9d Mon Sep 17 00:00:00 2001 From: afoucret Date: Thu, 10 Jul 2025 12:24:45 +0200 Subject: [PATCH 1/4] Add LTR retriver example in the doc. --- .../ranking/learning-to-rank-search-usage.md | 55 ++++++++++++++++--- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/solutions/search/ranking/learning-to-rank-search-usage.md b/solutions/search/ranking/learning-to-rank-search-usage.md index 3a3d913a56..786da31450 100644 --- a/solutions/search/ranking/learning-to-rank-search-usage.md +++ b/solutions/search/ranking/learning-to-rank-search-usage.md @@ -18,11 +18,15 @@ products: This feature was introduced in version 8.12.0 and is only available to certain subscription levels. For more information, see {{subscriptions}}. :::: +Once your LTR model is trained and deployed in {{es}}, there are two ways to use it with the [search API](../querying-for-search.md) to improve your search results: + +1. **As a [rescorer](#learning-to-rank-rescorer)** +2. **As a [retriever](#learning-to-rank-retriever)** ## Learning To Rank as a rescorer [learning-to-rank-rescorer] -Once your LTR model is trained and deployed in {{es}}, it can be used as a [rescorer](elasticsearch://reference/elasticsearch/rest-apis/filter-search-results.md#rescore) in the [search API](../querying-for-search.md): +To use your LTR model as a [rescorer](elasticsearch://reference/elasticsearch/rest-apis/filter-search-results.md#rescore) in the [search API](../querying-for-search.md), follow this example: ```console GET my-index/_search @@ -50,22 +54,55 @@ GET my-index/_search 3. Named parameters to be passed to the query templates used for feature. 4. The number of documents that should be examined by the rescorer on each shard. +## Learning To Rank as a retriever [learning-to-rank-retriever] +LTR models can also be used as a [retriever](../retrievers-overview.md) in the search pipeline by utilizing a [rescorer retriever](elasticsearch://reference/elasticsearch/rest-apis/retrievers.md#rescorer-retriever) as shown in the following example: -### Known limitations [learning-to-rank-rescorer-limitations] - +```console +GET my-index/_search +{ + "retriever": { + "rescorer": { + "rescore": { + "window_size": 100, <4> + "learning_to_rank": { + "model_id": "ltr-model", <2> + "params": { <3> + "query_text": "the quick brown fox" + } + } + }, + "retrievers": [ <1> + { + "standard": { + "query": { + "multi_match": { + "fields": ["title", "content"], + "query": "the quick brown fox" + } + } + } + } + ] + } + } +} +``` -#### Rescore window size [learning-to-rank-rescorer-limitations-window-size] +1. First pass retrievers used to retrieve documents to be rescored +2. The unique identifier of the trained model uploaded to {{es}}. +3. Named parameters to be passed to the query templates used for feature extraction. +4. The number of documents that should be examined by the rescorer. -Scores returned by LTR models are usually not comparable with the scores issued by the first pass query and can be lower than the non-rescored score. This can cause the non-rescored result document to be ranked higher than the rescored document. To prevent this, the `window_size` parameter is mandatory for LTR rescorers and should be greater than or equal to `from + size`. +## Known limitations [learning-to-rank-rescorer-limitations] -#### Pagination [learning-to-rank-rescorer-limitations-pagination] +### Rescore window size [learning-to-rank-rescorer-limitations-window-size] -When exposing pagination to users, `window_size` should remain constant as each page is progressed by passing different `from` values. Changing the `window_size` can alter the top hits causing results to confusingly shift as the user steps through pages. +Scores returned by LTR models are usually not comparable with the scores issued by the first pass query and can be lower than the non-rescored score. This can cause the non-rescored result document to be ranked higher than the rescored document. To prevent this, the `window_size` parameter is mandatory for LTR rescorers and should be greater than or equal to `from + size`. -#### Negative scores [learning-to-rank-rescorer-limitations-negative-scores] +### Pagination [learning-to-rank-rescorer-limitations-pagination] -Depending on how your model is trained, it’s possible that the model will return negative scores for documents. While negative scores are not allowed from first-stage retrieval and ranking, it is possible to use them in the LTR rescorer. +When exposing pagination to users, `window_size` should remain constant as each page is progressed by passing different `from` values. Changing the `window_size` can alter the top hits causing results to confusingly shift as the user steps through pages. From b15ab3f27bf18616e9ee703c8896351a1c9733e8 Mon Sep 17 00:00:00 2001 From: afoucret Date: Thu, 10 Jul 2025 12:48:39 +0200 Subject: [PATCH 2/4] Add applies_to for LTR as a retriever --- solutions/search/ranking/learning-to-rank-search-usage.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/solutions/search/ranking/learning-to-rank-search-usage.md b/solutions/search/ranking/learning-to-rank-search-usage.md index 786da31450..495dac526e 100644 --- a/solutions/search/ranking/learning-to-rank-search-usage.md +++ b/solutions/search/ranking/learning-to-rank-search-usage.md @@ -56,6 +56,11 @@ GET my-index/_search ## Learning To Rank as a retriever [learning-to-rank-retriever] +```{applies_to} +stack: ga 9.1 +serverless ga +``` + LTR models can also be used as a [retriever](../retrievers-overview.md) in the search pipeline by utilizing a [rescorer retriever](elasticsearch://reference/elasticsearch/rest-apis/retrievers.md#rescorer-retriever) as shown in the following example: ```console From a27350036aa19007870b80de1f296c56ec795e44 Mon Sep 17 00:00:00 2001 From: afoucret Date: Thu, 10 Jul 2025 12:52:14 +0200 Subject: [PATCH 3/4] Fix typo --- solutions/search/ranking/learning-to-rank-search-usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/search/ranking/learning-to-rank-search-usage.md b/solutions/search/ranking/learning-to-rank-search-usage.md index 495dac526e..6743cdf92d 100644 --- a/solutions/search/ranking/learning-to-rank-search-usage.md +++ b/solutions/search/ranking/learning-to-rank-search-usage.md @@ -58,7 +58,7 @@ GET my-index/_search ```{applies_to} stack: ga 9.1 -serverless ga +serverless: ga ``` LTR models can also be used as a [retriever](../retrievers-overview.md) in the search pipeline by utilizing a [rescorer retriever](elasticsearch://reference/elasticsearch/rest-apis/retrievers.md#rescorer-retriever) as shown in the following example: From b72a7425f8a00142f4c8aa74b4a26b2363839f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20FOUCRET?= Date: Thu, 10 Jul 2025 14:38:54 +0200 Subject: [PATCH 4/4] Update solutions/search/ranking/learning-to-rank-search-usage.md Co-authored-by: Liam Thompson --- solutions/search/ranking/learning-to-rank-search-usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/search/ranking/learning-to-rank-search-usage.md b/solutions/search/ranking/learning-to-rank-search-usage.md index 6743cdf92d..cbc63f3218 100644 --- a/solutions/search/ranking/learning-to-rank-search-usage.md +++ b/solutions/search/ranking/learning-to-rank-search-usage.md @@ -61,7 +61,7 @@ stack: ga 9.1 serverless: ga ``` -LTR models can also be used as a [retriever](../retrievers-overview.md) in the search pipeline by utilizing a [rescorer retriever](elasticsearch://reference/elasticsearch/rest-apis/retrievers.md#rescorer-retriever) as shown in the following example: +LTR models can also be used as a [retriever](../retrievers-overview.md) in the search pipeline. You can implement this with a [rescorer retriever](elasticsearch://reference/elasticsearch/rest-apis/retrievers.md#rescorer-retriever) as shown in the following example: ```console GET my-index/_search