Permalink
Browse files

Migrate deprecated match query syntax (#11554)

Since I'm considering not doing a full blown re-write of the existing filter model I wanted to get this taken care in a smaller way so we're not taken by surprise when the deprecated syntax disappears in 6.0.
  • Loading branch information...
Bargs committed May 5, 2017
1 parent 815b082 commit b984a1966b1cb30ffbbcc50238be11379b0802e9
@@ -0,0 +1,45 @@
import expect from 'expect.js';
import _ from 'lodash';
import { migrateFilter } from '../_migrate_filter';
describe('migrateFilter', function () {
const oldMatchPhraseFilter = {
match: {
fieldFoo: {
query: 'foobar',
type: 'phrase'
}
}
};
const newMatchPhraseFilter = {
match_phrase: {
fieldFoo: {
query: 'foobar'
}
}
};
// https://github.com/elastic/elasticsearch/pull/17508
it('should migrate match filters of type phrase', function () {
const migratedFilter = migrateFilter(oldMatchPhraseFilter);
expect(_.isEqual(migratedFilter, newMatchPhraseFilter)).to.be(true);
});
it('should not modify the original filter', function () {
const oldMatchPhraseFilterCopy = _.clone(oldMatchPhraseFilter, true);
migrateFilter(oldMatchPhraseFilter);
expect(_.isEqual(oldMatchPhraseFilter, oldMatchPhraseFilterCopy)).to.be(true);
});
it('should return the original filter if no migration is necessary', function () {
const originalFilter = {
match_all: {}
};
const migratedFilter = migrateFilter(originalFilter);
expect(migratedFilter).to.be(originalFilter);
expect(_.isEqual(migratedFilter, originalFilter)).to.be(true);
});
});
@@ -9,6 +9,7 @@ import { FetchProvider } from '../fetch';
import { DecorateQueryProvider } from './_decorate_query';
import { FieldWildcardProvider } from '../../field_wildcard';
import { getHighlightRequestProvider } from '../../highlight';
import { migrateFilter } from './_migrate_filter';
export function AbstractDataSourceProvider(Private, Promise, PromiseEmitter) {
const requestQueue = Private(RequestQueueProvider);
@@ -359,13 +360,15 @@ export function AbstractDataSourceProvider(Private, Promise, PromiseEmitter) {
.filter(filterNegate(false))
.map(translateToQuery)
.map(cleanFilter)
.map(migrateFilter)
)
),
must_not: (
(flatState.filters || [])
.filter(filterNegate(true))
.map(translateToQuery)
.map(cleanFilter)
.map(migrateFilter)
)
}
};
@@ -0,0 +1,22 @@
import _ from 'lodash';
export function migrateFilter(filter) {
if (filter.match) {
const fieldName = Object.keys(filter.match)[0];
if (isMatchPhraseFilter(filter, fieldName)) {
const params = _.get(filter, ['match', fieldName]);
return {
match_phrase: {
[fieldName]: _.omit(params, 'type'),
},
};
}
}
return filter;
}
function isMatchPhraseFilter(filter, fieldName) {
return _.get(filter, ['match', fieldName, 'type']) === 'phrase';
}

0 comments on commit b984a19

Please sign in to comment.