import _ from 'lodash' ; import { nodeTypes } from '../node_types' ; import * as ast from '../ast' ; import { IIndexPattern , KueryNode , IFieldType , LatLon } from '../../..' ; export function buildNodeParams ( fieldName , params ) { params = _ . pick ( params , 'topLeft' , 'bottomRight' ) ; const fieldNameArg = nodeTypes . literal . buildNode ( fieldName ) ; const args = _ . map ( params , ( value , key ) => { const latLon = `${ value . lat }, ${ value . lon }` ; return nodeTypes . namedArg . buildNode ( key , latLon ) ; } ) ; return { arguments : [ fieldNameArg , ... args ] , } ; } export function toOpenSearchQuery ( node , indexPattern ? , config = { } , context = { } ) { const [ fieldNameArg , ... args ] = node . arguments ; const fullFieldNameArg = { ... fieldNameArg , value : context ?. nested ? `${ context . nested . path }.${ fieldNameArg . value }` : fieldNameArg . value , } ; const fieldName = nodeTypes . literal . toOpenSearchQuery ( fullFieldNameArg ) as string ; const fieldList = indexPattern ?. fields ?? [ ] ; const field = fieldList . find ( ( fld ) => fld . name === fieldName ) ; const queryParams = args . reduce ( ( acc , arg ) => { const snakeArgName = _ . snakeCase ( arg . name ) ; return { ... acc , [ snakeArgName ] : ast . toOpenSearchQuery ( arg ) , } ; } , { } ) ; if ( field ?. scripted ) { throw new Error ( `Geo bounding box query does not support scripted fields` ) ; } return { geo_bounding_box : { [ fieldName ] : queryParams , ignore_unmapped : true , } , } ; }