import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { Filter , fromKueryExpression , luceneStringToDsl , Query , toElasticsearchQuery , } from '@kbn/es-query' ; export function processFilters ( filters , query , controlledBy ? ) { const inputQuery = query . language === 'kuery' ? toElasticsearchQuery ( fromKueryExpression ( query . query as string ) ) : luceneStringToDsl ( query . query ) ; const must = [ inputQuery ] ; const mustNot = [ ] ; for ( const filter of filters ) { if ( filter . meta . disabled || ( controlledBy !== undefined && filter . meta . controlledBy === controlledBy ) ) continue ; const { meta : { negate , type , key : fieldName } , } = filter ; let filterQuery = filter . query ; if ( filterQuery === undefined && type === 'exists' ) { filterQuery = { exists : { field : fieldName , } , } ; } if ( filterQuery ) { if ( negate ) { mustNot . push ( filterQuery ) ; } else { must . push ( filterQuery ) ; } } } return { bool : { must , must_not : mustNot , } , } ; }