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

Allow specifying an exclusive set of fields on ObjectParser #52893

Merged
merged 3 commits into from Mar 3, 2020

Conversation

romseygeek
Copy link
Contributor

ObjectParser allows you to declare a set of required fields, such that at least one
of the set must appear in an xcontent object for it to be valid. This commit adds
the similar concept of a set of exclusive fields, such that at most one of the set
must be present. It also enables required fields on ConstructingObjectParser, and
re-implements PercolateQueryBuilder.fromXContent() to use object parsing as
an example of how this could work.

@romseygeek
Copy link
Contributor Author

One tweak that we could make would be to combine declareRequiredFieldSet and declareExclusiveFieldSet somehow, as they're clearly related and I'm guessing most of the time we'll end up declaring both with identical sets.

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (:Core/Infra/Core)

Copy link
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

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

I wonder about the exception message for duplicate fields. But otherwise it is great!

@@ -302,18 +306,40 @@ public Value parse(XContentParser parser, Value value, Context context) throws I
}
}

// Check if this field is in an exclusive set, if it is then mark
// it as seen. If the set is already marked, then we have a duplicate
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't throw an exception though!

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see. It does, but later. Got it. Maybe update the comment. Not sure.

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 rewrote the comments a bit to make things clearer

* E.g. <code>declareExclusiveFieldSet("foo", "bar");</code> means that only one of 'foo'
* or 'bar' must be present, and if both appear then an exception will be thrown.
*
* Multiple exclusive sets may be declared
Copy link
Member

Choose a reason for hiding this comment

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

I think it might be worth mentioning that it doesn't make foo or bar required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

++

} else if (indexedDocumentId != null) {
queryBuilder = new PercolateQueryBuilder(field, indexedDocumentIndex, indexedDocumentId, indexedDocumentRouting,
indexedDocumentPreference, indexedDocumentVersion);
@SuppressWarnings("unchecked")
Copy link
Member

Choose a reason for hiding this comment

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

I tend to put this on the variable that needs it for extra paranoia.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL you can do this on variables - thanks!

public static PercolateQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;

String field = null;
Copy link
Member

Choose a reason for hiding this comment

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

Nice to see all this go!

@romseygeek
Copy link
Contributor Author

Thanks for the review @nik9000 , I pushed some changes to address your concerns.

Copy link
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

@polyfractal
Copy link
Contributor

I'm totally swamped right now and Nik knows way more about parser stuff than me, so don't feel blocked on my missing review :)

@romseygeek romseygeek merged commit fbd8d79 into elastic:master Mar 3, 2020
@romseygeek romseygeek deleted the objectparser/exclusive-set branch March 3, 2020 09:50
romseygeek added a commit that referenced this pull request Mar 3, 2020
ObjectParser allows you to declare a set of required fields, such that at least one
of the set must appear in an xcontent object for it to be valid. This commit adds
the similar concept of a set of exclusive fields, such that at most one of the set
must be present. It also enables required fields on ConstructingObjectParser, and
re-implements PercolateQueryBuilder.fromXContent() to use object parsing as
an example of how this works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants