TopK supports keyword search with the BM25 ranking function.

To perform a keyword search on your documents, use the match() function.

In the following example we’ll:

  1. Create a collection with a title and description text fields and add a keyword index to both of them.
  2. Run a keyword search on the title and description fields using the match() function.

Define a collection schema

Let’s create a collection with a title and description text fields and add a keyword index to both of them:

from topk_sdk.schema import keyword_index, text

client.collections().create(
    "books",
    schema={
        "title": text().index(keyword_index()),
        "description": text().index(keyword_index()),
    },
)

Now, let’s run a keyword search on the title and description fields using the match() function. We’ll query the collection to match the term "great" in the title field or the term "novel" in any of the keyword-indexed text fields:

from topk_sdk.query import select, fn, match

docs = client.collection("books").query(
    select(
        "title",
        "description",
        # Score documents using BM25 algorithm
        text_score=fn.bm25_score()
    )
    # Filter documents that have the `great` keyword in the `title` field
    # or the `novel` in any of the text-indexed fields.
    .filter(match("great", field="title").or(match("novel")))
    # Return top 10 documents with the highest text score
    .topk(field("text_score"), 10)
)

The match() function will by default execute against all fields with a keyword index.

TopK provides a powerful keyword search API allowing you to customize your search queries. Read more about keyword search here.