@searchkit/api
Advanced tools
Comparing version 4.0.0-next.8 to 4.0.0-next.9
@@ -42,2 +42,13 @@ import { MultipleQueriesQuery } from '@algolia/client-search'; | ||
} | ||
interface ContextRuleCondition { | ||
context: 'context'; | ||
value: string[]; | ||
} | ||
interface FilterRuleCondition { | ||
context: 'filterPresent'; | ||
values: { | ||
attribute: string; | ||
value: string; | ||
}[]; | ||
} | ||
interface PinnedResultAction { | ||
@@ -66,5 +77,5 @@ action: 'PinnedResult'; | ||
type QueryRuleAction = PinnedResultAction | QueryAttributeBoostAction | QueryRewriteAction | RenderUserDataAction | RenderFacetsOrderAction; | ||
type QueryRuleCondition = QueryStringRuleCondition; | ||
type QueryRuleCondition = QueryStringRuleCondition | ContextRuleCondition | FilterRuleCondition; | ||
interface QueryRule { | ||
conditions: QueryRuleCondition[]; | ||
conditions: QueryRuleCondition[][]; | ||
actions: QueryRuleAction[]; | ||
@@ -71,0 +82,0 @@ } |
@@ -60,2 +60,14 @@ "use strict"; | ||
}; | ||
var createElasticsearchQueryFromRequest = (requests) => { | ||
return requests.reduce( | ||
(sum, request) => [ | ||
...sum, | ||
JSON.stringify({ index: request.indexName }), | ||
"\n", | ||
JSON.stringify(request.body), | ||
"\n" | ||
], | ||
[] | ||
).join(""); | ||
}; | ||
@@ -407,3 +419,5 @@ // src/filters.ts | ||
]; | ||
const organicQuery = typeof query === "string" && query !== "" ? (requestOptions == null ? void 0 : requestOptions.getQuery) ? requestOptions.getQuery(query, searchAttributes, config) : RelevanceQueryMatch(query, searchAttributes) : []; | ||
const organicQuery = typeof query === "string" && query !== "" ? (requestOptions == null ? void 0 : requestOptions.getQuery) ? requestOptions.getQuery(query, searchAttributes, config) : RelevanceQueryMatch(query, searchAttributes) : { | ||
match_all: {} | ||
}; | ||
return { | ||
@@ -691,12 +705,3 @@ bool: { | ||
}, | ||
body: requests.reduce( | ||
(sum, request) => [ | ||
...sum, | ||
JSON.stringify({ index: request.indexName }), | ||
"\n", | ||
JSON.stringify(request.body), | ||
"\n" | ||
], | ||
[] | ||
).join(""), | ||
body: createElasticsearchQueryFromRequest(requests), | ||
method: "POST" | ||
@@ -734,6 +739,25 @@ }); | ||
// src/queryRules.ts | ||
var getFacetFilters = (facetFilters) => { | ||
if (!facetFilters) { | ||
return []; | ||
} | ||
if (typeof facetFilters === "string") { | ||
const [attribute, value] = facetFilters.split(":"); | ||
return [{ attribute, value }]; | ||
} else { | ||
return facetFilters.reduce((sum, filter) => { | ||
if (typeof filter === "string") { | ||
const [attribute, value] = filter.split(":"); | ||
return [...sum, { attribute, value }]; | ||
} | ||
return [...sum, ...getFacetFilters(filter)]; | ||
}, []); | ||
} | ||
}; | ||
var getQueryRulesActionsFromRequest = (queryRules, request) => { | ||
var _a; | ||
var _a, _b, _c; | ||
const queryContext = { | ||
query: ((_a = request.params) == null ? void 0 : _a.query) || "" | ||
query: ((_a = request.params) == null ? void 0 : _a.query) || "", | ||
context: ((_b = request.params) == null ? void 0 : _b.ruleContexts) || [], | ||
filters: getFacetFilters((_c = request.params) == null ? void 0 : _c.facetFilters) | ||
}; | ||
@@ -776,14 +800,26 @@ const satisfiedRules = getSatisfiedRules(queryContext, queryRules || []); | ||
var getSatisfiedRules = (queryContext, rules) => rules.filter( | ||
(rule) => rule.conditions.filter((condition) => { | ||
if (condition.context === "query" && condition.match_type === "exact") { | ||
return condition.value === queryContext.query; | ||
} | ||
if (condition.context === "query" && condition.match_type === "contains") { | ||
return queryContext.query.includes(condition.value); | ||
} | ||
if (condition.context === "query" && condition.match_type === "prefix") { | ||
return queryContext.query.startsWith(condition.value); | ||
} | ||
return false; | ||
}).length > 0 | ||
(ruleOrs) => ruleOrs.conditions.find( | ||
(rule) => rule.filter((condition) => { | ||
if (condition.context === "query" && condition.match_type === "exact") { | ||
return condition.value === queryContext.query; | ||
} | ||
if (condition.context === "query" && condition.match_type === "contains") { | ||
return queryContext.query.includes(condition.value); | ||
} | ||
if (condition.context === "query" && condition.match_type === "prefix") { | ||
return queryContext.query.startsWith(condition.value); | ||
} | ||
if (condition.context === "context") { | ||
return condition.value.some((value) => queryContext.context.includes(value)); | ||
} | ||
if (condition.context === "filterPresent") { | ||
return condition.values.every( | ||
(value) => queryContext.filters.find( | ||
(filter) => filter.attribute === value.attribute && filter.value === value.value | ||
) !== void 0 | ||
); | ||
} | ||
return false; | ||
}).length === rule.length | ||
) !== void 0 | ||
); | ||
@@ -801,3 +837,4 @@ | ||
console.log("Performing search with requests:"); | ||
console.log(JSON.stringify(requests, null, 2)); | ||
console.log("POST /_msearch"); | ||
console.log(createElasticsearchQueryFromRequest(requests)); | ||
} | ||
@@ -804,0 +841,0 @@ const responses = await this.transporter.msearch(requests); |
{ | ||
"name": "@searchkit/api", | ||
"version": "4.0.0-next.8", | ||
"version": "4.0.0-next.9", | ||
"main": "./dist/index.js", | ||
@@ -5,0 +5,0 @@ "description": "", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
167141
1897