Read one level up: https://skills.qdrant.tech/md/documentation/edge

> Explore Qdrant's agent skills catalog at https://skills.qdrant.tech/
> Search the documentation at https://skills.qdrant.tech/md/documentation?q=your+query+here
> Use this file for an index of the main pages: https://qdrant.tech/llms.txt
> Content inside `<only-applies-to>` tags applies only to the named products, or only as of the named version. All other content applies to every Qdrant product. If you know which product the user is on, ignore tagged content for other products.


# BM25 with Qdrant Edge

[BM25](https://en.wikipedia.org/wiki/Okapi_BM25) (Best Matching 25) is a popular ranking algorithm for keyword search. Qdrant Edge computes BM25 scores from a [full-text index](https://skills.qdrant.tech/md/documentation/manage-data/indexing?s=full-text-index) on a payload field, so you can run keyword search on-device without an embedding model or an internet connection. The index holds everything BM25 needs, including the length of each document, so Qdrant Edge derives all statistics from your data.

<aside role="status">Qdrant Edge also includes a BM25 embedder that generates sparse vectors, compatible with sparse vector BM25 on Qdrant Server. In Python, use the <code>Bm25</code> class. In Rust, use <code>EdgeBm25</code> from the <code>qdrant_edge::bm25_embed</code> module. Refer to <a href="/documentation/search/text-search/sparse-vector-search/#bm25">Sparse Vector Search</a>.</aside>

## Create a Full-Text Index with Scoring

An Edge Shard requires at least one vector. This example configures a small dense vector, which you can also use for [hybrid search](https://skills.qdrant.tech/md/documentation/search/text-search/hybrid-search):

```python
from qdrant_edge import Distance, EdgeConfig, EdgeShard, EdgeVectorParams

config = EdgeConfig(
    vectors={"embedding": EdgeVectorParams(size=4, distance=Distance.Cosine)},
)

shard = EdgeShard.create(SHARD_DIRECTORY, config)
```

> This snippet is also available in Rust. See the [full snippet](https://skills.qdrant.tech/md/documentation/snippets/edge/bm25/configure-bm25-shard).


To rank points with BM25, create a full-text index on the field you want to search and enable `scoring`. For example, to search the titles of books:

```python
from qdrant_edge import (
    Language,
    SnowballLanguage,
    SnowballParams,
    TextIndexParams,
    UpdateOperation,
)

shard.update(UpdateOperation.create_field_index(
    "title",
    TextIndexParams(
        scoring=True,
        stemmer=SnowballParams(language=SnowballLanguage.English),
        stopwords=Language.English,
    ),
))
```

> This snippet is also available in Rust. See the [full snippet](https://skills.qdrant.tech/md/documentation/snippets/edge/bm25/create-bm25-index).


This index also applies English stemming and stopword removal, which improves BM25 results for English text. Refer to [Text Processing](https://skills.qdrant.tech/md/documentation/search/text-search/full-text-search?s=text-processing).

## Add Points

Store the text in the indexed payload field:

```python
from qdrant_edge import Point, UpdateOperation

shard.update(UpdateOperation.upsert_points([
    Point(1, {"embedding": [0.05, 0.61, 0.76, 0.74]}, {"title": "The Time Machine"}),
    Point(2, {"embedding": [0.19, 0.81, 0.75, 0.11]}, {"title": "Time Travel in Einstein's Universe"}),
    Point(3, {"embedding": [0.36, 0.55, 0.47, 0.94]}, {"title": "The Travels of Marco Polo"}),
]))
```

> This snippet is also available in Rust. See the [full snippet](https://skills.qdrant.tech/md/documentation/snippets/edge/bm25/upsert-points).


## Query a Text Field

To run a BM25 query, use a `TextQuery` with the name of the indexed payload field and the query text. The following example searches for books with "time travel" in the title:

```python
from qdrant_edge import QueryRequest, TextQuery

results = shard.query(QueryRequest(
    query=TextQuery("title", "time travel"),
    limit=3,
    with_payload=True,
))
```

> This snippet is also available in Rust. See the [full snippet](https://skills.qdrant.tech/md/documentation/snippets/edge/bm25/query-bm25).


A point matches the query if its text contains at least one of the query terms. Points that contain none of the terms are not returned. To require or exclude specific terms, add a [full-text filter](https://skills.qdrant.tech/md/documentation/search/filtering?s=full-text-match) to the query.

A `TextQuery` can also be used in a prefetch, for example, to combine BM25 with dense vector search in [hybrid search](https://skills.qdrant.tech/md/documentation/search/text-search/hybrid-search).

### Configure BM25 Parameters

The BM25 [ranking function](https://en.wikipedia.org/wiki/Okapi_BM25#The_ranking_function) has two parameters that you can set per query:

- `k`. Controls term frequency saturation. Higher values increase the influence of term frequency. Must be 0 or greater. Defaults to 1.2.
- `b`. Controls document length normalization. Ranges from 0 (no normalization) to 1 (full normalization). A higher value means longer documents have less impact. Defaults to 0.75.

Because these parameters are set per query, you can change them without rebuilding the index:

```python
from qdrant_edge import Bm25Params, QueryRequest, TextQuery

results = shard.query(QueryRequest(
    query=TextQuery("title", "time travel", scoring=Bm25Params(k=1.5, b=0.5)),
    limit=3,
    with_payload=True,
))
```

> This snippet is also available in Rust. See the [full snippet](https://skills.qdrant.tech/md/documentation/snippets/edge/bm25/query-bm25-params).
