liveBook · Manning

BM25

As explained in Building Reliable AI Systems

BM25 is a keyword-based ranking algorithm that scores documents according to query-term frequency while accounting for document length and term saturation.

BM25 ranks documents by query-term frequency while penalizing common words. Unlike embedding-based retrieval, it matches exact terms: a search for “iPhone 14 Pro troubleshooting” can retrieve documents containing those words. Its deterministic, fast, reproducible behavior makes it a useful retrieval baseline, but exact matching can fall short when queries need semantic search. section 3.6.6

In a telecom support chatbot, BM25 retrieval can work alongside metadata filters for plan, device, topic, or year. For a question about international roaming on an iPhone 14 Pro with the Mobile Plus plan, extracted query details can narrow the retrieval search. Deterministic results avoid model inference and embedding drift, but reproducibility alone does not guarantee relevance. section 3.6.6

from haystack import Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever

retrieval_pipeline = Pipeline()
metadata_extractor = QueryMetadataExtractor()
retriever = InMemoryBM25Retriever(document_store=document_store)   #1


retrieval_pipeline.add_component(instance=metadata_extractor,
name="metadata_extractor")   #2
retrieval_pipeline.add_component(instance=retriever, 
name="retriever")   #3
retrieval_pipeline.connect("metadata_extractor.filters", 
"retriever.filters")   #4

query = "How do I set up international roaming on my iPhone 14 Pro? 
I'm on the Mobile Plus plan."
metadata_fields = ["topic", "plan_name", "device_type", "year"]

retrieval_pipeline.run(
    data={
        "metadata_extractor": {"query": query, "metadata_fields":
            metadata_fields},
        "retriever": {"query": query}
    }
)   #5

See a Haystack pipeline connecting extracted metadata filters to an InMemoryBM25Retriever. section 3.6.6

How Building Reliable AI Systems covers it

How deep it goes
Explains term-frequency ranking and common-word penalties qualitatively, without deriving a scoring formula.
Examples
Uses an iPhone troubleshooting search and a telecom roaming query with device and plan details.
What you can do after
Apply BM25 retrieval with Haystack's InMemoryBM25Retriever and connect extracted metadata filters to the retriever.
How it connects
Contrasts BM25 with embedding-based retrieval and semantic search, and connects it to metadata filtering in RAG.

Also explained in

Compare how 5 books explain BM25 →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Reliable AI Systems. The section links point to the parts of the book it draws on.