import { nodeTypes } from '../node_types/index' ; import { DQLSyntaxError } from '../kuery_syntax_error' ; import { KueryNode , DslQuery , KueryParseOptions } from '../types' ; import { IIndexPattern } from '../../../index_patterns/types' ; import { parse as parseKuery } from './_generated_/kuery' ; import { JsonObject } from '../../../../../opensearch_dashboards_utils/common' ; const fromExpression = ( expression , parseOptions = { } , parse = parseKuery ) : KueryNode => { if ( typeof expression === 'undefined' ) { throw new Error ( 'expression must be a string, got undefined instead' ) ; } return parse ( expression , { ... parseOptions , helpers : { nodeTypes } } ) ; } ; export const fromLiteralExpression = ( expression , parseOptions = { } ) : KueryNode => { return fromExpression ( expression , { ... parseOptions , startRule : 'Literal' , } , parseKuery ) ; } ; export const fromKueryExpression = ( expression , parseOptions = { } ) : KueryNode => { try { return fromExpression ( expression , parseOptions , parseKuery ) ; } catch ( error ) { if ( error . name === 'SyntaxError' ) { throw new DQLSyntaxError ( error , expression ) ; } else { throw error ; } } } ; export const doesKueryExpressionHaveLuceneSyntaxError = ( expression ) : boolean => { try { fromExpression ( expression , { errorOnLuceneSyntax : true } , parseKuery ) ; return false ; } catch ( e ) { return e . message . startsWith ( 'Lucene' ) ; } } ; params params export const toOpenSearchQuery = ( node , indexPattern ? , config ? , context ? ) : JsonObject => { if ( ! node || ! node . type || ! nodeTypes [ node . type ] ) { return toOpenSearchQuery ( nodeTypes . function . buildNode ( 'and' , [ ] ) , indexPattern ) ; } const nodeType = ( nodeTypes [ node . type ] as unknown ) as any ; return nodeType . toOpenSearchQuery ( node , indexPattern , config , context ) ; } ;