UNPKG

dsl-builder

Version:

OpenSearch Query Builder - Extract from OpenSearch Dashboards

47 lines (46 loc) 1.65 kB
/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Any modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ import { FILTERS, buildPhraseFilter, buildPhrasesFilter, buildRangeFilter, buildExistsFilter, } from '.'; export function buildFilter(indexPattern, field, type, negate, disabled, params, alias, store) { const filter = buildBaseFilter(indexPattern, field, type, params); filter.meta.alias = alias; filter.meta.negate = negate; filter.meta.disabled = disabled; filter.$state = { store }; return filter; } export function buildCustomFilter(indexPatternString, queryDsl, disabled, negate, alias, store) { const meta = { index: indexPatternString, type: FILTERS.CUSTOM, disabled, negate, alias, }; const filter = { ...queryDsl, meta }; filter.$state = { store }; return filter; } function buildBaseFilter(indexPattern, field, type, params) { switch (type) { case 'phrase': return buildPhraseFilter(field, params, indexPattern); case 'phrases': return buildPhrasesFilter(field, params, indexPattern); case 'range': const newParams = { gte: params.from, lt: params.to }; return buildRangeFilter(field, newParams, indexPattern); case 'exists': return buildExistsFilter(field, indexPattern); default: throw new Error(`Unknown filter type: ${type}`); } }