# rank_bm25

> A two-line Python search engine: BM25 and its variants for keyword document ranking

- Category: Retrieval & Knowledge (RAG) / Rerank, Search & Hybrid
- Language: Python
- Repository: https://github.com/dorianbrown/rank_bm25
- Canonical page: https://ai-tldr.dev/tools/rank-bm25/

## In short

rank_bm25 is a small Python library that scores documents against a query using the BM25 family of ranking algorithms. It includes Okapi BM25, BM25L, and BM25+, all taken from a published comparison paper, and exposes them through a simple class-based API. It sits in the Rerank, Search & Hybrid category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python.

## Overview

rank_bm25 is a small Python library that scores documents against a query using the BM25 family of ranking algorithms. It includes Okapi BM25, BM25L, and BM25+, all taken from a published comparison paper, and exposes them through a simple class-based API.

It is meant for developers who need keyword-based relevance ranking without standing up a full search server. You index a tokenized corpus once, then ask for either per-document scores or the top-n matches for a query. The library does no text preprocessing of its own, so lowercasing, stopword removal, and stemming are left to you.

Within RAG and retrieval work, rank_bm25 fits the hybrid-search and reranking slot: it gives you a fast lexical baseline you can use on its own or combine with dense vector retrieval. For large-scale production use, the README points to retriv as a more performant alternative.

## What it does

- Implements Okapi BM25, BM25L, and BM25+ ranking algorithms
- Simple API: build an index from a tokenized corpus, then query it
- get_scores returns a relevance score for every document
- get_top_n returns the n most relevant documents directly
- Pure keyword/lexical ranking with no external search server needed
- Algorithms drawn from a published BM25 comparison paper

## Getting started

Install from PyPI, then index a tokenized corpus and run a query in just a few lines.

1. Install

   Install the package from PyPI with pip.

```bash
pip install rank_bm25
```

2. Build an index

   Create a BM25 instance from a list of tokenized documents. The library expects a list of lists of string tokens and does no preprocessing itself.

```python
from rank_bm25 import BM25Okapi

corpus = [
    "Hello there good man!",
    "It is quite windy in London",
    "How is the weather today?"
]

tokenized_corpus = [doc.split(" ") for doc in corpus]

bm25 = BM25Okapi(tokenized_corpus)
```

3. Query the index

   Tokenize the query the same way, then get per-document scores or fetch the top-n matching documents.

```python
query = "windy London"
tokenized_query = query.split(" ")

doc_scores = bm25.get_scores(tokenized_query)
# array([0.        , 0.93729472, 0.        ])

bm25.get_top_n(tokenized_query, corpus, n=1)
# ['It is quite windy in London']
```

## When to use it

- Adding a lightweight keyword search engine to a Python app without running a search server
- Providing the lexical half of a hybrid retrieval setup alongside dense vector search in a RAG pipeline
- Reranking or scoring candidate documents by query relevance
- Prototyping and benchmarking BM25, BM25L, and BM25+ ranking on a corpus
