> ## Documentation Index
> Fetch the complete documentation index at: https://docs.topk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# topk-js/query

[topk-js](https://docs.topk.io/sdk/topk-js/index.md) / query

## Functions

### abs()

```ts theme={null}
function abs(expr: LogicalExpression): LogicalExpression;
```

Creates an absolute value expression.

**Parameters**

| Parameter | Type |
| - | - |
| `expr` | [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### all()

```ts theme={null}
function all(exprs: LogicalExpression[]): LogicalExpression;
```

Evaluates to true if each `expr` is true.

**Parameters**

| Parameter | Type |
| - | - |
| `exprs` | [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)\[] |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### any()

```ts theme={null}
function any(exprs: LogicalExpression[]): LogicalExpression;
```

Evaluates to true if at least one `expr` is true.

**Parameters**

| Parameter | Type |
| - | - |
| `exprs` | [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)\[] |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### field()

```ts theme={null}
function field(name: string): LogicalExpression;
```

Creates a field reference expression.

**Parameters**

| Parameter | Type |
| - | - |
| `name` | `string` |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### filter()

```ts theme={null}
function filter(expr: 
  | LogicalExpression
  | FunctionExpression
  | TextExpression): Query;
```

Creates a new query with a filter stage.

**Parameters**

| Parameter | Type |
| - | - |
| `expr` | \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`FunctionExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#functionexpression) \| [`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression) |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

***

### groupBy()

```ts theme={null}
function groupBy(keys: Record<string, LogicalExpression>, aggs: Record<string, AggregateExpression>): Query;
```

Creates a new query with a group-by stage.

Groups documents by one or more key expressions and computes aggregations for each group.

**Parameters**

| Parameter | Type |
| - | - |
| `keys` | `Record`\<`string`, [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)> |
| `aggs` | `Record`\<`string`, [`AggregateExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#aggregateexpression)> |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

***

### literal()

```ts theme={null}
function literal(value: 
  | string
  | number
  | boolean
  | string[]
  | number[]
  | Date
  | List): LogicalExpression;
```

Creates a literal value expression.

**Parameters**

| Parameter | Type |
| - | - |
| `value` | \| `string` \| `number` \| `boolean` \| `string`\[] \| `number`\[] \| `Date` \| [`List`](https://docs.topk.io/sdk/topk-js/Namespace.data#list) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### match()

```ts theme={null}
function match(token: string, options?: MatchOptions): TextExpression;
```

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**

| Parameter | Type |
| - | - |
| `token` | `string` |
| `options?` | [`MatchOptions`](https://docs.topk.io/sdk/topk-js/Namespace.query#matchoptions) |

**Returns**

[`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression)

***

### matchTokens()

```ts theme={null}
function matchTokens(tokens: (
  | string
  | MatchTokenInput)[], options?: MatchTokensOptions): TextExpression;
```

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**

| Parameter | Type |
| - | - |
| `tokens` | ( \| `string` \| [`MatchTokenInput`](https://docs.topk.io/sdk/topk-js/Namespace.query#matchtokeninput))\[] |
| `options?` | [`MatchTokensOptions`](https://docs.topk.io/sdk/topk-js/Namespace.query#matchtokensoptions) |

**Returns**

[`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression)

***

### max()

```ts theme={null}
function max(left: 
  | string
  | number
  | LogicalExpression, right: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

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

**Parameters**

| Parameter | Type |
| - | - |
| `left` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |
| `right` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### min()

```ts theme={null}
function min(left: 
  | string
  | number
  | LogicalExpression, right: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

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

**Parameters**

| Parameter | Type |
| - | - |
| `left` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |
| `right` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### not()

```ts theme={null}
function not(expr: LogicalExpression): LogicalExpression;
```

Creates a logical NOT expression.

**Parameters**

| Parameter | Type |
| - | - |
| `expr` | [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### select()

```ts theme={null}
function select(exprs: Record<string, 
  | LogicalExpression
  | FunctionExpression>): Query;
```

Creates a new query with a select stage.

**Parameters**

| Parameter | Type |
| - | - |
| `exprs` | `Record`\<`string`, \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`FunctionExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#functionexpression)> |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

***

### should()

```ts theme={null}
function should(token: string, options?: ShouldOptions): TextExpression;
```

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.

```js theme={null}
match("hobbit rings", { field: "title" }).and(should("lord", { field: "title" }))
```

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**

| Parameter | Type |
| - | - |
| `token` | `string` |
| `options?` | [`ShouldOptions`](https://docs.topk.io/sdk/topk-js/Namespace.query#shouldoptions) |

**Returns**

[`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression)

## Classes

### AggregateExpression

**`Internal`**

***

### FunctionExpression

**`Internal`**

**Methods**

##### abs()

```ts theme={null}
abs(): LogicalExpression;
```

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### add()

```ts theme={null}
add(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### boost()

```ts theme={null}
boost(condition: 
  | boolean
  | LogicalExpression, boost: 
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `condition` | \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |
| `boost` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### choose()

```ts theme={null}
choose(x: 
  | string
  | number
  | boolean
  | LogicalExpression, y: 
  | string
  | number
  | boolean
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `x` | \| `string` \| `number` \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |
| `y` | \| `string` \| `number` \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### coalesce()

```ts theme={null}
coalesce(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### div()

```ts theme={null}
div(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### eq()

```ts theme={null}
eq(other: 
  | string
  | number
  | boolean
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### exp()

```ts theme={null}
exp(): LogicalExpression;
```

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### gt()

```ts theme={null}
gt(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### gte()

```ts theme={null}
gte(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### isNotNull()

```ts theme={null}
isNotNull(): LogicalExpression;
```

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### isNull()

```ts theme={null}
isNull(): LogicalExpression;
```

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### ln()

```ts theme={null}
ln(): LogicalExpression;
```

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### lt()

```ts theme={null}
lt(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### lte()

```ts theme={null}
lte(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### max()

```ts theme={null}
max(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### min()

```ts theme={null}
min(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### mul()

```ts theme={null}
mul(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### ne()

```ts theme={null}
ne(other: 
  | string
  | number
  | boolean
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### sqrt()

```ts theme={null}
sqrt(): LogicalExpression;
```

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### square()

```ts theme={null}
square(): LogicalExpression;
```

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### sub()

```ts theme={null}
sub(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

***

### LogicalExpression

**`Internal`**

**Methods**

##### abs()

```ts theme={null}
abs(): LogicalExpression;
```

Computes the absolute value of the expression.

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### add()

```ts theme={null}
add(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

Adds another value to the expression.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### and()

```ts theme={null}
and(other: 
  | boolean
  | LogicalExpression): LogicalExpression;
```

Computes the logical AND of the expression and another expression.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### boost()

```ts theme={null}
boost(condition: 
  | boolean
  | LogicalExpression, boost: 
  | number
  | LogicalExpression): LogicalExpression;
```

Multiplies the scoring expression by the provided `boost` value if the `condition` is true.
Otherwise, the scoring expression is unchanged (multiplied by 1).

**Parameters**

| Parameter | Type |
| - | - |
| `condition` | \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |
| `boost` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### choose()

```ts theme={null}
choose(x: 
  | string
  | number
  | boolean
  | LogicalExpression, y: 
  | string
  | number
  | boolean
  | LogicalExpression): LogicalExpression;
```

Chooses between two values based on the expression.

**Parameters**

| Parameter | Type |
| - | - |
| `x` | \| `string` \| `number` \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |
| `y` | \| `string` \| `number` \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### coalesce()

```ts theme={null}
coalesce(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

Coalesces nulls in the expression with another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### contains()

```ts theme={null}
contains(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

Checks if the expression contains another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### datePart()

```ts theme={null}
datePart(part: DatePart): LogicalExpression;
```

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**

| Parameter | Type |
| - | - |
| `part` | [`DatePart`](https://docs.topk.io/sdk/topk-js/Namespace.query#datepart-1) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### div()

```ts theme={null}
div(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

Divides the expression by another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### elapsed()

```ts theme={null}
elapsed(end: 
  | number
  | LogicalExpression, interval: Interval): LogicalExpression;
```

Computes the number of `interval` units elapsed between the expression and `end`.

**Parameters**

| Parameter | Type |
| - | - |
| `end` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |
| `interval` | [`Interval`](https://docs.topk.io/sdk/topk-js/Namespace.query#interval) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### eq()

```ts theme={null}
eq(other: 
  | string
  | number
  | boolean
  | LogicalExpression): LogicalExpression;
```

Checks if the expression equals another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### exp()

```ts theme={null}
exp(): LogicalExpression;
```

Computes the exponential of the expression.

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### gt()

```ts theme={null}
gt(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

Checks if the expression is greater than another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### gte()

```ts theme={null}
gte(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

Checks if the expression is greater than or equal to another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### in()

```ts theme={null}
in(other: 
  | string
  | string[]
  | number[]
  | LogicalExpression
  | List): LogicalExpression;
```

Checks if the expression is in another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `string`\[] \| `number`\[] \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`List`](https://docs.topk.io/sdk/topk-js/Namespace.data#list) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### isNotNull()

```ts theme={null}
isNotNull(): LogicalExpression;
```

Checks if the expression evaluates to a non-null value.

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### isNull()

```ts theme={null}
isNull(): LogicalExpression;
```

Checks if the expression evaluates to null.

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### ln()

```ts theme={null}
ln(): LogicalExpression;
```

Computes the natural logarithm of the expression.

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### lt()

```ts theme={null}
lt(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

Checks if the expression is less than another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### lte()

```ts theme={null}
lte(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

Checks if the expression is less than or equal to another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### matchAll()

```ts theme={null}
matchAll(other: 
  | string
  | string[]
  | LogicalExpression): LogicalExpression;
```

Checks if the expression matches all terms against the field with keyword index.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `string`\[] \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### matchAny()

```ts theme={null}
matchAny(other: 
  | string
  | string[]
  | LogicalExpression): LogicalExpression;
```

Checks if the expression matches any term against the field with keyword index.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `string`\[] \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### max()

```ts theme={null}
max(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

Computes the maximum of the expression and another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### min()

```ts theme={null}
min(other: 
  | string
  | number
  | LogicalExpression): LogicalExpression;
```

Computes the minimum of the expression and another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### mul()

```ts theme={null}
mul(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

Multiplies the expression by another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### ne()

```ts theme={null}
ne(other: 
  | string
  | number
  | boolean
  | LogicalExpression): LogicalExpression;
```

Checks if the expression does not equal another value.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| `number` \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### or()

```ts theme={null}
or(other: 
  | boolean
  | LogicalExpression): LogicalExpression;
```

Computes the logical OR of the expression and another expression.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `boolean` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### regexpMatch()

```ts theme={null}
regexpMatch(other: string, flags?: string): LogicalExpression;
```

Check if the expression matches the provided regexp pattern.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | `string` |
| `flags?` | `string` |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### sqrt()

```ts theme={null}
sqrt(): LogicalExpression;
```

Computes the square root of the expression.

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### square()

```ts theme={null}
square(): LogicalExpression;
```

Computes the square of the expression.

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### startsWith()

```ts theme={null}
startsWith(other: 
  | string
  | LogicalExpression): LogicalExpression;
```

Checks if the expression starts with another value.
Can be applied on a string field or a list of strings field.

```ts theme={null}
// Example:
import { field, startsWith } from "topk-js/query";
client.collection("books").query(
  filter(field("title").startsWith("The") | field("tags").startsWith("fiction"))
)
```

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `string` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### sub()

```ts theme={null}
sub(other: 
  | number
  | LogicalExpression): LogicalExpression;
```

Subtracts another value from the expression.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | \| `number` \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) |

**Returns**

[`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression)

##### toString()

```ts theme={null}
toString(): string;
```

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

```ts theme={null}
count(): Query;
```

Adds a count stage to the query.

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

##### filter()

```ts theme={null}
filter(expr: 
  | LogicalExpression
  | FunctionExpression
  | TextExpression): Query;
```

Adds a filter stage to the query.

**Parameters**

| Parameter | Type |
| - | - |
| `expr` | \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`FunctionExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#functionexpression) \| [`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression) |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

##### groupBy()

```ts theme={null}
groupBy(keys: Record<string, 
  | LogicalExpression
  | FunctionExpression>, aggs: Record<string, AggregateExpression>): Query;
```

Adds a group-by stage to the query.

Groups documents by one or more key expressions and computes aggregations for each group.

**Parameters**

| Parameter | Type |
| - | - |
| `keys` | `Record`\<`string`, \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`FunctionExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#functionexpression)> |
| `aggs` | `Record`\<`string`, [`AggregateExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#aggregateexpression)> |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

##### limit()

```ts theme={null}
limit(k: number): Query;
```

Adds a limit stage to the query.

**Parameters**

| Parameter | Type |
| - | - |
| `k` | `number` |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

##### offset()

```ts theme={null}
offset(offset: number): Query;
```

Adds an offset stage to the query.

**Parameters**

| Parameter | Type |
| - | - |
| `offset` | `number` |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

##### select()

```ts theme={null}
select(exprs: Record<string, 
  | LogicalExpression
  | FunctionExpression>): Query;
```

Adds a select stage to the query.

**Parameters**

| Parameter | Type |
| - | - |
| `exprs` | `Record`\<`string`, \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`FunctionExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#functionexpression)> |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

##### sort()

###### Call Signature

```ts theme={null}
sort(expr: 
  | LogicalExpression
  | FunctionExpression, asc?: boolean): Query;
```

Adds a sort stage to the query.

**Parameters**

| Parameter | Type |
| - | - |
| `expr` | \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`FunctionExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#functionexpression) |
| `asc?` | `boolean` |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

###### Call Signature

```ts theme={null}
sort(expr: SortExpr[]): Query;
```

Adds a sort stage to the query.

**Parameters**

| Parameter | Type |
| - | - |
| `expr` | [`SortExpr`](https://docs.topk.io/sdk/topk-js/Namespace.query#sortexpr)\[] |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

##### ~~topk()~~

```ts theme={null}
topk(
   expr: 
  | LogicalExpression
  | FunctionExpression, 
   k: number, 
   asc?: boolean): Query;
```

Adds a top-k stage to the query.

**Parameters**

| Parameter | Type |
| - | - |
| `expr` | \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`FunctionExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#functionexpression) |
| `k` | `number` |
| `asc?` | `boolean` |

**Returns**

[`Query`](https://docs.topk.io/sdk/topk-js/Namespace.query#query)

###### Deprecated

Use `.sort(expr, false).limit(k)` instead.

***

### TextExpression

**Constructors**

**Constructor**

```ts theme={null}
new TextExpression(): TextExpression;
```

**Returns**

[`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression)

**Methods**

##### and()

```ts theme={null}
and(other: TextExpression): TextExpression;
```

Computes the logical AND of the expression and another text expression.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | [`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression) |

**Returns**

[`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression)

##### or()

```ts theme={null}
or(other: TextExpression): TextExpression;
```

Computes the logical OR of the expression and another text expression.

**Parameters**

| Parameter | Type |
| - | - |
| `other` | [`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression) |

**Returns**

[`TextExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#textexpression)

## Interfaces

### MatchOptions

Options for text matching.

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

**Properties**

| Property | Type | Description |
| - | - | - |
|  `all?` | `boolean` | Whether to match all terms |
|  `field?` | `string` | Field to match against |
|  `weight?` | `number` | Weight for the match |

***

### MatchTokenInput

A token for match\_tokens

**Properties**

| Property | Type | Description |
| - | - | - |
|  `token` | `string` | The token to match |
|  `weight?` | `number` | Weight for the term (defaults to 1.0) |

***

### MatchTokensOptions

Options for match\_tokens.

**Properties**

| Property | Type | Description |
| - | - | - |
|  `all?` | `boolean` | Whether to match all terms |
|  `field?` | `string` | Field to match against |

***

### ShouldOptions

Options for `should` scoring.

**Properties**

| Property | Type | Description |
| - | - | - |
|  `field?` | `string` | Field to score against |
|  `weight?` | `number` | Weight for the term |

***

### SortExpr

An expression to sort by with its sort order.

**Properties**

| Property | Type | Description |
| - | - | - |
|  `expr` | \| [`LogicalExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#logicalexpression) \| [`FunctionExpression`](https://docs.topk.io/sdk/topk-js/Namespace.query#functionexpression) | The expression to sort by. |
|  `order` | [`SortOrder`](https://docs.topk.io/sdk/topk-js/Namespace.query#sortorder) | Sort order. |

***

### Term

**Properties**

| Property | Type | Description |
| - | - | - |
|  `field?` | `string` | The field to match against. |
|  `token` | `string` | The token to match. |
|  `weight` | `number` | The weight of the term. |

## Type Aliases

### DatePart

```ts theme={null}
type DatePart = 
  | "year"
  | "month"
  | "week"
  | "day"
  | "day_of_year"
  | "day_of_week"
  | "hour"
  | "minute"
  | "second"
  | "millisecond";
```

***

### Interval

```ts theme={null}
type Interval = "millisecond" | "second" | "minute" | "hour" | "day" | "week";
```

***

### SortOrder

```ts theme={null}
type SortOrder = "asc" | "desc";
```

Sort order.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.