import { Filter , FilterMeta } from './meta_filter' ; import { getPhraseScript } from './phrase_filter' ; import { FILTERS } from './index' ; import { IIndexPattern , IFieldType } from '../../index_patterns' ; export type PhrasesFilterMeta = FilterMeta & { params : string [ ] ; field ? : string ; } ; export type PhrasesFilter = Filter & { meta : PhrasesFilterMeta ; } ; export const isPhrasesFilter = ( filter ) : filter is PhrasesFilter => filter ?. meta ?. type === FILTERS . PHRASES ; export const getPhrasesFilterField = ( filter ) => { return filter . meta . key ; } ; export const buildPhrasesFilter = ( field , params , indexPattern ) => { const index = indexPattern . id ; const type = FILTERS . PHRASES ; const key = field . name ; const format = ( f , value ) => f && f . format && f . format . convert ? f . format . convert ( value ) : value ; const value = params . map ( ( v ) => format ( field , v ) ) . join ( ', ' ) ; let should ; if ( field . scripted ) { should = params . map ( ( v ) => ( { script : getPhraseScript ( field , v ) , } ) ) ; } else { should = params . map ( ( v ) => ( { match_phrase : { [ field . name ] : v , } , } ) ) ; } return { meta : { index , type , key , value , params } , query : { bool : { should , minimum_should_match : 1 , } , } , } as PhrasesFilter ; } ;