Skip to content

Commit

Permalink
Migrate deprecated match query syntax (#11554)
Browse files Browse the repository at this point in the history
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 authored May 5, 2017
1 parent 815b082 commit b984a19
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/ui/public/courier/data_source/__tests__/_migrate_filter.js
Original file line number Diff line number Diff line change
@@ -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);
});

});
3 changes: 3 additions & 0 deletions src/ui/public/courier/data_source/_abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
)
}
};
Expand Down
22 changes: 22 additions & 0 deletions src/ui/public/courier/data_source/_migrate_filter.js
Original file line number Diff line number Diff line change
@@ -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.