UNPKG

dsl-builder

Version:

OpenSearch Query Builder - Extract from OpenSearch Dashboards

88 lines (87 loc) 3.53 kB
"use strict"; /* * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.toOpenSearchQuery = exports.doesKueryExpressionHaveLuceneSyntaxError = exports.fromKueryExpression = exports.fromLiteralExpression = 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 index_1 = require("../node_types/index"); const kuery_syntax_error_1 = require("../kuery_syntax_error"); // @ts-ignore const parseKuery = require('./_generated_/kuery.js').parse; const fromExpression = (expression, parseOptions = {}, parse = parseKuery) => { if (typeof expression === 'undefined') { throw new Error('expression must be a string, got undefined instead'); } return parse(expression, { ...parseOptions, helpers: { nodeTypes: index_1.nodeTypes } }); }; const fromLiteralExpression = (expression, parseOptions = {}) => { return fromExpression(expression, { ...parseOptions, startRule: 'Literal', }, parseKuery); }; exports.fromLiteralExpression = fromLiteralExpression; const fromKueryExpression = (expression, parseOptions = {}) => { try { return fromExpression(expression, parseOptions, parseKuery); } catch (error) { if (error instanceof Error && error.name === 'SyntaxError') { throw new kuery_syntax_error_1.DQLSyntaxError(error, expression); } else { throw error; } } }; exports.fromKueryExpression = fromKueryExpression; const doesKueryExpressionHaveLuceneSyntaxError = (expression) => { try { fromExpression(expression, { errorOnLuceneSyntax: true }, parseKuery); return false; } catch (e) { return e instanceof Error && e.message.startsWith('Lucene'); } }; exports.doesKueryExpressionHaveLuceneSyntaxError = doesKueryExpressionHaveLuceneSyntaxError; /** * @params {String} indexPattern * @params {Object} config - contains the dateFormatTZ * * IndexPattern isn't required, but if you pass one in, we can be more intelligent * about how we craft the queries (e.g. scripted fields) */ const toOpenSearchQuery = (node, indexPattern, config, context) => { if (!node || !node.type || !index_1.nodeTypes[node.type]) { return (0, exports.toOpenSearchQuery)(index_1.nodeTypes.function.buildNode('and', []), indexPattern); } const nodeType = index_1.nodeTypes[node.type]; return nodeType.toOpenSearchQuery(node, indexPattern, config, context); }; exports.toOpenSearchQuery = toOpenSearchQuery;