import { isUndefined } from 'lodash' ; import { migrateFilter } from './migrate_filter' ; import { filterMatchesIndex } from './filter_matches_index' ; import { Filter , cleanFilter , isFilterDisabled } from '../filters' ; import { IIndexPattern } from '../../index_patterns' ; import { handleNestedFilter } from './handle_nested_filter' ; param { boolean } reverse returns { function } const filterNegate = ( reverse ) => ( filter ) => { if ( isUndefined ( filter . meta ) || isUndefined ( filter . meta . negate ) ) { return ! reverse ; } return filter . meta && filter . meta . negate === reverse ; } ; param { Object } filter return { Object } const translateToQuery = ( filter ) => { if ( ! filter ) return ; if ( filter . query ) { return filter . query ; } return filter ; } ; export const buildQueryFromFilters = ( filters = [ ] , indexPattern , ignoreFilterIfFieldNotInIndex = false ) => { filters = filters . filter ( ( filter ) => filter && ! isFilterDisabled ( filter ) ) ; const filtersToOpenSearchQueries = ( negate ) => { return filters . filter ( filterNegate ( negate ) ) . filter ( ( filter ) => ! ignoreFilterIfFieldNotInIndex || filterMatchesIndex ( filter , indexPattern ) ) . map ( ( filter ) => { return migrateFilter ( filter , indexPattern ) ; } ) . map ( ( filter ) => handleNestedFilter ( filter , indexPattern ) ) . map ( translateToQuery ) . map ( cleanFilter ) ; } ; return { must : [ ] , filter : filtersToOpenSearchQueries ( false ) , should : [ ] , must_not : filtersToOpenSearchQueries ( true ) , } ; } ;