Skip to main content

Reranking

LICENSE Weaviate on Stackoverflow badge Weaviate issues on GitHub badge Weaviate version badge Weaviate total Docker pulls badge Go Report Card

Overview

This page shows you how to rerank a result set returned by a search.

A reranker algorithm is applied after a search on the retrieved results. Thus a reranker algorithm can apply a different set of criteria, or simply use a different algorithm.

For example, a more computationally expensive (and accurate) algorithm can be used for reranking, as it is only applied to a subset of the data.

Requirements

To rerank search results, you'll need set the class with an enabled reranker module.

You can rerank results using:

  • The same query as the initial search, or
  • A different reranking query.
How do I set the reranker module in the target class?

If there is only one reranker module enabled, you don't need to do anything. The reranker module will be used by default.


Where multiple reranker modules are enabled, you must specify the reranker module to be used in the moduleConfig section of the schema. For example, this configures the Article class to use the reranker-cohere module:

{
"classes": [
{
"class": "Article",
...,
"moduleConfig": {
"reranker-cohere": {}, // This will configure the 'Article' class to use the 'reranker-cohere' module
}
}
]
}

You may be able to set additional module parameters here. Please refer to the "Schema configuration" section in the relevant module page.

Reranking vector search results

Using the JeopardyQuestions dataset, let's say we want to find Q&As about flying, and further sort towards the top those about floating. We can start with a nearText search for flying, limited to 10 results:

response = (
client.query
.get('JeopardyQuestion', ['question', 'answer'])
.with_near_text({
'concepts': ['flying']
})
.with_additional('distance')
.with_limit(10)
.do()
)

print(json.dumps(response, indent=2))
Example response

The response should look like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"distance": 0.16765535
},
"answer": "on their stomachs",
"question": "W. & O. Wright felt passengers wouldn't mind flying in this position they 1st flew in themselves"
},
{
"_additional": {
"distance": 0.17469394
},
"answer": "flying the mail",
"question": "In 1926 Lindbergh had to parachute out of planes 4 times while employed to do this"
},
{
"_additional": {
"distance": 0.17639679
},
"answer": "a hot air balloon",
"question": "In 1783 Benjamin Franklin saw the first piloted flight of this type of transport while in Paris"
},
{
"_additional": {
"distance": 0.17872101
},
"answer": "a falcon",
"question": "The fastest flying animal is the peregrine species of this bird of prey"
},
{
"_additional": {
"distance": 0.18135852
},
"answer": "Pterodactyl",
"question": "The name of this prehistoric reptile, the largest known flying animal, means \"wing finger\""
},
{
"_additional": {
"distance": 0.18168795
},
"answer": "hot air balloons",
"question": "These in the skies of Albuquerque on October 3, 1999 were a fine example of Charles' Law in action"
},
{
"_additional": {
"distance": 0.1847046
},
"answer": "Lizards",
"question": "In the East Indies certain species of this reptile are called flying dragons because they can glide from tree to tree"
},
{
"_additional": {
"distance": 0.18559676
},
"answer": "a limp blimp",
"question": "An uninflated airship"
},
{
"_additional": {
"distance": 0.18577725
},
"answer": "hot air balloons",
"question": "During the Cold War, 2 different families escaped over the Berlin Wall using these lighter-than-air vehicles"
},
{
"_additional": {
"distance": 0.1866476
},
"answer": "a dirigible",
"question": "In 1926 Roald Amundsen flew over the North Pole in the Norge, this type of craft"
}
]
}
}
}

We can see that results pertaining to floating aircraft (balloons/blimps/dirigibles) are mixed in with other results (animals, mail). To sort floating results to the top, we can apply the rerank operator:

response = (
client.query
.get('JeopardyQuestion', ['question', 'answer'])
.with_near_text({
'concepts': ['flying']
})
.with_additional('rerank(property: "answer" query: "floating") { score }')
.with_limit(10)
.do()
)

print(json.dumps(response, indent=2))
Example response

