@searchkit/api
Advanced tools
Comparing version 4.0.0-next.1 to 4.0.0-next.2
@@ -12,4 +12,5 @@ import { MultipleQueriesQuery } from '@algolia/client-search'; | ||
attribute: string; | ||
field?: string; | ||
type?: 'numeric' | 'string' | 'date'; | ||
field: string; | ||
type: 'numeric' | 'string' | 'date'; | ||
nestedPath?: string; | ||
}; | ||
@@ -16,0 +17,0 @@ interface ClientConfigConnection { |
@@ -28,18 +28,11 @@ "use strict"; | ||
// src/utils.ts | ||
var getFacetField = (facet_attributes, attribute) => { | ||
var _a; | ||
const attributeKey = typeof attribute === "string" ? attribute : attribute.attribute; | ||
if (facet_attributes.includes(attributeKey)) { | ||
return attributeKey; | ||
} | ||
return ((_a = facet_attributes.find((a) => a.attribute === attributeKey)) == null ? void 0 : _a.field) || attributeKey; | ||
var getFacet = (facet_attributes, attributeName) => { | ||
const f = facet_attributes.find((a) => { | ||
if (typeof a === "string") { | ||
return a === attributeName; | ||
} | ||
return a.attribute === attributeName; | ||
}); | ||
return f || attributeName; | ||
}; | ||
var getFacetByAttribute = (facet_attributes, attribute) => { | ||
var _a; | ||
const attributeKey = getFacetAttribute(attribute); | ||
if (facet_attributes.includes(attributeKey)) { | ||
return attributeKey; | ||
} | ||
return ((_a = facet_attributes.find((a) => a.attribute === attributeKey)) == null ? void 0 : _a.attribute) || attributeKey; | ||
}; | ||
var getFacetAttribute = (facetAttribute) => { | ||
@@ -135,2 +128,5 @@ return typeof facetAttribute === "string" ? facetAttribute : facetAttribute.attribute; | ||
}; | ||
var termFilter = (field, value) => { | ||
return { term: { [field]: value } }; | ||
}; | ||
var transformFacetFilters = (request, config) => { | ||
@@ -150,4 +146,32 @@ const { params = {} } = request; | ||
const [facet, value] = filter2.split(":"); | ||
const field = getFacetField(config.facet_attributes || [], facet); | ||
return [...sum2, { term: { [field]: value } }]; | ||
const facetAttribute = getFacetAttribute(facet); | ||
const facetConfig = getFacet(config.facet_attributes || [], facetAttribute); | ||
if (typeof facetConfig !== "string" && isNestedFacet(facetConfig) && facetConfig.nestedPath) { | ||
const nestedFilter = sum2.find((filter3) => { | ||
return filter3.nested && filter3.nested.path === facetConfig.nestedPath; | ||
}); | ||
if (nestedFilter) { | ||
nestedFilter.nested.query.bool.should.push( | ||
termFilter(`${facetConfig.nestedPath}.${facetConfig.field}`, value) | ||
); | ||
return sum2; | ||
} else { | ||
return [ | ||
...sum2, | ||
{ | ||
nested: { | ||
path: facetConfig.nestedPath, | ||
query: { | ||
bool: { | ||
should: [ | ||
termFilter(`${facetConfig.nestedPath}.${facetConfig.field}`, value) | ||
] | ||
} | ||
} | ||
} | ||
} | ||
]; | ||
} | ||
} | ||
return [...sum2, termFilter(facetAttribute, value)]; | ||
}, []) | ||
@@ -159,66 +183,53 @@ } | ||
const [facet, value] = filter.split(":"); | ||
const field = getFacetField(config.facet_attributes || [], facet); | ||
return [...sum, { term: { [field]: value } }]; | ||
const facetAttribute = getFacetAttribute(facet); | ||
const facetConfig = getFacet(config.facet_attributes || [], facetAttribute); | ||
if (typeof facetConfig !== "string" && isNestedFacet(facetConfig) && facetConfig.nestedPath) { | ||
const nestedFilter = sum.find((filter2) => { | ||
return filter2.nested && filter2.nested.path === facetConfig.nestedPath + "."; | ||
}); | ||
if (nestedFilter) { | ||
nestedFilter.nested.query.bool.should.push( | ||
termFilter(`${facetConfig.nestedPath}.${facetConfig.field}`, value) | ||
); | ||
return sum; | ||
} else { | ||
return [ | ||
...sum, | ||
{ | ||
nested: { | ||
path: facetConfig.nestedPath, | ||
query: { | ||
bool: { | ||
should: [termFilter(`${facetConfig.nestedPath}.${facetConfig.field}`, value)] | ||
} | ||
} | ||
} | ||
} | ||
]; | ||
} | ||
} | ||
return [...sum, termFilter(facet, value)]; | ||
} | ||
}, []); | ||
}; | ||
var getAggs = (request, config, queryRuleActions) => { | ||
const { params = {}, type } = request; | ||
const { facets, maxValuesPerFacet, facetName, facetQuery } = params; | ||
const maxFacetSize = maxValuesPerFacet || 10; | ||
if (facetName) { | ||
return { | ||
[facetName]: { | ||
terms: { | ||
field: getFacetField(config.facet_attributes || [], facetName), | ||
size: maxFacetSize, | ||
include: createRegexQuery(facetQuery) | ||
} | ||
} | ||
}; | ||
} else if (Array.isArray(facets)) { | ||
let facetAttibutes = config.facet_attributes || []; | ||
if (queryRuleActions.facetAttributesOrder) { | ||
facetAttibutes = queryRuleActions.facetAttributesOrder.map((attribute) => { | ||
return getFacetByAttribute(config.facet_attributes || [], attribute); | ||
}); | ||
} | ||
const facetAttributes = facets[0] === "*" ? facetAttibutes : facets; | ||
return facetAttributes.reduce((sum, facet) => { | ||
const fieldType = getFacetFieldType(config.facet_attributes || [], facet); | ||
const field = getFacetField(config.facet_attributes || [], facet); | ||
const attributeName = getFacetByAttribute(config.facet_attributes || [], facet); | ||
if (fieldType === "numeric") { | ||
return { | ||
...sum, | ||
[attributeName + "$_stats"]: { | ||
stats: { | ||
field | ||
} | ||
}, | ||
[attributeName + "$_entries"]: { | ||
terms: { | ||
field, | ||
size: maxFacetSize | ||
} | ||
} | ||
}; | ||
} | ||
return { | ||
...sum, | ||
[attributeName]: { | ||
var isNestedFacet = (facet) => { | ||
return typeof facet !== "string" && !!facet.nestedPath; | ||
}; | ||
var getTermAggregation = (facet, size, search) => { | ||
const searchInclude = search && search.length > 0 ? { include: createRegexQuery(search) } : {}; | ||
let aggEntries = {}; | ||
const getInnerAggs = (facetName, field) => { | ||
if (typeof facet === "string" || facet.type === "string") { | ||
aggEntries = { | ||
[facetName]: { | ||
terms: { | ||
field, | ||
size: maxFacetSize | ||
size, | ||
...searchInclude | ||
} | ||
} | ||
}; | ||
}, {}) || {}; | ||
} else if (typeof facets === "string") { | ||
const fieldType = getFacetFieldType(config.facet_attributes || [], facets); | ||
const field = getFacetField(config.facet_attributes || [], facets); | ||
const attributeName = getFacetByAttribute(config.facet_attributes || [], facets); | ||
if (fieldType === "numeric") { | ||
return { | ||
[attributeName + "$_stats"]: { | ||
} else if (facet.type === "numeric") { | ||
aggEntries = { | ||
[facetName + "$_stats"]: { | ||
stats: { | ||
@@ -228,6 +239,6 @@ field | ||
}, | ||
[attributeName + "$_entries"]: { | ||
[facetName + "$_entries"]: { | ||
terms: { | ||
field, | ||
size: maxFacetSize | ||
size | ||
} | ||
@@ -237,12 +248,47 @@ } | ||
} | ||
return aggEntries; | ||
}; | ||
if (typeof facet === "string") { | ||
return getInnerAggs(facet, facet); | ||
} else if (isNestedFacet(facet)) { | ||
return { | ||
[attributeName]: { | ||
terms: { | ||
field, | ||
size: maxFacetSize | ||
} | ||
[`${facet.nestedPath}.`]: { | ||
nested: { | ||
path: facet.nestedPath | ||
}, | ||
aggs: getInnerAggs(facet.attribute, `${facet.nestedPath}.${facet.field}`) | ||
} | ||
}; | ||
} else { | ||
return getInnerAggs(facet.attribute, facet.field); | ||
} | ||
}; | ||
var getAggs = (request, config, queryRuleActions) => { | ||
const { params = {}, type } = request; | ||
const { facets, maxValuesPerFacet, facetName, facetQuery } = params; | ||
const maxFacetSize = maxValuesPerFacet || 10; | ||
const facetAttributes = config.facet_attributes || []; | ||
if (facetName) { | ||
return getTermAggregation(getFacet(facetAttributes, facetName), maxFacetSize, facetQuery); | ||
} else if (Array.isArray(facets)) { | ||
let facetAttibutes = config.facet_attributes || []; | ||
if (queryRuleActions.facetAttributesOrder) { | ||
facetAttibutes = queryRuleActions.facetAttributesOrder.map((attribute) => { | ||
return getFacet(config.facet_attributes || [], attribute); | ||
}); | ||
} | ||
const facetAttributes2 = facets[0] === "*" ? facetAttibutes : facets.map((facetAttribute) => { | ||
return getFacet(config.facet_attributes || [], facetAttribute); | ||
}); | ||
return facetAttributes2.reduce((sum, facet) => { | ||
return { | ||
...sum, | ||
...getTermAggregation(facet, maxFacetSize, "") | ||
}; | ||
}, {}) || {}; | ||
} else if (typeof facets === "string") { | ||
const field = getFacet(config.facet_attributes || [], facets); | ||
return getTermAggregation(field, maxFacetSize, ""); | ||
} | ||
}; | ||
function RelevanceQueryMatch(query, search_attributes, queryRuleActions) { | ||
@@ -399,6 +445,22 @@ if (queryRuleActions) { | ||
var getFacets = (response, config) => { | ||
const { aggregations } = response; | ||
if (!aggregations) { | ||
if (!(response == null ? void 0 : response.aggregations)) { | ||
return {}; | ||
} | ||
const aggregations = Object.keys(response.aggregations).reduce( | ||
(sum, key) => { | ||
const value = (response.aggregations || {})[key]; | ||
if (key.endsWith(".")) { | ||
const { doc_count, ...nestedAggregations } = value; | ||
return { | ||
...sum, | ||
...nestedAggregations | ||
}; | ||
} | ||
return { | ||
...sum, | ||
[key]: value | ||
}; | ||
}, | ||
{} | ||
); | ||
return Object.keys(aggregations).reduce( | ||
@@ -405,0 +467,0 @@ (sum, f) => { |
{ | ||
"name": "@searchkit/api", | ||
"version": "4.0.0-next.1", | ||
"version": "4.0.0-next.2", | ||
"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
135678
1565