import { get , omit } from 'lodash' ; import { getConvertedValueForField } from '../filters' ; import { Filter } from '../filters' ; import { IIndexPattern } from '../../index_patterns' ; export interface DeprecatedMatchPhraseFilter extends Filter { query : { match : { [ field ] : { query : any ; type : 'phrase' ; } ; } ; } ; } function isDeprecatedMatchPhraseFilter ( filter ) { const fieldName = filter . query && filter . query . match && Object . keys ( filter . query . match ) [ 0 ] ; return Boolean ( fieldName && get ( filter , [ 'query' , 'match' , fieldName , 'type' ] ) === 'phrase' ) ; } export function migrateFilter ( filter , indexPattern ? ) { if ( isDeprecatedMatchPhraseFilter ( filter ) ) { const fieldName = Object . keys ( filter . query . match ) [ 0 ] ; const params = get ( filter , [ 'query' , 'match' , fieldName ] ) ; let query = params . query ; if ( indexPattern ) { const field = indexPattern . fields . find ( ( f ) => f . name === fieldName ) ; if ( field ) { query = getConvertedValueForField ( field , params . query ) ; } } return { ... filter , query : { match_phrase : { [ fieldName ] : omit ( { ... params , query , } , 'type' ) , } , } , } ; } return filter ; }