Skip to main content

Classes

LogicalExpr

Internal Instances of the LogicalExpr class are used to represent logical expressions in TopK. Usually created using logical constructors such as field(), literal(), etc. Methods

is_null()

Check if the expression is null. Returns LogicalExpr

is_not_null()

Check if the expression is not null. Returns LogicalExpr

abs()

Compute the absolute value of the expression. Returns LogicalExpr

ln()

Compute the natural logarithm of the expression. Returns LogicalExpr

exp()

Compute the exponential of the expression. Returns LogicalExpr

sqrt()

Compute the square root of the expression. Returns LogicalExpr

square()

Compute the square of the expression. Returns LogicalExpr

eq()

Check if the expression is equal to another expression. Parameters Returns LogicalExpr

ne()

Check if the expression is not equal to another expression. Parameters Returns LogicalExpr

lt()

Check if the expression is less than another expression. Parameters Returns LogicalExpr

lte()

Check if the expression is less than or equal to another expression. Parameters Returns LogicalExpr

gt()

Check if the expression is greater than another expression. Parameters Returns LogicalExpr

gte()

Check if the expression is greater than or equal to another expression. Parameters Returns LogicalExpr

add()

Add another value to the expression. Parameters Returns LogicalExpr

sub()

Subtract another value from the expression. Parameters Returns LogicalExpr

mul()

Multiply the expression by another value. Parameters Returns LogicalExpr

div()

Divide the expression by another value. Parameters Returns LogicalExpr

min()

Compute the minimum of the expression and another value. Parameters Returns LogicalExpr

max()

Compute the maximum of the expression and another value. Parameters Returns LogicalExpr

and_()

Compute the logical AND of the expression and another expression. Parameters Returns LogicalExpr

or_()

Compute the logical OR of the expression and another expression. Parameters Returns LogicalExpr

starts_with()

Check if the expression starts with the provided string expression. Can be applied on a string field or a list of strings field.
Parameters Returns LogicalExpr

contains()

Check if the expression contains another value. Parameters Returns LogicalExpr

in_()

Check if the expression is in the provided iterable expression. Parameters Returns LogicalExpr

match_all()

Check if the expression matches all terms against the field with keyword index. Parameters Returns LogicalExpr

match_any()

Check if the expression matches any term against the field with keyword index. Parameters Returns LogicalExpr

coalesce()

Coalesce nulls in the expression with another value. Parameters Returns LogicalExpr

choose()

Choose between two values based on the expression. Parameters Returns LogicalExpr

boost()

Multiply the scoring expression by the provided boost value if the condition is true. Parameters Returns LogicalExpr

regexp_match()

Check if the expression matches the provided regexp pattern. Parameters Returns LogicalExpr

FunctionExpr

Internal Instances of the FunctionExpr class are used to represent function expressions in TopK. Usually created using function constructors such as fn.vector_distance(), fn.semantic_similarity() or fn.bm25_score().

AggregateExpr

Internal Instances of the AggregateExpr class are used to represent aggregate expressions in TopK. Usually created using aggregate constructors such as agg.count(), agg.sum(), agg.min(), agg.max() or agg.avg().

TextExpr

Internal Instances of the TextExpr class are used to represent text expressions in TopK.

Query

Methods

select()

Adds a select stage to the query. Returns Query

filter()

Adds a filter stage to the query. Parameters Returns Query

sort()

Adds a sort stage to the query. Parameters Returns Query

limit()

Adds a limit stage to the query. Parameters Returns Query

offset()

Adds an offset stage to the query. Parameters Returns Query

count()

Adds a count stage to the query. Returns Query

group_by()

Adds a group-by stage to the query. Groups documents by one or more key expressions and computes aggregations for each group. Parameters Returns Query

topk()

Deprecated — Use .sort(expr, asc).limit(k) instead.
Adds a top-k stage to the query. Parameters Returns Query

fn

The query.fn submodule exposes functions for creating function expressions such as fn.vector_distance(), fn.semantic_similarity() or fn.bm25_score(). Methods

vector_distance()

Calculate the vector distance between a field and a query vector.
Parameters Returns FunctionExpr

semantic_similarity()

Calculate the semantic similarity between a field and a query string.
Parameters Returns FunctionExpr

bm25_score()

Calculate the BM25 score for a keyword search. Optional parameters: b (0-1), k1 (>=0) to override BM25 scoring behavior.
Parameters Returns FunctionExpr

multi_vector_distance()

Calculate the multi-vector distance between a field and a query matrix. The query matrix can be a list of lists (defaults to f32), a numpy array (type inferred from dtype), or a Matrix instance. To specify a different matrix type, use matrix() with value_type or a 2-D numpy array with the corresponding dtype. The optional candidates parameter limits the number of candidate vectors considered during search.
Parameters Returns FunctionExpr

agg

The query.agg submodule exposes functions for creating aggregate expressions used in group_by()’s aggs argument, such as agg.count(), agg.sum(), agg.min(), agg.max() or agg.avg(). Methods

count()

Count the number of documents in the group. If field is provided, counts only the documents where that field is non-null. If omitted, counts every document in the group.
Parameters Returns AggregateExpr

sum()

Sum the values of field across the group. Parameters Returns AggregateExpr

min()

Find the minimum value of field across the group. Parameters Returns AggregateExpr

max()

Find the maximum value of field across the group. Parameters Returns AggregateExpr

avg()

Calculate the average value of field across the group. Parameters Returns AggregateExpr

Functions

field()

Select a field from the document. Parameters Returns LogicalExpr

select()

Creates a new query with a select stage.
Returns Query

filter()

Creates a new query with a filter stage.
Parameters Returns Query

group_by()

Creates a new query with a group-by stage. Groups documents by one or more key expressions and computes aggregations for each group.
Parameters Returns Query

literal()

Create a literal expression. Parameters Returns LogicalExpr

match()

Perform a BM25 keyword search using TopK’s built-in tokenizer. The token argument is a raw query string — TopK tokenizes it automatically, removes stop words, and scores documents using BM25. Pass the full query string directly; tokenization and stop-word removal happen server-side.
  • field: restrict matching to a specific keyword-indexed field (default: all).
  • weight: scale the BM25 contribution of this expression (default: 1.0).
  • all: if True, require all tokens to match (AND); default is any-token (OR).
Parameters Returns TextExpr

match_tokens()

Filters documents that match the provided tokens with optional per-token weights. Each token can be a string (with the default weight of 1.0) or a (token, weight) tuple. When field is provided, matches only against that field (must have a keyword index). When field is None (default), matches against all keyword-indexed fields. When all is False (default), matches documents containing any of the tokens (OR). When all is True, matches only documents containing all tokens (AND). Parameters Returns TextExpr

not_()

Negate a logical expression.
Parameters Returns LogicalExpr

abs()

Compute the absolute value of a logical expression.
Parameters Returns LogicalExpr

all()

Create a logical AND expression.
Parameters Returns LogicalExpr

any()

Create a logical OR expression.
Parameters Returns LogicalExpr

min()

Create a logical MIN expression.
Parameters Returns LogicalExpr

max()

Create a logical MAX expression.
Parameters Returns LogicalExpr

Type Aliases

FlexibleExpr

Type str | int | float | bool | None | LogicalExpr

Numeric

Type int | float | LogicalExpr

Ordered

Type int | float | str | LogicalExpr

Boolish

Type bool | LogicalExpr

Stringy

Type str | LogicalExpr

StringyWithList

Type str | list[str] | LogicalExpr

Iterable

Type str | list[int] | list[float] | list[str] | topk_sdk.data.List | LogicalExpr