When we search across multiple indices, we may want to have a document found in one index take precedence over the same document found in another index. That is, we may want to boost certain indices over others when performing a search across multiple indices. For that, we can attach an indices_boost
object at the same level as the query object. We can input multiple indices with the appropriate boost rating set on this indices_boost
object.
To demonstrate this concept, I’ve created two new indices (index_top
and index_new
) and indexed The Shawshank Redemption movie in these two new indices(see the code on my GitHub page). Now that we have the same movie across three indices, let’s create the query with a requirement of enhancing the score of the document obtained from movies_top
so that it’s the topmost result.
GET movies*/_search
{
"indices_boost": [
{ "movies": 0.1},
{ "movies_new": 0},
{ "movies_top": 2.0}
],
"query": {
"match": {
"title": "Redemption"
}
}
}
As the query indicates, we bumped up the score by double if the document is found in the Query DSL’s movies_top
, and we decreased it to 0.1 for the documents fetched from the movies
index. Finally, we set indices_boost
to 0
for the movies_new
documents. If the document’s original score is 0.2876821 in movies_top
, for example, the new score will be 0.5753642 (2 * 0.2876821), whereas the other documents’ scores will be calculated as per the setting in the indices_boost
object.
These short articles are condensed excerpts taken from my book Elasticsearch in Action, Second Edition. The code is available in my GitHub repository.