UNPKG

@webiny/api-form-builder-so-ddb-es

Version:

[![](https://img.shields.io/npm/dw/@webiny/api-form-builder-so-ddb-es.svg)](https://www.npmjs.com/package/@webiny/api-form-builder-so-ddb-es) [![](https://img.shields.io/npm/v/@webiny/api-form-builder-so-ddb-es.svg)](https://www.npmjs.com/package/@webiny

143 lines (141 loc) 4.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createFormElasticType = exports.createElasticsearchBody = void 0; var _apiElasticsearch = require("@webiny/api-elasticsearch"); var _FormElasticsearchFieldPlugin = require("../../plugins/FormElasticsearchFieldPlugin"); var _FormElasticsearchSortModifierPlugin = require("../../plugins/FormElasticsearchSortModifierPlugin"); var _FormElasticsearchBodyModifierPlugin = require("../../plugins/FormElasticsearchBodyModifierPlugin"); var _FormElasticsearchQueryModifierPlugin = require("../../plugins/FormElasticsearchQueryModifierPlugin"); const createFormElasticType = () => { return "fb.form"; }; exports.createFormElasticType = createFormElasticType; const createInitialQueryValue = () => { return { must: [ /** * We add the __type filtering in the initial query because it must be applied. */ { term: { "__type.keyword": createFormElasticType() } }], must_not: [], should: [], filter: [] }; }; const createElasticsearchQuery = params => { const { plugins, where: initialWhere, fieldPlugins } = params; const query = createInitialQueryValue(); /** * Be aware that, if having more registered operator plugins of same type, the last one will be used. */ const operatorPlugins = (0, _apiElasticsearch.getElasticsearchOperatorPluginsByLocale)(plugins, initialWhere.locale); const where = { ...initialWhere }; /** * !!! IMPORTANT !!! There are few specific cases where we hardcode the query conditions. * * When ES index is shared between tenants, we need to filter records by tenant ID. * No need for the tenant filtering otherwise as each index is for single tenant. */ const sharedIndex = (0, _apiElasticsearch.isSharedElasticsearchIndex)(); if (sharedIndex && where.tenant) { query.must.push({ term: { "tenant.keyword": where.tenant } }); } /** * Remove tenant so it is not applied again later. * Possibly tenant is not defined, but just in case, remove it. */ delete where.tenant; /** * Add the locale to filtering. */ query.must.push({ term: { "locale.keyword": where.locale } }); delete where.locale; /** * We apply other conditions as they are passed via the where value. */ (0, _apiElasticsearch.applyWhere)({ query, where, fields: fieldPlugins, operators: operatorPlugins }); return query; }; const createElasticsearchBody = params => { const { plugins, where, limit: initialLimit, sort: initialSort, after } = params; const fieldPlugins = plugins.byType(_FormElasticsearchFieldPlugin.FormElasticsearchFieldPlugin.type).reduce((acc, plugin) => { acc[plugin.field] = plugin; return acc; }, {}); const limit = (0, _apiElasticsearch.createLimit)(initialLimit, 100); const query = createElasticsearchQuery({ ...params, fieldPlugins }); const sort = (0, _apiElasticsearch.createSort)({ sort: initialSort, fieldPlugins }); const queryModifiers = plugins.byType(_FormElasticsearchQueryModifierPlugin.FormElasticsearchQueryModifierPlugin.type); for (const plugin of queryModifiers) { plugin.modifyQuery({ query, where }); } const sortModifiers = plugins.byType(_FormElasticsearchSortModifierPlugin.FormElasticsearchSortModifierPlugin.type); for (const plugin of sortModifiers) { plugin.modifySort({ sort }); } const body = { query: { constant_score: { filter: { bool: { ...query } } } }, size: limit + 1, search_after: after, sort }; const bodyModifiers = plugins.byType(_FormElasticsearchBodyModifierPlugin.FormElasticsearchBodyModifierPlugin.type); for (const plugin of bodyModifiers) { plugin.modifyBody({ body }); } return body; }; exports.createElasticsearchBody = createElasticsearchBody; //# sourceMappingURL=elasticsearchBody.js.map