AWS Builder Center
OpenSearch Hybrid Search: Combine Keyword and Semantic Results

OpenSearch Hybrid Search: Combine Keyword and Semantic Results

In this article, you merge keyword search and semantic search into one ranked list using a hybrid query and a normalization search pipeline.

Series: Search on OpenSearch (8 articles)

  1. …
  2. 8
    OpenSearch Hybrid Search: Combine Keyword and Semantic Results This article
OpenSearch Specialist Solutions Architect at AWS
Keyword search is precise: it matches the exact words a reader types. Semantic search is forgiving: it matches meaning. Each misses cases the other catches. Hybrid search runs both and merges the results into a single ranking. This article shows the two pieces that make it work: the  hybrid  query and a search pipeline with a normalization processor.
This is Part 8 of a beginner series on search with  Amazon OpenSearch Service . It builds on the semantically enriched  books  collection from Part 7.

Why you cannot just add the scores

A keyword (BM25) score and a semantic score are on different scales. BM25 scores are unbounded and depend on term statistics; semantic scores come from a different calculation entirely. If you naively add them, whichever score happens to be numerically larger dominates, and the ranking is meaningless. The fix is to normalize each set of scores onto a common scale first, then combine them. That two-step process, normalize then combine, is what the normalization processor does.

Step 1: Create a search pipeline with a normalization processor

A search pipeline is a sequence of processors that transforms a search request or its results. For hybrid search, you create one containing a  normalization-processor . It has two techniques: a normalization technique that rescales each query's scores, and a combination technique that merges them, with optional per-query weights.
PUT _search/pipeline/hybrid-pipeline
{
"description": "Normalize and combine keyword and semantic scores",
"phase_results_processors": [
{
"normalization-processor": {
"normalization": { "technique": "min_max" },
"combination": {
"technique": "arithmetic_mean",
"parameters": { "weights": [0.4, 0.6] }
}
}
}
]
}
Here  min_max  rescales each query's scores into a 0-to-1 range,  arithmetic_mean  averages them, and the weights give the semantic query slightly more influence than the keyword query. The weights array lines up with the order of the subqueries in your hybrid query.

Step 2: Run a hybrid query

The  hybrid  query holds a list of subqueries. Each runs independently, and the pipeline normalizes and combines their scores. Here one subquery is a keyword  match  on the title, and the other is a  match  on the semantically enriched  description  field from Part 7.
GET books/_search?search_pipeline=hybrid-pipeline
{
"query": {
"hybrid": {
"queries": [
{ "match": { "title": "wizard school" } },
{ "match": { "description": "a young magician learns their craft" } }
]
}
}
}
The first subquery finds books with those literal words in the title. The second, running against the enriched field, matches books whose descriptions mean something similar even without the same words. The pipeline puts both score sets on the same scale and merges them, so a book that is a strong keyword match and a book that is a strong semantic match can both rank near the top.
You attach the pipeline per request with the  search_pipeline  query parameter, as shown, or set it as the index default so every search uses it.

Tuning the balance

Two levers control the blend:
  • Weights in the combination technique shift influence between keyword and semantic. Raise the keyword weight when exact wording matters (part numbers, names); raise the semantic weight when intent matters more than phrasing.
  • Combination technique changes how scores merge.  arithmetic_mean  is a sound default.  harmonic_mean  and  geometric_mean  reward documents that score reasonably on both queries rather than spiking on one.
Change one lever at a time and compare the top results, the same disciplined approach from the relevance tuning in Part 5. Your mileage will vary by dataset.

What to remember

  • Normalize before you combine. Raw keyword and semantic scores are not comparable.
  • The  normalization-processor  lives in a search pipeline and does both steps.
  • The  hybrid  query lists the subqueries; the weights array matches their order.
  • Start with  min_max  normalization and  arithmetic_mean  combination, then tune weights.

Series: Search on OpenSearch (8 articles)

  1. …
  2. 8
    OpenSearch Hybrid Search: Combine Keyword and Semantic Results 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