dsl-builder-test
Version:
OpenSearch Query Builder - Extract from OpenSearch Dashboards
50 lines (49 loc) • 1.75 kB
JavaScript
/*
* 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 { getPhraseScript } from './phrase_filter';
import { FILTERS } from './index';
export const isPhrasesFilter = (filter) => { var _a; return ((_a = filter === null || filter === void 0 ? void 0 : filter.meta) === null || _a === void 0 ? void 0 : _a.type) === FILTERS.PHRASES; };
export const getPhrasesFilterField = (filter) => {
// Phrases is a newer filter type that has always been created via a constructor that ensures
// `meta.key` is set to the field name
return filter.meta.key;
};
// Creates a filter where the given field matches one or more of the given values
// params should be an array of values
export const buildPhrasesFilter = (field, params, indexPattern) => {
const index = indexPattern.id;
const type = FILTERS.PHRASES;
const key = field.name;
const format = (f, value) => f && f.format && f.format.convert ? f.format.convert(value) : value;
const value = params.map((v) => format(field, v)).join(', ');
let should;
if (field.scripted) {
should = params.map((v) => ({
script: getPhraseScript(field, v),
}));
}
else {
should = params.map((v) => ({
match_phrase: {
[field.name]: v,
},
}));
}
return {
meta: { index, type, key, value, params },
query: {
bool: {
should,
minimum_should_match: 1,
},
},
};
};