Notebooks
E
Elastic
12 Semantic Reranking Elastic Rerank

12 Semantic Reranking Elastic Rerank

openai-chatgptlangchain-pythonchatgptgenaielasticsearchelasticopenaiAIchatlogvectordatabasenotebooksPythonsearchgenaistackvectorelasticsearch-labslangchainapplications

Semantic reranking with Elastic Rerank

Open In Colab

In this notebook you'll learn how to implement semantic reranking in Elasticsearch using the built-in Elastic Rerank model. You'll also learn about the retriever abstraction, a simpler syntax for crafting queries and combining different search operations.

You will query your data using the text_similarity_rerank retriever, and the Elastic Rerank model to boost the relevance of your search results.

馃О Requirements

For this example, you will need:

鈩癸笍 Deploying the Elastic Rerank model in combination with ELSER (or other hosted models) requires at minimum 8GB ML node. The current max size for trial ML nodes is 4GB (defaults to 1GB).

Install packages

This will take a couple of minutes.

[ ]

Import packages

[ ]

Initialize Elasticsearch Python client

You need to connect to a running Elasticsearch instance. In this example we're using an Elastic Cloud deployment.

[5]
Elastic Cloud ID: 路路路路路路路路路路
Elastic Api Key: 路路路路路路路路路路

Test connection

Confirm that the Python client has connected to your Elasticsearch instance with this test.

[ ]

Enable Telemetry

Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See telemetry.py for details. Thank you!

[ ]

Upload sample data

This examples uses a small dataset of movies.

[7]
Done indexing documents into `movies` index!

Lexical queries

First let's use a standard retriever to test out some lexical (or full-text) searchs and then we'll compare the improvements when we layer in semantic reranking.

Lexical match with query_string query

Let's say we vaguely remember that there is a famous movie about a killer who eats his victims. For the sake of argument, pretend we've momentarily forgotten the word "cannibal".

Let's perform a query_string query to find the phrase "flesh-eating bad guy" in the plot fields of our Elasticsearch documents.

[21]
No search results found

No results! Unfortunately we don't have any near exact matches for "flesh-eating bad guy". Because we don't have any more specific information about the exact phrasing in the Elasticsearch data, we'll need to cast our search net wider.

Simple match query

This lexical query performs a standard keyword search for the term "crime" within the "plot" and "genre" fields of our Elasticsearch documents.

[22]
Title: The Godfather
Plot: An organized crime dynasty's aging patriarch transfers control of his clandestine empire to his reluctant son.

Title: Goodfellas
Plot: The story of Henry Hill and his life in the mob, covering his relationship with his wife Karen Hill and his mob partners Jimmy Conway and Tommy DeVito in the Italian-American crime syndicate.

Title: The Silence of the Lambs
Plot: A young F.B.I. cadet must receive the help of an incarcerated and manipulative cannibal killer to help catch another serial killer, a madman who skins his victims.

Title: Pulp Fiction
Plot: The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.

Title: Se7en
Plot: Two detectives, a rookie and a veteran, hunt a serial killer who uses the seven deadly sins as his motives.

Title: The Departed
Plot: An undercover cop and a mole in the police attempt to identify each other while infiltrating an Irish gang in South Boston.

Title: The Usual Suspects
Plot: A sole survivor tells of the twisty events leading up to a horrific gun battle on a boat, which began when five criminals met at a seemingly random police lineup.

Title: The Dark Knight
Plot: When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.

That's better! At least we've got some results now. We broadened our search criteria to increase the chances of finding relevant results.

But these results aren't very precise in the context of our original query "flesh-eating bad guy". We can see that "The Silence of the Lambs" is returned in the middle of the results set with this generic match query. Let's see if we can use our semantic reranking model to get closer to the searcher's original intent.

Semantic reranker

In the following retriever syntax, we wrap our standard match query retriever in a text_similarity_reranker. This allows us to leverage the Elastic rerank model to rerank the results based on the phrase "flesh-eating bad guy".

[ ]
Title: The Silence of the Lambs
Plot: A young F.B.I. cadet must receive the help of an incarcerated and manipulative cannibal killer to help catch another serial killer, a madman who skins his victims.

Title: Pulp Fiction
Plot: The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.

Title: Se7en
Plot: Two detectives, a rookie and a veteran, hunt a serial killer who uses the seven deadly sins as his motives.

Title: Goodfellas
Plot: The story of Henry Hill and his life in the mob, covering his relationship with his wife Karen Hill and his mob partners Jimmy Conway and Tommy DeVito in the Italian-American crime syndicate.

Title: The Dark Knight
Plot: When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.

Title: The Godfather
Plot: An organized crime dynasty's aging patriarch transfers control of his clandestine empire to his reluctant son.

Title: The Departed
Plot: An undercover cop and a mole in the police attempt to identify each other while infiltrating an Irish gang in South Boston.

Title: The Usual Suspects
Plot: A sole survivor tells of the twisty events leading up to a horrific gun battle on a boat, which began when five criminals met at a seemingly random police lineup.

Success! "The Silence of the Lambs" is our top result. Semantic reranking helped us find the most relevant result by parsing a natural language query, overcoming the limitations of lexical search that relies on keyword matching.

Semantic reranking enables semantic search in a few steps, without the need for generating and storing embeddings. This a great tool for testing and building hybrid search systems in Elasticsearch.

Note Starting with Elasticsearch version 8.18, The inference_id field is optional. If not specified, it defaults to .rerank-v1-elasticsearch. If you are using an earlier version or prefer to manage your own endpoint, you can set up a custom rerank inference endpoint using the create inference API.

Learn more