Skip to content

@xtandard/lib/filters/elasticsearch

Renders the compiled filter AST to an Elasticsearch / OpenSearch Query DSL clause (no client dependency; plain objects for @elastic/elasticsearch, @opensearch-project/opensearch or a raw _search body).

Scalars → term/range; inArray → terms; null checks → exists; contains/endsWith → wildcard and startsWith → prefix, both case_insensitive (ES 7.10+ / OpenSearch); like/ilike patterns → wildcard (% → *, _ → ?). and/or → bool.filter / bool.should; negation keeps SQL’s NULL semantics (an exists guard, so ne/not never match a document without the field). arrayContains → one term per value, arrayOverlaps → terms; arrayContained has no script-free equivalent and throws. Text fields must be keyword (or a .keyword sub-field). Sorting → buildSort, search_after keyset pagination → createElasticsearchKeyset.

import { buildQuery, textField, numberField } from "@xtandard/lib/filters/elasticsearch";
const spec = { name: textField({ field: "name.keyword" }), amount: numberField({ field: "amount" }) };
const { query } = buildQuery({ spec, filters, resolveDate });
// query → { bool: { filter: [{ wildcard: { "name.keyword": { value: "*ab*", case_insensitive: true } } }, …] } }
await client.search({ index: "tasks", query: { bool: { filter: query ? [query] : [] } } });
import { … } from "@xtandard/lib/filters/elasticsearch";
Name Summary
buildFilterNode Compile a recursive and/or/not FilterNode tree into a Query DSL clause (or undefined). Same allow-list rules as buildQuery; not follows SQL NULL semantics (see toElasticsearchQuery).
buildQuery Compile a flat, AND-combined list of column filters into a Query DSL clause. Only fields in spec whose kind matches the filter’s kind are kept; the rest are silently dropped. query is undefined when nothing survives. A date preset filter needs resolveDate (it throws without one). Put it in a bool.filter next to your own clauses (tenant, full-text match) so it doesn’t affect scoring.
buildSort Render a Sort to an Elasticsearch/OpenSearch sort array, allow-listed to fields (public field name → sortable field: keyword, numeric, date). Unknown fields are dropped; when nothing is left, defaultSort is used instead. nulls sets missing on every entry (Elasticsearch’s default is _last in both directions, unlike Postgres).
createElasticsearchKeyset Build a search_after keyset helper from a sort spec + the field each logical key sorts on. The last sort column MUST be unique (a tiebreaker such as a keyword copy of the id) so the cursor is stable. sort(direction) gives the sort array ("backward" flips every column) and searchAfter(cursor) the matching search_after values. The seek happens in the engine, so it composes with any query (including buildQuery’s). Sort keys without a field in fields are skipped (keep fields complete: search_after must line up with sort).
toElasticsearchQuery Render an already-compiled CompiledWhere AST (from compileFilters / compileFilterNode in @xtandard/lib/filters) to a Query DSL clause, looking each field up in fields. Conditions on fields missing from fields are dropped; null (or an AST that drops to nothing) gives undefined. and → bool.filter, or → bool.should (minimum_should_match: 1), and negation follows SQL’s three-valued logic: ne, notInArray, notIlike and not never match a document where the field is missing or null (an exists guard is added). Most code calls buildQuery, which compiles and renders in one step.
Name Summary
ElasticsearchKeyset The keyset helper returned by createElasticsearchKeyset.
Name Summary
ElasticsearchFieldSpec One allow-listed field: its filter kind plus the indexed field it queries — a keyword, numeric, date or boolean field (for text, the keyword sub-field, e.g. "name.keyword").
ElasticsearchFilterSpec A resource’s filter allow-list: public field name → ElasticsearchFieldSpec.
ElasticsearchQuery One Query DSL clause ({ term: … }, { bool: … }, …), ready for the query of a search request (Elasticsearch or OpenSearch client, or a raw _search body).
ElasticsearchSort One sort entry: { [field]: { order, missing? } }.
Name Summary
arrayField Allow-list a public field as an array filter over a multi-valued keyword (or numeric) field. arrayContains → one term per value (all must match), arrayOverlaps → terms. arrayContained (field ⊆ values) has no script-free Query DSL equivalent and throws.
booleanField Allow-list a public field as a boolean filter (eq/ne/null checks) over a boolean field. ne keeps SQL semantics: documents without the field don’t match.
dateField Allow-list a public field as a date filter over a date field. A date preset needs the resolveDate you pass to buildQuery, and becomes a half-open range (gte/lt) with ISO-8601 instants.
enumField Allow-list a public field as an enum filter (eq/ne/inArray/notInArray/null checks) over a keyword field. inArray → terms.
numberField Allow-list a public field as a number filter over a numeric field. between becomes two range clauses (gte + lte).
textField Allow-list a public field as a text filter. Point it at a keyword field (or a text field’s .keyword sub-field): eq is an exact term, contains/endsWith a case-insensitive wildcard (input escaped), startsWith a case-insensitive prefix, and like/ilike patterns are translated to wildcard (% → *, _ → ?).