UNPKG

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

Version:

DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.

91 lines (89 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyFullTextSearch = void 0; var _apiElasticsearch = require("@webiny/api-elasticsearch"); var _plugins = require("../../../plugins"); /** * Our default plugin is working with the AND operator for the multiple words query string. */ const defaultPlugin = (0, _plugins.createCmsEntryElasticsearchFullTextSearchPlugin)({ apply: params => { const { query, term, fields, prepareTerm } = params; query.must.push({ query_string: { allow_leading_wildcard: true, fields: fields.getAllStoragePaths().map(field => { return `values.${field}`; }), query: `*${prepareTerm(term)}*`, default_operator: "and" } }); } }); defaultPlugin.name = "headless-cms.elasticsearch.entry.fullTextSearch.default"; const getPlugin = params => { const { container, model } = params; /** * We need to reverse the plugins, so we can take the last one first - possibility to override existing plugins. */ const plugins = container.byType(_plugins.CmsEntryElasticsearchFullTextSearchPlugin.type).reverse(); /** * We need to find the most specific plugin for the given model. * Also, we need to use the first possible plugin if the specific one is not found. */ let plugin = null; for (const pl of plugins) { const models = pl.models || []; /** * We take the first available plugin for the given model. */ if (models.includes(model.modelId)) { return pl; } /** * Then we set the first possible plugin, which has no models defined, as the default one. * It is important not to set the plugin which has models defined as they are specifically for the targeted model. */ // else if (!plugin && models.length === 0) { plugin = pl; } } return plugin || defaultPlugin; }; const applyFullTextSearch = params => { const { plugins, query, term, fields, model } = params; if (!term || term.length === 0 || !fields || fields.hasAny() === false) { return; } const plugin = getPlugin({ container: plugins, model }); plugin.apply({ model, fields, query, term, prepareTerm: _apiElasticsearch.normalizeValue }); }; exports.applyFullTextSearch = applyFullTextSearch; //# sourceMappingURL=fullTextSearch.js.map