The response should look like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"distance": 0.16765535,
"rerank": [
{
"score": 0.357119
}
]
},
"answer": "on their stomachs",
"question": "W. & O. Wright felt passengers wouldn't mind flying in this position they 1st flew in themselves"
},
{
"_additional": {
"distance": 0.17639679,
"rerank": [
{
"score": 0.14010079
}
]
},
"answer": "a hot air balloon",
"question": "In 1783 Benjamin Franklin saw the first piloted flight of this type of transport while in Paris"
},
{
"_additional": {
"distance": 0.1866476,
"rerank": [
{
"score": 0.10631887
}
]
},
"answer": "a dirigible",
"question": "In 1926 Roald Amundsen flew over the North Pole in the Norge, this type of craft"
},
{
"_additional": {
"distance": 0.18168795,
"rerank": [
{
"score": 0.096705794
}
]
},
"answer": "hot air balloons",
"question": "These in the skies of Albuquerque on October 3, 1999 were a fine example of Charles' Law in action"
},
{
"_additional": {
"distance": 0.18577725,
"rerank": [
{
"score": 0.096705794
}
]
},
"answer": "hot air balloons",
"question": "During the Cold War, 2 different families escaped over the Berlin Wall using these lighter-than-air vehicles"
},
{
"_additional": {
"distance": 0.18559676,
"rerank": [
{
"score": 0.037750274
}
]
},
"answer": "a limp blimp",
"question": "An uninflated airship"
},
{
"_additional": {
"distance": 0.17469394,
"rerank": [
{
"score": 0.036977556
}
]
},
"answer": "flying the mail",
"question": "In 1926 Lindbergh had to parachute out of planes 4 times while employed to do this"
},
{
"_additional": {
"distance": 0.1847046,
"rerank": [
{
"score": 0.014172366
}
]
},
"answer": "Lizards",
"question": "In the East Indies certain species of this reptile are called flying dragons because they can glide from tree to tree"
},
{
"_additional": {
"distance": 0.18135852,
"rerank": [
{
"score": 0.0025809042
}
]
},
"answer": "Pterodactyl",
"question": "The name of this prehistoric reptile, the largest known flying animal, means \"wing finger\""
},
{
"_additional": {
"distance": 0.17872101,
"rerank": [
{
"score": 0.0018386653
}
]
},
"answer": "a falcon",
"question": "The fastest flying animal is the peregrine species of this bird of prey"
}
]
}
}
}

We can see in the reranked result set, that answers are sorted descending by the _additional.rerank[0].score field, and those involving balloons/dirigibles/blimps are sorted towards the top.

Reranking bm25 search results

The example below uses rerank in a bm25 query to sort the top results for the query "paper" towards "publication" to disambiguate from those related to other meanings, such as the material "paper".

response = (
client.query
.get('JeopardyQuestion', ['question', 'answer'])
.with_bm25(
query='paper'
)
.with_additional('rerank(property: "question" query: "publication") { score }')
.with_limit(10)
.do()
)

print(json.dumps(response, indent=2))
Example response

The response should look like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"rerank": [
{
"score": 0.64957863
}
],
"score": "1.917839"
},
"answer": "Albert Einstein",
"question": "His 1905 paper \"On the Electrodynamics of Moving Bodies\" contained his special Theory of Relativity"
},
{
"_additional": {
"rerank": [
{
"score": 0.42018318
}
],
"score": "1.8317645"
},
"answer": "Mark Twain",
"question": "In 1852 his story \"The Dandy Frightening the Squatter\" appeared in The Carpet-Bag, a humorous paper"
},
{
"_additional": {
"rerank": [
{
"score": 0.38139236
}
],
"score": "1.680885"
},
"answer": "Louis Pasteur",
"question": "In 1857 this French chemist's theory of fermentation was first presented in a paper \"on Lactic Fermentation\""
},
{
"_additional": {
"rerank": [
{
"score": 0.14829372
}
],
"score": "1.6143973"
},
"answer": "Benito Mussolini",
"question": "After being expelled as editor of the Socialist \"Avanti\" in 1914, he founded his own fascist paper"
},
{
"_additional": {
"rerank": [
{
"score": 0.13974822
}
],
"score": "1.917839"
},
"answer": "Bookworm",
"question": "It can be a voracious reader, or a beetle larva that feeds on paper"
},
{
"_additional": {
"rerank": [
{
"score": 0.030214587
}
],
"score": "1.8317645"
},
"answer": "hot air balloon",
"question": "In 1783 Joseph & Jacques Montgolfier, sons of a French paper bag maker, invented this"
},
{
"_additional": {
"rerank": [
{
"score": 0.02470387
}
],
"score": "1.7530843"
},
"answer": "a balloon",
"question": "The Montgolfier brothers were papermakers by profession & used paper in their early ones of these"
},
{
"_additional": {
"rerank": [
{
"score": 0.018797074
}
],
"score": "1.6143973"
},
"answer": "New York Herald",
"question": "This paper that had sent Stanley to find Livingstone merged with the New York Tribune in 1924"
},
{
"_additional": {
"rerank": [
{
"score": 0.014672035
}
],
"score": "2.2325633"
},
"answer": "Scott",
"question": "In 1907 this Phildelphia-based company introduced the paper towel"
},
{
"_additional": {
"rerank": [
{
"score": 0.011915022
}
],
"score": "1.917839"
},
"answer": "crepe",
"question": "The flowers on this type of myrtle tree resemble the crinkly paper of the same name"
}
]
}
}
}

More Resources

For additional information, try these sources.

  1. Frequently Asked Questions
  2. Weaviate Community Forum
  3. Knowledge base of old issues
  4. Stackoverflow
  5. Weaviate slack channel