-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/ui/public/courier/data_source/__tests__/_migrate_filter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |