@searchkit/api
Advanced tools
Comparing version 4.0.0-next.7 to 4.0.0-next.8
@@ -16,2 +16,8 @@ import { MultipleQueriesQuery } from '@algolia/client-search'; | ||
}; | ||
type FilterAttribute = { | ||
attribute: string; | ||
field: string; | ||
type: 'numeric' | 'string' | 'date'; | ||
nestedPath?: string; | ||
}; | ||
interface ClientConfigConnection { | ||
@@ -25,2 +31,3 @@ host: string; | ||
facet_attributes?: FacetAttribute[]; | ||
filter_attributes?: FilterAttribute[]; | ||
result_attributes: string[]; | ||
@@ -172,2 +179,2 @@ highlight_attributes?: string[]; | ||
export { AppSettings, ClientConfig, ClientConfigConnection, ElasticsearchHit, ElasticsearchQuery, ElasticsearchResponseBody, FacetAttribute, FacetFieldConfig, QueryRule, QueryRuleAction, QueryRuleCondition, RequestOptions, SearchRequest, SearchSettingsConfig, Transporter, createClient as default }; | ||
export { AppSettings, ClientConfig, ClientConfigConnection, ElasticsearchHit, ElasticsearchQuery, ElasticsearchResponseBody, FacetAttribute, FacetFieldConfig, FilterAttribute, QueryRule, QueryRuleAction, QueryRuleCondition, RequestOptions, SearchRequest, SearchSettingsConfig, Transporter, createClient as default }; |
@@ -46,2 +46,5 @@ "use strict"; | ||
}; | ||
var isNestedFacet = (facet) => { | ||
return typeof facet !== "string" && !!facet.nestedPath; | ||
}; | ||
var getFacetAttribute = (facetAttribute) => { | ||
@@ -59,17 +62,3 @@ return typeof facetAttribute === "string" ? facetAttribute : facetAttribute.attribute; | ||
// src/transformRequest.ts | ||
var createRegexQuery = (queryString) => { | ||
let query = queryString.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | ||
query = query.split("").map((char) => { | ||
if (/[a-z]/.test(char)) { | ||
return `[${char}${char.toUpperCase()}]`; | ||
} | ||
return char; | ||
}).join(""); | ||
query = `${query}.*`; | ||
if (queryString.length > 2) { | ||
query = `([a-zA-Z]+ )+?${query}`; | ||
} | ||
return query; | ||
}; | ||
// src/filters.ts | ||
var transformNumericFilters = (request, config) => { | ||
@@ -85,10 +74,14 @@ const { params = {} } = request; | ||
); | ||
const fieldConfig = getFacet(config.facet_attributes || [], field); | ||
const facetFilterMap = getFacetFilterMap( | ||
config.facet_attributes || [], | ||
config.filter_attributes || [] | ||
); | ||
const facetFilterConfig = facetFilterMap[field]; | ||
if (!match) | ||
return sum; | ||
const getFilter = (operator2, value2) => { | ||
const getFilter = (field2, operator2, value2) => { | ||
if (operator2 === "=") { | ||
return { | ||
term: { | ||
[field]: value2 | ||
[field2]: value2 | ||
} | ||
@@ -101,3 +94,3 @@ }; | ||
term: { | ||
[field]: value2 | ||
[field2]: value2 | ||
} | ||
@@ -110,3 +103,3 @@ } | ||
range: { | ||
[field]: { | ||
[field2]: { | ||
gt: value2 | ||
@@ -119,3 +112,3 @@ } | ||
range: { | ||
[field]: { | ||
[field2]: { | ||
gte: value2 | ||
@@ -128,3 +121,3 @@ } | ||
range: { | ||
[field]: { | ||
[field2]: { | ||
lt: value2 | ||
@@ -137,3 +130,3 @@ } | ||
range: { | ||
[field]: { | ||
[field2]: { | ||
lte: value2 | ||
@@ -146,9 +139,15 @@ } | ||
const esFilter = []; | ||
if (typeof fieldConfig !== "string" && fieldConfig.nestedPath) { | ||
if (facetFilterConfig.nestedPath) { | ||
esFilter.push({ | ||
nested: { | ||
path: fieldConfig.nestedPath, | ||
path: facetFilterConfig.nestedPath, | ||
query: { | ||
bool: { | ||
filter: [getFilter(operator, value)] | ||
filter: [ | ||
getFilter( | ||
facetFilterConfig.nestedPath + "." + facetFilterConfig.field, | ||
operator, | ||
value | ||
) | ||
] | ||
} | ||
@@ -159,3 +158,3 @@ } | ||
} else { | ||
esFilter.push(getFilter(operator, value)); | ||
esFilter.push(getFilter(facetFilterConfig.field, operator, value)); | ||
} | ||
@@ -168,2 +167,14 @@ return [...sum, ...esFilter]; | ||
}; | ||
var getFacetFilterMap = (facets, filters) => { | ||
return [...filters, ...facets].reduce( | ||
(sum, filter) => { | ||
let f = typeof filter === "string" ? { attribute: filter, field: filter, type: "string" } : filter; | ||
return { | ||
...sum, | ||
[f.attribute]: f | ||
}; | ||
}, | ||
{} | ||
); | ||
}; | ||
var transformFacetFilters = (request, config) => { | ||
@@ -175,2 +186,6 @@ const { params = {} } = request; | ||
} | ||
const facetFilterMap = getFacetFilterMap( | ||
config.facet_attributes || [], | ||
config.filter_attributes || [] | ||
); | ||
return facetFilters.reduce((sum, filter) => { | ||
@@ -184,12 +199,11 @@ if (Array.isArray(filter)) { | ||
const [facet, value] = filter2.split(":"); | ||
const facetAttribute = getFacetAttribute(facet); | ||
const facetConfig = getFacet(config.facet_attributes || [], facetAttribute); | ||
const field = typeof facetConfig === "string" ? facetConfig : facetConfig.field; | ||
if (typeof facetConfig !== "string" && isNestedFacet(facetConfig) && facetConfig.nestedPath) { | ||
const facetFilterConfig = facetFilterMap[facet]; | ||
const field = facetFilterConfig.field; | ||
if (isNestedFacet(facetFilterConfig)) { | ||
const nestedFilter = sum2.find((filter3) => { | ||
return filter3.nested && filter3.nested.path === facetConfig.nestedPath; | ||
return filter3.nested && filter3.nested.path === facetFilterConfig.nestedPath; | ||
}); | ||
if (nestedFilter) { | ||
nestedFilter.nested.query.bool.should.push( | ||
termFilter(`${facetConfig.nestedPath}.${facetConfig.field}`, value) | ||
termFilter(`${facetFilterConfig.nestedPath}.${facetFilterConfig.field}`, value) | ||
); | ||
@@ -202,7 +216,10 @@ return sum2; | ||
nested: { | ||
path: facetConfig.nestedPath, | ||
path: facetFilterConfig.nestedPath, | ||
query: { | ||
bool: { | ||
should: [ | ||
termFilter(`${facetConfig.nestedPath}.${facetConfig.field}`, value) | ||
termFilter( | ||
`${facetFilterConfig.nestedPath}.${facetFilterConfig.field}`, | ||
value | ||
) | ||
] | ||
@@ -255,5 +272,47 @@ } | ||
}; | ||
var isNestedFacet = (facet) => { | ||
return typeof facet !== "string" && !!facet.nestedPath; | ||
var transformBaseFilters = (request, config) => { | ||
const { params = {} } = request; | ||
const { filters } = params; | ||
if (!filters || filters === "") { | ||
return []; | ||
} | ||
const filterMap = getFacetFilterMap(config.facet_attributes || [], config.filter_attributes || []); | ||
const regex = /([\w\.\-]+)\:/gi; | ||
const queryString = filters.replace(regex, (match, word) => { | ||
if (!filterMap[word]) { | ||
throw new Error( | ||
`Attribute "${word}" is not defined as an attribute in the facet or filter search settings` | ||
); | ||
} | ||
if (!!filterMap[word].nestedPath) { | ||
throw new Error( | ||
`Attribute "${word}" is a nested field and cannot be used as a filter. Nested fields are supported in facetFilers or numericFilters.` | ||
); | ||
} | ||
return filterMap[word].field + ":"; | ||
}); | ||
return [ | ||
{ | ||
query_string: { | ||
query: queryString | ||
} | ||
} | ||
]; | ||
}; | ||
// src/transformRequest.ts | ||
var createRegexQuery = (queryString) => { | ||
let query = queryString.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | ||
query = query.split("").map((char) => { | ||
if (/[a-z]/.test(char)) { | ||
return `[${char}${char.toUpperCase()}]`; | ||
} | ||
return char; | ||
}).join(""); | ||
query = `${query}.*`; | ||
if (queryString.length > 2) { | ||
query = `([a-zA-Z]+ )+?${query}`; | ||
} | ||
return query; | ||
}; | ||
var getTermAggregation = (facet, size, search) => { | ||
@@ -359,2 +418,3 @@ const searchInclude = search && search.length > 0 ? { include: createRegexQuery(search) } : {}; | ||
...transformNumericFilters(request, config), | ||
...transformBaseFilters(request, config), | ||
...((_a = requestOptions == null ? void 0 : requestOptions.getBaseFilters) == null ? void 0 : _a.call(requestOptions)) || [] | ||
@@ -361,0 +421,0 @@ ]; |
{ | ||
"name": "@searchkit/api", | ||
"version": "4.0.0-next.7", | ||
"version": "4.0.0-next.8", | ||
"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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
158633
1812