Skip to main content
topk-js / query

Functions

abs()

Creates an absolute value expression. Parameters Returns LogicalExpression

all()

Evaluates to true if each expr is true. Parameters Returns LogicalExpression

any()

Evaluates to true if at least one expr is true. Parameters Returns LogicalExpression

field()

Creates a field reference expression. Parameters Returns LogicalExpression

filter()

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

groupBy()

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

Creates a literal value expression. Parameters Returns LogicalExpression

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.
  • options.field: restrict matching to a specific keyword-indexed field (default: all).
  • options.weight: scale the BM25 contribution of this expression (default: 1.0).
  • options.all: if true, require all tokens to match (AND); default is any-token (OR).
Parameters Returns TextExpression

matchTokens()

Creates a text match expression from multiple tokens with optional per-token weights. Each token can be a string (with the default weight of 1.0) or a MatchTokenInput object. Parameters Returns TextExpression

max()

Creates a MAX expression that returns the larger of two values. Parameters Returns LogicalExpression

min()

Creates a MIN expression that returns the smaller of two values. Parameters Returns LogicalExpression

not()

Creates a logical NOT expression. Parameters Returns LogicalExpression

select()

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

should()

Adds an optional BM25 scoring term without filtering documents from the result set. Documents containing the term receive a higher BM25 score, while documents that do not contain it remain eligible for the results. Use should() together with match() when some terms are required and others should only influence ranking. When used on its own, should() matches the entire collection and ranks documents according to how well they match the term.
This returns only documents matching hobbit or rings, while boosting documents that also match lord.
  • options.field: Keyword-indexed field used for scoring. Searches all eligible fields when omitted.
  • options.weight: Multiplier applied to the term’s BM25 contribution. Defaults to 1.0.
Parameters Returns TextExpression

Classes

AggregateExpression

Internal

FunctionExpression

Internal Methods
abs()
Returns LogicalExpression
add()
Parameters Returns LogicalExpression
boost()
Parameters Returns LogicalExpression
choose()
Parameters Returns LogicalExpression
coalesce()
Parameters Returns LogicalExpression
div()
Parameters Returns LogicalExpression
eq()
Parameters Returns LogicalExpression
exp()
Returns LogicalExpression
gt()
Parameters Returns LogicalExpression
gte()
Parameters Returns LogicalExpression
isNotNull()
Returns LogicalExpression
isNull()
Returns LogicalExpression
ln()
Returns LogicalExpression
lt()
Parameters Returns LogicalExpression
lte()
Parameters Returns LogicalExpression
max()
Parameters Returns LogicalExpression
min()
Parameters Returns LogicalExpression
mul()
Parameters Returns LogicalExpression
ne()
Parameters Returns LogicalExpression
sqrt()
Returns LogicalExpression
square()
Returns LogicalExpression
sub()
Parameters Returns LogicalExpression

LogicalExpression

Internal Methods
abs()
Computes the absolute value of the expression. Returns LogicalExpression
add()
Adds another value to the expression. Parameters Returns LogicalExpression
and()
Computes the logical AND of the expression and another expression. Parameters Returns LogicalExpression
boost()
Multiplies the scoring expression by the provided boost value if the condition is true. Otherwise, the scoring expression is unchanged (multiplied by 1). Parameters Returns LogicalExpression
choose()
Chooses between two values based on the expression. Parameters Returns LogicalExpression
coalesce()
Coalesces nulls in the expression with another value. Parameters Returns LogicalExpression
contains()
Checks if the expression contains another value. Parameters Returns LogicalExpression
datePart()
Extracts a part of a timestamp expression as an integer. Supported parts:
  • "year" — calendar year
  • "month" — 1-12
  • "week" — ISO week number
  • "day" — day of month, 1-31
  • "day_of_year" — 1-366
  • "day_of_week" — 0-6, Monday = 0
  • "hour" — 0-23
  • "minute" — 0-59
  • "second" — 0-59
  • "millisecond" — 0-999
Parameters Returns LogicalExpression
div()
Divides the expression by another value. Parameters Returns LogicalExpression
elapsed()
Computes the number of interval units elapsed between the expression and end. Parameters Returns LogicalExpression
eq()
Checks if the expression equals another value. Parameters Returns LogicalExpression
exp()
Computes the exponential of the expression. Returns LogicalExpression
gt()
Checks if the expression is greater than another value. Parameters Returns LogicalExpression
gte()
Checks if the expression is greater than or equal to another value. Parameters Returns LogicalExpression
in()
Checks if the expression is in another value. Parameters Returns LogicalExpression
isNotNull()
Checks if the expression evaluates to a non-null value. Returns LogicalExpression
isNull()
Checks if the expression evaluates to null. Returns LogicalExpression
ln()
Computes the natural logarithm of the expression. Returns LogicalExpression
lt()
Checks if the expression is less than another value. Parameters Returns LogicalExpression
lte()
Checks if the expression is less than or equal to another value. Parameters Returns LogicalExpression
matchAll()
Checks if the expression matches all terms against the field with keyword index. Parameters Returns LogicalExpression
matchAny()
Checks if the expression matches any term against the field with keyword index. Parameters Returns LogicalExpression
max()
Computes the maximum of the expression and another value. Parameters Returns LogicalExpression
min()
Computes the minimum of the expression and another value. Parameters Returns LogicalExpression
mul()
Multiplies the expression by another value. Parameters Returns LogicalExpression
ne()
Checks if the expression does not equal another value. Parameters Returns LogicalExpression
or()
Computes the logical OR of the expression and another expression. Parameters Returns LogicalExpression
regexpMatch()
Check if the expression matches the provided regexp pattern. Parameters Returns LogicalExpression
sqrt()
Computes the square root of the expression. Returns LogicalExpression
square()
Computes the square of the expression. Returns LogicalExpression
startsWith()
Checks if the expression starts with another value. Can be applied on a string field or a list of strings field.
Parameters Returns LogicalExpression
sub()
Subtracts another value from the expression. Parameters Returns LogicalExpression
toString()
Returns a string representation of the logical expression. Returns string

Query

Internal A query object that represents a sequence of query stages. Queries are built by chaining together different stages like select, filter, topk, etc. Each stage performs a specific operation on the data. Methods
count()
Adds a count stage to the query. Returns Query
filter()
Adds a filter stage to the query. Parameters Returns Query
groupBy()
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
limit()
Adds a limit stage to the query. Parameters Returns Query
offset()
Adds an offset stage to the query. Parameters Returns Query
select()
Adds a select stage to the query. Parameters Returns Query
sort()
Call Signature
Adds a sort stage to the query. Parameters Returns Query
Call Signature
Adds a sort stage to the query. Parameters Returns Query
topk()
Adds a top-k stage to the query. Parameters Returns Query
Deprecated
Use .sort(expr, false).limit(k) instead.

TextExpression

Constructors Constructor
Returns TextExpression Methods
and()
Computes the logical AND of the expression and another text expression. Parameters Returns TextExpression
or()
Computes the logical OR of the expression and another text expression. Parameters Returns TextExpression

Interfaces

MatchOptions

Options for text matching. This struct contains configuration options for text matching operations, including field specification, weight, and matching behavior. Properties

MatchTokenInput

A token for match_tokens Properties

MatchTokensOptions

Options for match_tokens. Properties

ShouldOptions

Options for should scoring. Properties

SortExpr

An expression to sort by with its sort order. Properties

Term

Properties

Type Aliases

DatePart


Interval


SortOrder

Sort order.