Skip to content

toElasticsearchQuery

function toElasticsearchQuery(input):
| ElasticsearchQuery
| undefined;

Defined in: filters/elasticsearch.ts:287

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.

Parameter Type
input { fields: Record<string, string>; where: | CompiledWhere | null; }
input.fields Record<string, string>
input.where | CompiledWhere | null

| ElasticsearchQuery | undefined

import { toElasticsearchQuery } from "@xtandard/lib/filters/elasticsearch";
toElasticsearchQuery({
where: { type: "or", nodes: [
{ type: "cond", cond: { field: "amount", op: "gt", value: 5 } },
{ type: "not", node: { type: "cond", cond: { field: "name", op: "eq", value: "x" } } },
] },
fields: { amount: "amount", name: "name" },
});
// → { bool: { should: [
// { range: { amount: { gt: 5 } } },
// { bool: { filter: [{ exists: { field: "name" } }], must_not: [{ term: { name: "x" } }] } },
// ], minimum_should_match: 1 } }