UNPKG

@webiny/api-headless-cms-ddb-es

Version:

DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.

88 lines (87 loc) 2.39 kB
import { normalizeValue } from "@webiny/api-opensearch"; /** * Our default implementation works with the AND operator for the multiple words query string. */ const defaultFullTextSearch = { apply: params => { const { query, term, fields, createFieldPath, prepareTerm } = params; query.must.push({ query_string: { allow_leading_wildcard: true, fields: Object.values(fields).map(createFieldPath), query: `*${prepareTerm(term)}*`, default_operator: "and" } }); } }; const getFullTextSearch = params => { const { fullTextSearches, model } = params; /** * We need to reverse the list, so we can take the last one first - possibility to override existing implementations. */ const reversed = [...fullTextSearches].reverse(); /** * We need to find the most specific implementation for the given model. * Also, we need to use the first possible implementation if the specific one is not found. */ let fallback = null; for (const item of reversed) { const models = item.models || []; /** * We take the first available implementation for the given model. */ if (models.includes(model.modelId)) { return item; } else if (!fallback && models.length === 0) { /** * Then we set the first possible implementation, which has no models defined, as the default one. * It is important not to set the one which has models defined as they are specifically for the targeted model. */ fallback = item; } } return fallback || defaultFullTextSearch; }; export const applyFullTextSearch = params => { const { fullTextSearches, query, term, fields, model } = params; const keys = Object.keys(fields); if (!term || term.length === 0 || keys.length === 0) { return; } const fullTextSearch = getFullTextSearch({ fullTextSearches, model }); fullTextSearch.apply({ model, createFieldPath: field => { if (typeof field.path === "function") { return field.path(term); } else if (field.systemField) { return field.path || field.field.storageId; } return `values.${field.path || field.field.storageId}`; }, fields, query, term, prepareTerm: normalizeValue }); }; //# sourceMappingURL=fullTextSearch.js.map