-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Allow specifying an exclusive set of fields on ObjectParser #52893
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
Conversation
One tweak that we could make would be to combine |
Pinging @elastic/es-core-infra (:Core/Infra/Core) |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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!
Thanks for the review @nik9000 , I pushed some changes to address your concerns. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM
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 :) |
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.
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 asan example of how this could work.