Overview
The query module provides functions and classes to build queries for searching and filtering data in TopK collections. Import query functions fromtopk-js/query.
Query Building
select(exprs)
Creates a new query with a select stage. The select stage defines which fields and computed values to return.A map of field names to expressions
filter(expr)
Creates a new query with a filter stage. The filter stage restricts which documents are returned.The filter expression
Query Class
A query object that represents a sequence of query stages. Queries are built by chaining together different stages like select, filter, topk, etc.filter(expr)
Adds a filter stage to the query.The filter expression
select(exprs)
Adds a select stage to the query.A map of field names to expressions
topk(expr, k, asc?)
Adds a top-k stage to the query. Returns the top k documents based on the expression.The expression to rank by
The number of documents to return
Whether to sort in ascending order. Defaults to false (descending)
limit(k)
Adds a limit stage to the query.The maximum number of documents to return
sort(expr, asc?)
Adds a sort stage to the query.The expression to sort by
Whether to sort in ascending order. Defaults to false (descending)
count()
Adds a count stage to the query.rerank(options?)
Adds a rerank stage to the query.Optional reranking configuration
The reranking model to use
The query text for reranking
Fields to include in reranking
Multiple of top-k to consider for reranking
Expression Functions
field(name)
Creates a field reference expression.The name of the field to reference
literal(value)
Creates a literal value expression.The literal value
match(token, options?)
Creates a text match expression for keyword search.The token to match
Optional matching configuration
Field to match against
Weight for the match
Whether to match all terms
Utility Functions
abs(expr)
Creates an absolute value expression.The expression to compute absolute value of
all(exprs)
Evaluates to true if each expression is true (logical AND).Array of expressions to evaluate
any(exprs)
Evaluates to true if at least one expression is true (logical OR).Array of expressions to evaluate
min(left, right)
Creates a MIN expression that returns the smaller of two values.First value
Second value
max(left, right)
Creates a MAX expression that returns the larger of two values.First value
Second value
not(expr)
Creates a logical NOT expression.The expression to negate
LogicalExpression
Represents a logical expression that can be used in queries. LogicalExpressions support method chaining for building complex queries.Comparison Methods
eq(other)
Checks if the expression equals another value.The value to compare against
ne(other)
Checks if the expression does not equal another value.lt(other)
Checks if the expression is less than another value.The value to compare against
lte(other)
Checks if the expression is less than or equal to another value.gt(other)
Checks if the expression is greater than another value.gte(other)
Checks if the expression is greater than or equal to another value.Logical Methods
and(other)
Computes the logical AND of the expression and another expression.The expression to AND with
or(other)
Computes the logical OR of the expression and another expression.The expression to OR with
Null Checks
isNull()
Checks if the expression evaluates to null.isNotNull()
Checks if the expression evaluates to a non-null value.Math Methods
abs()
Computes the absolute value of the expression.ln()
Computes the natural logarithm of the expression.exp()
Computes the exponential of the expression.sqrt()
Computes the square root of the expression.square()
Computes the square of the expression.add(other)
Adds another value to the expression.sub(other)
Subtracts another value from the expression.mul(other)
Multiplies the expression by another value.div(other)
Divides the expression by another value.min(other)
Computes the minimum of the expression and another value.max(other)
Computes the maximum of the expression and another value.String Methods
startsWith(other)
Checks if the expression starts with another value. Can be applied to a string field or a list of strings field.The value to check for
contains(other)
Checks if the expression contains another value.The value to check for
in(other)
Checks if the expression is in another value.The value or list to check against
Keyword Search Methods
matchAll(other)
Checks if the expression matches all terms against the field with keyword index.The terms to match
matchAny(other)
Checks if the expression matches any term against the field with keyword index.The terms to match
Other Methods
coalesce(other)
Coalesces nulls in the expression with another value.The value to use if the expression is null
choose(x, y)
Chooses between two values based on the expression.Value to return if expression is true
Value to return if expression is false
boost(condition, boost)
Multiplies the scoring expression by the provided boost value if the condition is true. Otherwise, the scoring expression is unchanged (multiplied by 1).The condition to check
The boost multiplier
regexpMatch(pattern, flags?)
Check if the expression matches the provided regexp pattern.The regular expression pattern
Optional regex flags
Function Expressions (fn)
Function expressions compute special values like semantic similarity or vector distance. Import fromtopk-js/query as fn.
fn.semanticSimilarity(field, query)
Computes the semantic similarity between a field and a query string.The name of the field to search
The query text
fn.vectorDistance(field, query, options?)
Computes the vector distance between a field and a query vector.The name of the vector field
The query vector (dense or sparse)
Optional configuration
Whether to skip refinement step
fn.multiVectorDistance(field, query, candidates?)
Calculates the multi-vector distance between a field and a query matrix.The name of the matrix field
The query matrix. Can be an array of number arrays (defaults to f32), or a Matrix instance
Limits the number of candidate vectors considered during retrieval