import { nodeTypes } from '../node_types' ; import * as ast from '../ast' ; import { IIndexPattern , KueryNode , IFieldType , LatLon } from '../../..' ; import { LiteralTypeBuildNode } from '../node_types/types' ; export function buildNodeParams ( fieldName , points ) { const fieldNameArg = nodeTypes . literal . buildNode ( fieldName ) ; const args = points . map ( ( point ) => { const latLon = `${ point . lat }, ${ point . lon }` ; return nodeTypes . literal . buildNode ( latLon ) ; } ) ; return { arguments : [ fieldNameArg , ... args ] , } ; } export function toOpenSearchQuery ( node , indexPattern ? , config = { } , context = { } ) { const [ fieldNameArg , ... points ] = 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 = { points : points . map ( ( point ) => { return ast . toOpenSearchQuery ( point , indexPattern , config , context ) ; } ) , } ; if ( field ?. scripted ) { throw new Error ( `Geo polygon query does not support scripted fields` ) ; } return { geo_polygon : { [ fieldName ] : queryParams , ignore_unmapped : true , } , } ; }