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

is_null(self) -> LogicalExpr
Check if the expression is null. Returns LogicalExpr

is_not_null()

is_not_null(self) -> LogicalExpr
Check if the expression is not null. Returns LogicalExpr

abs()

abs(self) -> LogicalExpr
Compute the absolute value of the expression. Returns LogicalExpr

ln()

ln(self) -> LogicalExpr
Compute the natural logarithm of the expression. Returns LogicalExpr

exp()

exp(self) -> LogicalExpr
Compute the exponential of the expression. Returns LogicalExpr

sqrt()

sqrt(self) -> LogicalExpr
Compute the square root of the expression. Returns LogicalExpr

square()

square(self) -> LogicalExpr
Compute the square of the expression. Returns LogicalExpr

eq()

eq(self, other: FlexibleExpr) -> LogicalExpr
Check if the expression is equal to another expression. Parameters
ParameterType
otherFlexibleExpr
Returns LogicalExpr

ne()

ne(self, other: FlexibleExpr) -> LogicalExpr
Check if the expression is not equal to another expression. Parameters
ParameterType
otherFlexibleExpr
Returns LogicalExpr

lt()

lt(self, other: Ordered) -> LogicalExpr
Check if the expression is less than another expression. Parameters
ParameterType
otherOrdered
Returns LogicalExpr

lte()

lte(self, other: Ordered) -> LogicalExpr
Check if the expression is less than or equal to another expression. Parameters
ParameterType
otherOrdered
Returns LogicalExpr

gt()

gt(self, other: Ordered) -> LogicalExpr
Check if the expression is greater than another expression. Parameters
ParameterType
otherOrdered
Returns LogicalExpr

gte()

gte(self, other: Ordered) -> LogicalExpr
Check if the expression is greater than or equal to another expression. Parameters
ParameterType
otherOrdered
Returns LogicalExpr

add()

add(self, other: Numeric) -> LogicalExpr
Add another value to the expression. Parameters
ParameterType
otherNumeric
Returns LogicalExpr

sub()

sub(self, other: Numeric) -> LogicalExpr
Subtract another value from the expression. Parameters
ParameterType
otherNumeric
Returns LogicalExpr

mul()

mul(self, other: Numeric) -> LogicalExpr
Multiply the expression by another value. Parameters
ParameterType
otherNumeric
Returns LogicalExpr

div()

div(self, other: Numeric) -> LogicalExpr
Divide the expression by another value. Parameters
ParameterType
otherNumeric
Returns LogicalExpr

min()

min(self, other: Ordered) -> LogicalExpr
Compute the minimum of the expression and another value. Parameters
ParameterType
otherOrdered
Returns LogicalExpr

max()

max(self, other: Ordered) -> LogicalExpr
Compute the maximum of the expression and another value. Parameters
ParameterType
otherOrdered
Returns LogicalExpr

and_()

and_(self, other: Boolish) -> LogicalExpr
Compute the logical AND of the expression and another expression. Parameters
ParameterType
otherBoolish
Returns LogicalExpr

or_()

or_(self, other: Boolish) -> LogicalExpr
Compute the logical OR of the expression and another expression. Parameters
ParameterType
otherBoolish
Returns LogicalExpr

starts_with()

starts_with(self, other: Stringy) -> LogicalExpr
Check if the expression starts with the provided string expression. Parameters
ParameterType
otherStringy
Returns LogicalExpr

contains()

contains(self, other: FlexibleExpr) -> LogicalExpr
Check if the expression contains another value. Parameters
ParameterType
otherFlexibleExpr
Returns LogicalExpr

in_()

in_(self, other: Iterable) -> LogicalExpr
Check if the expression is in the provided iterable expression. Parameters
ParameterType
otherIterable
Returns LogicalExpr

match_all()

match_all(self, other: StringyWithList) -> LogicalExpr
Check if the expression matches all terms against the field with keyword index. Parameters
ParameterType
otherStringyWithList
Returns LogicalExpr

match_any()

match_any(self, other: StringyWithList) -> LogicalExpr
Check if the expression matches any term against the field with keyword index. Parameters
ParameterType
otherStringyWithList
Returns LogicalExpr

coalesce()

coalesce(self, other: Numeric) -> LogicalExpr
Coalesce nulls in the expression with another value. Parameters
ParameterType
otherNumeric
Returns LogicalExpr

choose()

choose(self, x: FlexibleExpr, y: FlexibleExpr) -> LogicalExpr
Choose between two values based on the expression. Parameters
ParameterType
xFlexibleExpr
yFlexibleExpr
Returns LogicalExpr

boost()

boost(self, condition: FlexibleExpr, boost: Numeric) -> LogicalExpr
Multiply the scoring expression by the provided boost value if the condition is true. Parameters
ParameterType
conditionFlexibleExpr
boostNumeric
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().

TextExpr

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

Query

Methods

select()

select(self) -> Query
Returns Query

filter()

filter(self, expr: LogicalExpr | TextExpr) -> Query
Parameters
ParameterType
exprLogicalExprTextExpr
Returns Query

topk()

topk(self, expr: LogicalExpr, k: int, asc: bool = False) -> Query
Parameters
ParameterType
exprLogicalExpr
kint
ascbool
Returns Query

rerank()

rerank(
   self,
   model: Optional[str] = None,
   query: Optional[str] = None,
   fields: Sequence[str] = ...,
   topk_multiple: Optional[int] = None
)
Parameters
ParameterType
modelOptional[str]
queryOptional[str]
fieldsSequence[str]
topk_multipleOptional[int]
Returns Query

