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

Refactors MoreLikeThisQueryBuilder and Parser #13486

Conversation

alexksikes
Copy link
Contributor

Relates to #10217

This PR is against the query-refactoring branch.

@s1monw
Copy link
Contributor

s1monw commented Sep 11, 2015

@alexksikes can you rebase this to latest - there are quite some changes here that are now gone

@s1monw s1monw self-assigned this Sep 11, 2015
Relates to elastic#10217

This PR is against the query-refactoring branch.

Closes elastic#13486
@alexksikes alexksikes force-pushed the feature/query-refactoring-more-like-this branch from 9c1a75c to d722c47 Compare September 12, 2015 16:22
@alexksikes
Copy link
Contributor Author

@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;
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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).

Copy link
Contributor Author

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.

@colings86
Copy link
Contributor

@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

@alexksikes
Copy link
Contributor Author

@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).
*/
Copy link
Member

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.

Copy link
Contributor

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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.

@cbuescher
Copy link
Member

@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.

@alexksikes
Copy link
Contributor Author

@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()];
Copy link
Contributor

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());
Copy link
Contributor

Choose a reason for hiding this comment

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

this is unnecessary

@s1monw
Copy link
Contributor

s1monw commented Sep 15, 2015

I left some minor comments LGTM otherwise

alexksikes added a commit that referenced this pull request Sep 16, 2015
Relates to #10217

This PR is against the query-refactoring branch.

Closes #13486
@alexksikes alexksikes removed the review label Sep 16, 2015
@alexksikes alexksikes closed this Sep 16, 2015
@alexksikes alexksikes deleted the feature/query-refactoring-more-like-this branch September 16, 2015 00:20
@clintongormley clintongormley added :Search/Search Search-related issues that do not fall into other categories and removed :Query Refactoring labels Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Search/Search Search-related issues that do not fall into other categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants