dsl-builder
Version:
OpenSearch Query Builder - Extract from OpenSearch Dashboards
165 lines (164 loc) • 6.83 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.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toOpenSearchQuery = exports.buildNodeParams = void 0;
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
const lodash_1 = __importDefault(require("lodash"));
const node_types_1 = require("../node_types");
const ast = __importStar(require("../ast"));
const filters_1 = require("../../filters");
const get_fields_1 = require("./utils/get_fields");
const utils_1 = require("../../utils");
const get_full_field_name_node_1 = require("./utils/get_full_field_name_node");
function buildNodeParams(fieldName, params) {
const paramsToMap = lodash_1.default.pick(params, 'gt', 'lt', 'gte', 'lte', 'format');
const fieldNameArg = typeof fieldName === 'string'
? ast.fromLiteralExpression(fieldName)
: node_types_1.nodeTypes.literal.buildNode(fieldName);
const args = lodash_1.default.map(paramsToMap, (value, key) => {
return node_types_1.nodeTypes.namedArg.buildNode(key, value);
});
return {
arguments: [fieldNameArg, ...args],
};
}
exports.buildNodeParams = buildNodeParams;
function toOpenSearchQuery(node, indexPattern, config = {}, context = {}) {
const [fieldNameArg, ...args] = node.arguments;
const fullFieldNameArg = (0, get_full_field_name_node_1.getFullFieldNameNode)(fieldNameArg, indexPattern, (context === null || context === void 0 ? void 0 : context.nested) ? context.nested.path : undefined);
const fields = indexPattern ? (0, get_fields_1.getFields)(fullFieldNameArg, indexPattern) : [];
const namedArgs = extractArguments(args);
const queryParams = lodash_1.default.mapValues(namedArgs, (arg) => {
return ast.toOpenSearchQuery(arg);
});
// If no fields are found in the index pattern we send through the given field name as-is. We do this to preserve
// the behaviour of lucene on dashboards where there are panels based on different index patterns that have different
// fields. If a user queries on a field that exists in one pattern but not the other, the index pattern without the
// field should return no results. It's debatable whether this is desirable, but it's been that way forever, so we'll
// keep things familiar for now.
if (fields && fields.length === 0) {
fields.push({
name: ast.toOpenSearchQuery(fullFieldNameArg),
scripted: false,
type: '',
});
}
const queries = fields.map((field) => {
const wrapWithNestedQuery = (query) => {
// Wildcards can easily include nested and non-nested fields. There isn't a good way to let
// users handle this themselves so we automatically add nested queries in this scenario.
if (!(fullFieldNameArg.type === 'wildcard') ||
!lodash_1.default.get(field, 'subType.nested') ||
context.nested) {
return query;
}
else {
return {
nested: {
path: field.subType.nested.path,
query,
score_mode: 'none',
},
};
}
};
if (field.scripted) {
return {
script: (0, filters_1.getRangeScript)(field, queryParams),
};
}
else if (field.type === 'date') {
const timeZoneParam = config.dateFormatTZ
? { time_zone: (0, utils_1.getTimeZoneFromSettings)(config.dateFormatTZ) }
: {};
return wrapWithNestedQuery({
range: {
[field.name]: {
...queryParams,
...timeZoneParam,
},
},
});
}
return wrapWithNestedQuery({
range: {
[field.name]: queryParams,
},
});
});
return {
bool: {
should: queries,
minimum_should_match: 1,
},
};
}
exports.toOpenSearchQuery = toOpenSearchQuery;
function extractArguments(args) {
const unnamedArgOrder = ['gte', 'lte', 'format'];
const result = args.reduce((acc, arg, index) => {
if (arg.type === 'namedArg') {
acc[arg.name] = arg.value;
}
else {
acc[unnamedArgOrder[index]] = arg;
}
return acc;
}, {});
if ((result.gt && result.gte) || (result.lt && result.lte)) {
throw new Error('range ends cannot be both inclusive and exclusive');
}
return result;
}