count()

count(self) -> Query
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()

vector_distance(
   field: str,
   vector: list[int] | list[float] | dict[int, float] | dict[int, int] | topk_sdk.data.SparseVector | topk_sdk.data.List,
   skip_refine: bool = False
)
Calculate the vector distance between a field and a query vector.
# Example:

from topk_sdk.query import field, fn, select

client.collection("books").query(
  select(
    "title",
    title_similarity=fn.vector_distance(
      "title_embedding",
      [0.1, 0.2, 0.3, ...] # embedding for "animal"
    )
  )
  .topk(field("title_similarity"), 10)
)
Parameters
ParameterType
fieldstr
vectorlist[int]list[float]dict[int, float]dict[int, int]topk_sdk.data.SparseVectortopk_sdk.data.List
skip_refinebool
Returns FunctionExpr

semantic_similarity()

semantic_similarity(field: str, query: str) -> FunctionExpr
Calculate the semantic similarity between a field and a query string.
# Example:

from topk_sdk.query import field, fn, select

client.collection("books").query(
  select(
    "title",
    title_similarity=fn.semantic_similarity("title", "animal")
  )
  .topk(field("title_similarity"), 10)
)
Parameters
ParameterType
fieldstr
querystr
Returns FunctionExpr

bm25_score()

bm25_score() -> FunctionExpr
Calculate the BM25 score for a keyword search.
# Example:

from topk_sdk.query import field, fn, select

client.collection("books").query(
  select(
    "title",
    text_score=fn.bm25_score()
  )
  .filter(match("animal"))
  .topk(field("text_score"), 10)
)
Returns FunctionExpr

Functions

field()

field(name: str) -> LogicalExpr
Select a field from the document. Parameters
ParameterType
namestr
Returns LogicalExpr

select()

select() -> Query

Example:

Create a select stage of a query.
# Example:

from topk_sdk.query import select, field

client.collection("books").query(
  select("title", year=field("published_year"))
)
Returns Query

filter()

filter(expr: LogicalExpr | TextExpr) -> Query
Create a filter stage of a query.
# Example:

from topk_sdk.query import filter, field

client.collection("books").query(
  filter(field("published_year") > 1980)
)
Parameters
ParameterType
exprLogicalExprTextExpr
Returns Query

literal()

literal(value: Any) -> LogicalExpr
Create a literal expression. Parameters
ParameterType
valueAny
Returns LogicalExpr

match()

match(token: str, field: str | None = None, weight: float = 1.0, all: bool = False) -> LogicalExpr
Perform a keyword search for documents that contain specific keywords or phrases. This function should be used in the filter stage of a query. You can configure the match() function to:
  • Match on multiple terms
  • Match only on specific fields
  • Use weights to prioritize certain terms
Parameters
ParameterType
tokenstr
fieldstrNone
weightfloat
allbool
Returns LogicalExpr

not_()

not_(expr: LogicalExpr) -> LogicalExpr
Negate a logical expression.
# Example:

from topk_sdk.query import field, not_

.filter(
    not_(field("title").contains("Catcher"))
)
Parameters
ParameterType
exprLogicalExpr
Returns LogicalExpr

abs()

abs(expr: LogicalExpr) -> LogicalExpr
Compute the absolute value of a logical expression.
# Example:

from topk_sdk.query import field, abs

client.collection("books").query(
  filter(abs(field("rating")) > 4.5)
)
Parameters
ParameterType
exprLogicalExpr
Returns LogicalExpr

all()

all(exprs: Sequence[LogicalExpr]) -> LogicalExpr
Create a logical AND expression.
# Example:

from topk_sdk.query import field, all

client.collection("books").query(
  filter(all([
    field("published_year") >= 1900,
    field("published_year") <= 2000,
    field("title").is_not_null()
  ]))
)
Parameters
ParameterType
exprsSequence[LogicalExpr]
Returns LogicalExpr

any()

any(exprs: Sequence[LogicalExpr]) -> LogicalExpr
Create a logical OR expression.
# Example:

from topk_sdk.query import field, any

client.collection("books").query(
  filter(any([
    field("genre") == "fiction",
    field("genre") == "mystery",
    field("genre") == "thriller"
  ]))
)
Parameters
ParameterType
exprsSequence[LogicalExpr]
Returns LogicalExpr

min()

min(left: Ordered, right: Ordered) -> LogicalExpr
Create a logical MIN expression.
# Example:

from topk_sdk.query import field, min

client.collection("books").query(
  filter(min(field("rating"), field("published_year")))
)
Parameters
ParameterType
leftOrdered
rightOrdered
Returns LogicalExpr

max()

max(left: Ordered, right: Ordered) -> LogicalExpr
Create a logical MAX expression.
from topk_sdk.query import field, max

client.collection("books").query(
  filter(max(field("rating"), field("published_year")))
)
Parameters
ParameterType
leftOrdered
rightOrdered
Returns LogicalExpr

Type Aliases

FlexibleExpr

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

Numeric

Numeric = int | float | LogicalExpr
Type int | float | LogicalExpr

Ordered

Ordered = int | float | str | LogicalExpr
Type int | float | str | LogicalExpr

Boolish

Boolish = bool | LogicalExpr
Type bool | LogicalExpr

Stringy

Stringy = str | LogicalExpr
Type str | LogicalExpr

StringyWithList

StringyWithList = str | list[str] | LogicalExpr
Type str | list[str] | LogicalExpr

Iterable

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