License YOLO
Back to Ultralytics Glossary

Hybrid Search

Hybrid search runs keyword (BM25) and vector retrieval for the same query and fuses the two ranked lists, often with Reciprocal Rank Fusion, into one.

Hybrid search runs keyword retrieval and vector search for the same query and merges the two ranked lists into one. Keyword, or lexical, search scores exact term matches with a formula such as BM25 and is called sparse retrieval, because it represents each document as a sparse vector of term weights over the vocabulary. Vector search compares dense embeddings that capture meaning. BM25 finds the exact error code, product SKU, or person's name that an embedding model may blur. Embeddings find the paraphrases and synonyms that share no words with the query. Combining them is a common retrieval setup for retrieval-augmented generation (RAG) and enterprise search.

Why Combine Sparse and Dense Retrieval#

Dense retrievers learn from particular training data, and their advantage shrinks on unfamiliar domains. The BEIR benchmark (2021) tested retrieval models zero-shot on 18 datasets and found BM25 a robust baseline that many dense models failed to beat outside their training domain, while rerankers and late-interaction models such as ColBERT scored best at a higher computational cost. Running BM25 next to a dense retriever keeps the keyword strengths without giving up semantic search. Learned sparse models such as SPLADE blur the line from the other side, using a transformer to assign weights to vocabulary terms, including related terms that a document never contains.

How Results Are Fused#

BM25 scores are unbounded, while cosine similarities fall between -1 and 1, so the two lists cannot be added directly. Two fusion methods are common:

  • Reciprocal Rank Fusion (RRF): Gives each document the sum of 1 / (k + rank) over the lists it appears in, with k = 60 as in the 2009 paper by Cormack, Clarke, and Büttcher. It uses only ranks, so it needs no score normalization or tuning.
  • Weighted score fusion: Normalizes both score sets, for example to the range 0 to 1, and combines them as alpha x dense + (1 - alpha) x sparse, with alpha tuned on validation queries.

A reranker often reorders the fused list before results are shown or passed to a large language model (LLM). The example below runs both legs with scikit-learn TF-IDF keyword scores and Sentence Transformers embeddings, then fuses them with RRF:

import numpy as np
from sentence_transformers import SentenceTransformer
from sklearn.feature_extraction.text import TfidfVectorizer

docs = [
    "Fault E-4012: hydraulic pressure low on press 7",
    "Fault E-4021: hydraulic pressure low on press 2",
    "Order form for E-4012 replacement sensor kits",
    "Low oil pressure makes the press ram sluggish",
    "Weekly cleaning checklist for the paint booth",
]
query = "E-4012 sluggish"

# Sparse leg: TF-IDF keyword scores, which match exact tokens such as fault codes
tfidf = TfidfVectorizer(token_pattern=r"[\w-]+", stop_words="english").fit(docs)
sparse = (tfidf.transform(docs) @ tfidf.transform([query]).T).toarray().ravel()

# Dense leg: cosine similarity of sentence embeddings, which match meaning
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
dense = model.similarity(model.encode(query), model.encode(docs))[0].numpy()

# Reciprocal Rank Fusion: each list adds 1 / (60 + rank) to a document's score
fused = np.zeros(len(docs))
for scores in (sparse, dense):
    fused[np.argsort(-scores)] += 1 / (60 + np.arange(1, len(docs) + 1))
for name, scores in [("sparse", sparse), ("dense", dense), ("hybrid", fused)]:
    print(name, np.argsort(-scores)[:3])

The keyword leg ranks the symptom description (document 3) first because it contains the rare word "sluggish," and the dense leg ranks the E-4012 fault entry (document 0) first, followed by the parts order form. RRF places the E-4012 fault first, because it holds rank 2 in one list and rank 1 in the other, and the symptom description second, giving the two documents that together answer the query.

Real-World Applications#

  • Retail product search: Matches exact model numbers and brand names while also understanding descriptive queries such as "waterproof trail shoes."
  • Technical support and RAG over manuals: Retrieves the page for an exact error code together with passages that describe the same symptom in other words.
  • Legal and compliance research: Finds statute numbers and case names verbatim and also retrieves passages about the same legal concept.

Hybrid Search in Practice#

Elasticsearch, OpenSearch, Weaviate, Qdrant, Pinecone, and Vespa support hybrid queries natively, and PostgreSQL can combine its built-in full-text search with the pgvector extension in one vector database query. Metadata filters, such as a date range or product category, are a separate mechanism: they restrict which documents are eligible before either leg ranks them.

Ultralytics Platform uses a hybrid ordering in its dataset image search: images whose filename, annotated class, or custom metadata matches the search text come first, followed by up to 1,000 more images that look like what the text describes, so a search such as "forklift near a doorway" also returns images of such a scene that carry no matching name, label, or metadata. Keyword retrieval also strengthens text RAG. In Anthropic's tests of contextual retrieval, adding contextual BM25 to contextual embeddings lowered the top-20 retrieval failure rate from 3.7% to 2.9%.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning