█

AI/TLDR

Buy me a coffee
Buy me a coffee

rank_bm25

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

Rerank, Search & HybridOpen source
Language
Python
$pip install rank_bm25
IN SHORT

What is rank_bm25?

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.

Install

Install the package from PyPI with pip.

bashbash
pip install rank_bm25

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.

pythonpython
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)

Query the index

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

pythonpython
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']

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

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

How rank_bm25 compares

rank_bm25 alongside other open-source rerank, search & hybrid tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
Elasticsearch★ 78.9kDistributed search and analytics engine with a built-in vector database for dense/sparse embeddings and hybrid keyword-plus-semantic retrieval.
Meilisearch Cloud★ 60.1kManaged cloud for the Meilisearch engine, combining fast full-text search with hybrid, semantic, and multimodal vector search.
Typesense Cloud★ 27.2kManaged hosting for the Typesense search engine, offering typo-tolerant keyword search plus vector and semantic search via a simple API.
Tantivy★ 16.5kA fast full-text search engine library in Rust that provides BM25 keyword search for the lexical half of hybrid retrieval.
FlagEmbedding★ 12.4kBAAI's retrieval toolkit that provides the BGE embedding and cross-encoder reranker models used widely in RAG pipelines.
Vespa★ 7.2kA search and serving engine that natively combines vector, keyword (BM25), and structured search with built-in ranking for large-scale retrieval.
DigUp★ 6.6kAn on-device Mac app that searches photos, PDFs, recordings, videos and code by what is in them, combining EmbeddingGemma 2 vectors run on llama.cpp with a keyword index.
rank_bm25★ 1.4kA two-line Python search engine: BM25 and its variants for keyword document ranking