feat(elasticsearch): improve REST commands and visual filters
This commit is contained in:
15 files changed
+1151
-115
No files matched your search
+50
-1
@@ -1,6 +1,16 @@
|
||||
import { strict as assert } from "node:assert";
|
||||
import { test } from "vitest";
|
||||
import { buildDocumentFilterCondition, combineDocumentFilterConditions, currentDocumentFilterJson, documentStoreProviderFor, elasticsearchSearchBodyFromDocumentQuery, type DocumentFilterRule } from "../../apps/desktop/src/lib/app/documentStoreProvider.ts";
|
||||
import {
|
||||
buildDocumentFilterCondition,
|
||||
buildElasticsearchQueryFromRules,
|
||||
combineDocumentFilterConditions,
|
||||
currentDocumentFilterJson,
|
||||
documentStoreProviderFor,
|
||||
elasticsearchQueryTypeOptions,
|
||||
elasticsearchSearchBodyFromDocumentQuery,
|
||||
elasticsearchStructuredFilter,
|
||||
type DocumentFilterRule,
|
||||
} from "../../apps/desktop/src/lib/app/documentStoreProvider.ts";
|
||||
|
||||
function rule(patch: Partial<DocumentFilterRule>): DocumentFilterRule {
|
||||
return {
|
||||
@@ -98,3 +108,42 @@ test("translates document filters to Elasticsearch search body previews", () =>
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test("builds native Elasticsearch bool queries from visual rules", () => {
|
||||
const query = buildElasticsearchQueryFromRules([
|
||||
rule({ fieldName: "customer_name", rawValue: "Customer", elasticsearchClause: "must", elasticsearchQueryType: "match" }),
|
||||
rule({ fieldName: "amount", rawValue: "500", elasticsearchClause: "filter", elasticsearchQueryType: "range_gte" }),
|
||||
rule({ fieldName: "status", rawValue: "cancelled", elasticsearchClause: "must_not", elasticsearchQueryType: "term" }),
|
||||
rule({ fieldName: "note", rawValue: "", elasticsearchClause: "should", elasticsearchQueryType: "exists" }),
|
||||
]);
|
||||
|
||||
assert.deepEqual(query, {
|
||||
bool: {
|
||||
must: [{ match: { customer_name: "Customer" } }],
|
||||
filter: [{ range: { amount: { gte: 500 } } }],
|
||||
must_not: [{ term: { status: "cancelled" } }],
|
||||
should: [{ exists: { field: "note" } }],
|
||||
minimum_should_match: 1,
|
||||
},
|
||||
});
|
||||
|
||||
const body = elasticsearchSearchBodyFromDocumentQuery({
|
||||
filterJson: currentDocumentFilterJson("", elasticsearchStructuredFilter(query), "elasticsearch"),
|
||||
skip: 0,
|
||||
limit: 25,
|
||||
});
|
||||
assert.deepEqual(body.query, query);
|
||||
});
|
||||
|
||||
test("offers Elasticsearch query types based on mapping field type", () => {
|
||||
assert.deepEqual(elasticsearchQueryTypeOptions("text"), ["match", "match_phrase", "term", "wildcard", "exists"]);
|
||||
assert.deepEqual(elasticsearchQueryTypeOptions("keyword"), ["term", "terms", "wildcard", "exists"]);
|
||||
assert.ok(elasticsearchQueryTypeOptions("double").includes("range_gte"));
|
||||
assert.deepEqual(elasticsearchQueryTypeOptions("boolean"), ["term", "exists"]);
|
||||
});
|
||||
|
||||
test("builds wildcard queries compatible with Elasticsearch 7.x", () => {
|
||||
assert.deepEqual(buildElasticsearchQueryFromRules([rule({ fieldName: "sku", rawValue: "DBX-*", elasticsearchQueryType: "wildcard" })]), {
|
||||
bool: { filter: [{ wildcard: { sku: "DBX-*" } }] },
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user