Search using Learning To Rank

edit

This feature was introduced in version 8.12.0 and is only available to certain subscription levels. For more information, see https://www.elastic.co/subscriptions.

Learning To Rank as a rescorer

edit

Once your LTR model is trained and deployed in Elasticsearch, it can be used as a rescorer in the search API:

resp = client.search(
    index="my-index",
    query={
        "multi_match": {
            "fields": [
                "title",
                "content"
            ],
            "query": "the quick brown fox"
        }
    },
    rescore={
        "learning_to_rank": {
            "model_id": "ltr-model",
            "params": {
                "query_text": "the quick brown fox"
            }
        },
        "window_size": 100
    },
)
print(resp)
response = client.search(
  index: 'my-index',
  body: {
    query: {
      multi_match: {
        fields: [
          'title',
          'content'
        ],
        query: 'the quick brown fox'
      }
    },
    rescore: {
      learning_to_rank: {
        model_id: 'ltr-model',
        params: {
          query_text: 'the quick brown fox'
        }
      },
      window_size: 100
    }
  }
)
puts response
const response = await client.search({
  index: "my-index",
  query: {
    multi_match: {
      fields: ["title", "content"],
      query: "the quick brown fox",
    },
  },
  rescore: {
    learning_to_rank: {
      model_id: "ltr-model",
      params: {
        query_text: "the quick brown fox",
      },
    },
    window_size: 100,
  },
});
console.log(response);
GET my-index/_search
{
  "query": { 
    "multi_match": {
      "fields": ["title", "content"],
      "query": "the quick brown fox"
    }
  },
  "rescore": {
    "learning_to_rank": {
      "model_id": "ltr-model", 
      "params": { 
        "query_text": "the quick brown fox"
      }
    },
    "window_size": 100 
  }
}

First pass query providing documents to be rescored.

The unique identifier of the trained model uploaded to Elasticsearch.

Named parameters to be passed to the query templates used for feature.

The number of documents that should be examined by the rescorer on each shard.

Known limitations
edit
Rescore window size
edit

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.

Pagination
edit

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.

Negative scores
edit

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.