AWS Builder Center
Get Better OpenSearch Results with BM25 and Field Boosting

Get Better OpenSearch Results with BM25 and Field Boosting

In this article, you learn how OpenSearch scores results with BM25, how to search several fields at once with multi_match, and how field boosting changes the ranking.

Series: Search on OpenSearch (8 articles)

  1. …
  2. 5
    Get Better OpenSearch Results with BM25 and Field Boosting This article
OpenSearch Specialist Solutions Architect at AWS
Every search in the earlier parts returned a  _score , and results came back ordered by it. This article explains where that score comes from, how to search more than one field at a time, and how to tell OpenSearch that a match in the title matters more than a match in the description.
This is Part 5 of a beginner series on search with  Amazon OpenSearch Service . Continue with the  books  index and Dev Tools from the earlier parts.

Where _score comes from: BM25

By default, OpenSearch ranks full-text matches using BM25 (Best Matching 25), the default similarity algorithm inherited from Apache Lucene. You do not configure anything to get it. BM25 rewards documents where the query terms are frequent, and discounts terms that are common across the whole index, so a rare, on-topic word contributes more to the score than a filler word. The exact number is not meaningful on its own; what matters is the relative order it produces.
You rarely need to change the similarity algorithm. What you do need is control over which fields contribute and how much, and that is what  multi_match  and boosting give you.

Searching several fields: multi_match

The  match  query searches one field. The  multi_match  query searches several at once.
GET books/_search
{
"query": {
"multi_match": {
"query": "wizard",
"fields": ["title", "description"]
}
}
}
By default,  multi_match  runs in  best_fields  mode: it scores each document by its single best-matching field. That is the right default for "find the same words in any of these fields." Other types exist, and two are worth knowing early:
  • best_fields (default) — uses the score of the best-matching field. Good when a document is relevant if any one field matches well.
  • most_fields — combines the scores of all matching fields. Good when the same text is indexed several ways (for example, a plain field and a stemmed field) and you want matches in more of them to rank higher.

Field boosting: the ^ multiplier

A match in the title usually means more than a match in the description. You express that with a boost, written as  ^  followed by a multiplier on the field name. The boost is a score multiplier: values above 1 increase a field's contribution, and values between 0 and 1 decrease it.
GET books/_search
{
"query": {
"multi_match": {
"query": "wizard",
"fields": ["title^3", "description"],
"type": "best_fields"
}
}
}
Here a match in  title  influences the score three times as much as a match in  description . Books with "wizard" in the title rise to the top, while books that only mention wizards in the description rank lower. This single technique, boosting the field that carries the most signal, is the highest-leverage relevance change you can make.

Boosting whole clauses

Boosts are not limited to fields. You can boost an entire clause inside a  bool  query to express preferences, which pairs naturally with the  should  clause from Part 2.
GET books/_search
{
"query": {
"bool": {
"must": [
{ "multi_match": { "query": "wizard", "fields": ["title^3", "description"] } }
],
"should": [
{ "match": { "genre": { "query": "fantasy", "boost": 2 } } }
]
}
}
}
The  must  clause decides which books match. The boosted  should  clause nudges fantasy titles higher without excluding anything.

A relevance-tuning checklist

  • Start with  multi_match  over the fields a reader actually searches, and leave the type at  best_fields .
  • Boost the highest-signal field first, usually the title ( title^3  is a reasonable starting point).
  • Change one boost at a time and compare the top 10 results. Relevance tuning is iterative, and your mileage will vary by dataset.
  • Use  should  clauses with boosts for soft preferences, and  filter  clauses (Part 3) for hard requirements.
  • Do not chase absolute  _score  values. Compare the order of results, not the numbers.

Where this series goes next

You can now control ranking with scoring, multi-field search, and boosts. Everything so far has matched on the literal words in your query. In Part 6, you move beyond keywords into vector search: representing books as embeddings and running a k-nearest-neighbors query to find results by meaning rather than exact words.

Series: Search on OpenSearch (8 articles)

  1. …
  2. 5
    Get Better OpenSearch Results with BM25 and Field Boosting This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article