-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Refactors MoreLikeThisQueryBuilder and Parser #13486
Refactors MoreLikeThisQueryBuilder and Parser #13486
Conversation
@alexksikes can you rebase this to latest - there are quite some changes here that are now gone |
Relates to elastic#10217 This PR is against the query-refactoring branch. Closes elastic#13486
9c1a75c
to
d722c47
Compare
@s1monw You can take a look now. Thank you. |
@@ -147,7 +204,8 @@ public Item doc(BytesReference doc) { | |||
* Sets to a given artificial document, that is a document that is not present in the index. | |||
*/ | |||
public Item doc(XContentBuilder doc) { | |||
return this.doc(doc.bytes()); | |||
this.doc = doc != null ? doc.bytes() : 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.
I don't think we should allow null here I think this should throw an exception if null is passed in
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.
In fact, given that we require either doc or an index document id, this should really be a constructor and this setter should be removed. That way the user can only specify doc OR id and must specify one of them. We should add null checks in the constructors and throw IllegalArgumentException if nulls are passed in
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.
Also, given that type and index are required if id is specified we should remove the setters for type, id and index and remove the zero-arg constructor for Item so the user is forced to correct initialise the object with either the index, type and id, or the index, type and doc
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.
There is a constructor for doc. However, we do have this method for bwc, so maybe we should keep it?
Concerning null index, or type, this is so that these could be initialized by the search context, and we only have this context when the query is actually being created.
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.
We don't need to keep the constructors/methods for bwc. For the Java API we are able to break backwards compatibility here and we should if it makes the use of the API less trappy (as it does here).
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.
OK then if we are allowed to break bwc, then I much prefer to remove this method. That would simplify things from code and user perspective. A definite +1.
@alexksikes I left some comments. I also think someone else will need to quickly look before its merged as I have no experience of the MLT query |
@colings86 Thanks for the review. I'll address the comments on the next round of review. |
@@ -316,6 +316,18 @@ public void writeStringArrayNullable(@Nullable String[] array) throws IOExceptio | |||
} | |||
} | |||
|
|||
/** | |||
* Writes a string array, for nullable string, writes it as 0 (empty string). | |||
*/ |
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.
Comment seems wrong, as far as I can see the difference to writeStringArrayNullable
is that it writes boolean instead of int in case of null argument.
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.
why is this method needed if we have writeStringArrayNullable
?
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.
That one doesn't seem to differentiate between null and zero-length array, which we need if we e.g. test for equality of queries after serialization. At least that's what I think, @Ale might have had other reasons.
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 exactly the reason. I first tried writeStringArrayNullable
, but then you read back an empty array, not a null array.
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'm not a fan of having both of these methods. The difference between them is too subtle. I think we should instead replace writeStringArrayNullable
with writeOptionalStringArray
as the former is trappy and could easily lead to bugs. Serialisation code should always serialise and de-serialise to the same thing.
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.
+1 should this change be part of this PR? Looks like this is not used anywhere else.
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.
If it's not used anywhere else (or even if its only used in a couple of places) then I would be +1 to doing it in this PR
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.
My bad this is actually used in a couple of places. I'll open an issue on this.
@alexksikes I did a round of reviews, given the amount of complexity in constructing the final MLT query I didn't check the internals of the helper methods moved over from the parsers, maybe those can be simplified later but as far as separating the toQuery/fromXContent I think this is good. Maybe someone else should have final look after you adressed the last comments. |
@cbuescher @colings86 I addressed all the comments. Should I do the package change for the Item class as part of this PR too? Thanks again for the review. |
|
||
@Override | ||
public VersionType readFrom(StreamInput in) throws IOException { | ||
return VersionType.values()[in.readVInt()]; |
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.
can we check the value we are reading here? an assertion would be ok
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeVInt(this.ordinal()); |
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
is unnecessary
I left some minor comments LGTM otherwise |
Relates to #10217
This PR is against the query-refactoring branch.