Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate deprecated match query syntax #11554

Merged
merged 6 commits into from
May 5, 2017
Merged

Conversation

Bargs
Copy link
Contributor

@Bargs Bargs commented May 1, 2017

Fixes #10653

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.

Copy link
Member

@lukasolson lukasolson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess my concern here is that, as of now, you can still manually edit the query DSL for a given filter by clicking on the "edit" button. Seems to me that it would make sense to also use this migrateFilter in the mapFilter module so that if someone edits the DSL, we actually send what they specify. Thoughts @Bargs?

@@ -0,0 +1,19 @@
import _ from 'lodash';

export function migrateFilter(filter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a unit test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 f2798b5

@Bargs
Copy link
Contributor Author

Bargs commented May 2, 2017

I'm not sure what you mean by "if someone edits the DSL, we actually send what they specify". Can you give me an example?

@lukasolson
Copy link
Member

I mean when they click on the edit button in the filter popover:

image


if (_.isPlainObject(params) && params.type === 'phrase') {
const newFilter = {
match_phrase: _.clone(filter.match, true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about turning this into a single declarative expression like

return {
  match_phrase: {
    [fieldName]: _.omit(params, 'type'),
  },
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 9c75310

const fieldName = Object.keys(filter.match)[0];
const params = filter.match[fieldName];

if (_.isPlainObject(params) && params.type === 'phrase') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be worth extracting this into a separate predicate?

function isMatchPhraseFilter(filter, fieldName) {
  return _.get(filter, ['match', fieldName, 'type']) === 'phrase';
}

Then the condition would be very descriptive as to its purpose:

  if (isMatchPhraseFilter(filter, fieldName)) {
    // ...
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 ff2158a

@Bargs
Copy link
Contributor Author

Bargs commented May 4, 2017

@lukasolson @weltenwort ready for another look

Copy link
Member

@lukasolson lukasolson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Made one minor comment about the test below.

match_all: {}
};
const migratedFilter = migrateFilter(originalFilter);
expect(migratedFilter).to.be(originalFilter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could add to this test

expect(migratedFilter).to.not.eql(newMatchPhraseFilter);

Just to make sure that the filter hasn't been modified.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, would the following be better though?

expect(_.isEqual(migratedFilter, originalFilter)).to.be(true);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way is really fine... I prefer to.eql because when you use the syntax you've suggested, then the error is reported as something like "expected false to be true" instead of "expected { foo: bar } to sort of equal { foo: baz }" or whatever.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I talking more about the idea of checking the equality between migratedFilter and originalFilter instead of checking that migratedFilter is not eql to newMatchPhraseFilter. It seems that the former would catch more errors, but I wasn't sure if I was missing something about what you were suggesting.

As an aside, I agree that the error reporting sucks when using _.isEqual but the problem I have with expect.js's eql function is that it uses loose equality (==) so there are subtle bugs it would miss. Another reason I wish we were using Chai.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bargs Bargs merged commit b984a19 into elastic:master May 5, 2017
Bargs added a commit to Bargs/kibana that referenced this pull request May 5, 2017
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.
Bargs added a commit that referenced this pull request May 5, 2017
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants