import { get , isUndefined } from 'lodash' ; import { getPhraseScript } from '../../filters' ; import { getFields } from './utils/get_fields' ; import { getTimeZoneFromSettings } from '../../utils' ; import { getFullFieldNameNode } from './utils/get_full_field_name_node' ; import { IIndexPattern , KueryNode , IFieldType } from '../../..' ; import * as ast from '../ast' ; import * as literal from '../node_types/literal' ; import * as wildcard from '../node_types/wildcard' ; export function buildNodeParams ( fieldName , value , isPhrase = false ) { if ( isUndefined ( fieldName ) ) { throw new Error ( 'fieldName is a required argument' ) ; } if ( isUndefined ( value ) ) { throw new Error ( 'value is a required argument' ) ; } const fieldNode = typeof fieldName === 'string' ? ast . fromLiteralExpression ( fieldName ) : literal . buildNode ( fieldName ) ; const valueNode = typeof value === 'string' ? ast . fromLiteralExpression ( value ) : literal . buildNode ( value ) ; const isPhraseNode = literal . buildNode ( isPhrase ) ; return { arguments : [ fieldNode , valueNode , isPhraseNode ] , } ; } export function toOpenSearchQuery ( node , indexPattern ? , config = { } , context = { } ) { const { arguments : [ fieldNameArg , valueArg , isPhraseArg ] , } = node ; const fullFieldNameArg = getFullFieldNameNode ( fieldNameArg , indexPattern , context ?. nested ? context . nested . path : undefined ) ; const fieldName = ast . toOpenSearchQuery ( fullFieldNameArg ) ; const value = ! isUndefined ( valueArg ) ? ast . toOpenSearchQuery ( valueArg ) : valueArg ; const type = isPhraseArg . value ? 'phrase' : 'best_fields' ; if ( fullFieldNameArg . value === null ) { if ( valueArg . type === 'wildcard' ) { return { query_string : { query : wildcard . toQueryStringQuery ( valueArg ) , } , } ; } return { multi_match : { type , query : value , lenient : true , } , } ; } const fields = indexPattern ? getFields ( fullFieldNameArg , indexPattern ) : [ ] ; if ( fields && fields . length === 0 ) { fields . push ( { name : ( ast . toOpenSearchQuery ( fullFieldNameArg ) as unknown ) as string , scripted : false , type : '' , } ) ; } const isExistsQuery = valueArg . type === 'wildcard' && ( value as any ) === '*' ; const isAllFieldsQuery = ( fullFieldNameArg . type === 'wildcard' && ( ( fieldName as unknown ) as string ) === '*' ) || ( fields && indexPattern && fields . length === indexPattern . fields . length ) ; const isMatchAllQuery = isExistsQuery && isAllFieldsQuery ; if ( isMatchAllQuery ) { return { match_all : { } } ; } const queries = fields ! . reduce ( ( accumulator , field ) => { const wrapWithNestedQuery = ( query ) => { if ( ! ( fullFieldNameArg . type === 'wildcard' ) || ! get ( field , 'subType.nested' ) || context ?. nested ) { return query ; } else { return { nested : { path : field . subType ! . nested ! . path , query , score_mode : 'none' , } , } ; } } ; if ( field . scripted ) { if ( ! isExistsQuery ) { return [ ... accumulator , { script : { ... getPhraseScript ( field , value as any ) , } , } , ] ; } } else if ( isExistsQuery ) { return [ ... accumulator , wrapWithNestedQuery ( { exists : { field : field . name , } , } ) , ] ; } else if ( valueArg . type === 'wildcard' ) { return [ ... accumulator , wrapWithNestedQuery ( { query_string : { fields : [ field . name ] , query : wildcard . toQueryStringQuery ( valueArg ) , } , } ) , ] ; } else if ( field . type === 'date' ) { const timeZoneParam = config . dateFormatTZ ? { time_zone : getTimeZoneFromSettings ( config ! . dateFormatTZ ) } : { } ; return [ ... accumulator , wrapWithNestedQuery ( { range : { [ field . name ] : { gte : value , lte : value , ... timeZoneParam , } , } , } ) , ] ; } else { const queryType = type === 'phrase' ? 'match_phrase' : 'match' ; return [ ... accumulator , wrapWithNestedQuery ( { [ queryType ] : { [ field . name ] : value , } , } ) , ] ; } } , [ ] ) ; return { bool : { should : queries || [ ] , minimum_should_match : 1 , } , } ; }