Skip to content

Conversation

cbuescher
Copy link
Member

Split the parse(QueryParseContext ctx) method into a parsing and a query building part, adding Streamable for serialization and hashCode(), equals() for better testing.
Add basic unit test for Builder and Parser.

PR goes agains query-refacoring feature branch.

Copy link
Member

Choose a reason for hiding this comment

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

why package private? shall we add a getter for it instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we only need to access the fields from toQuery(), equals() or from the corresponding test class. I talked to Lee about getters vs. package private, had the feeling we could go with pp here. In this class it would be only one getter, in some of the classes with more fields it's a lot more and just bloats the builders interface I'd say, but I'm also good with getters.

Copy link
Member

Choose a reason for hiding this comment

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

the getter doesn't seem to be needed either. I am leaning towards not adding getters in this round unless they are needed, which they don't seem to. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

we were commenting at the same time :) lemme reply to your last comment. here you are using the package private field to set it, that doesn't make sense as we have a setter for it. There is no reason here to hide anything, this is public api exposed through java API. Lee's concerns were more around toQuery visibility, which is another story I think.
I agree on not adding getters as part of these changes simply because we would have to follow setter with no set prefix convention etc., it's out of the scope of this change. But users might want to retrieve what they set at some point...in general it makes no sense to be able to set stuff that you cant retrieve.

Copy link
Member

Choose a reason for hiding this comment

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

mmm ok looking again... I think I am changing my mind. It feels bad that we use package visibility to retrieve stuff from a test, just because we don't want to add getters that would be useful for users too. Thoughts?

@cbuescher
Copy link
Member Author

Added getter, pulled out test base class and removed usage of jdk 1.8 api.

@cbuescher cbuescher force-pushed the feature/query-refactoring-matchall branch from 2904248 to ff6743d Compare April 20, 2015 14:22
Copy link
Member

Choose a reason for hiding this comment

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

I still wonder if for this simple case we should just to return boost != other.boost , doesn't make sense to call Objects.equals to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed and changed. Should we do so for all primitive types where null checks are not necessary, or what do you suggest for other queries?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thinking about it, using "==" is probably not the best way for float primitives. The internal Float.equals() uses floatToIntBits() just like the hashCode(), maybe we should better use that for consitency?

Copy link
Member

Choose a reason for hiding this comment

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

yes sounds good

@javanna
Copy link
Member

javanna commented Apr 20, 2015

thanks @cbuescher left a few comments

@cbuescher
Copy link
Member Author

Pulled some more of the test setup in the BaseQueryTestCase, I was able to do a generic testFromXContent method but since not all queries will be Streamable from the start and to the readFrom/writeTo won't be in the interface until all classes implemen this I wasn't able to pull that up. Also, tests for toQuery() will probably do different assertions on the produced lucene queries, so I left it in the individual test.

Copy link

Choose a reason for hiding this comment

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

Calling this.equals(null) will yield a NullPointerException - shouldn't this return false?

Copy link
Member

Choose a reason for hiding this comment

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

good point

@cbuescher
Copy link
Member Author

Small change to the equal() method to prevent possible npe. Other than that I went with reverting the base test so that injector is not static again because I'm not sure if that would work when multiple tests are running. Maybe we should revisit that question once we have more tests extending this base test.

Copy link
Member

Choose a reason for hiding this comment

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

shall we rename T to QB ? Also can it simply be <QB extends QueryBuilder & Streamable> ? I think this might help generifying more code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Renaming works for me, but having the type for the testQuery field is good because only then we can use the type dependend getters for assertions in the actual test class. Otherwise we need cast there.

Copy link
Member

Choose a reason for hiding this comment

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

having the type is great, my suggestion was different but it wasn't shown properly... it should be clear now

@cbuescher
Copy link
Member Author

Refactored the base test class even more, but I'm not sure if the test code is now spread too much between the base test and the abstract methods. The serialization test can be generalized even more once we put Streamable in the QueryBuilder interface. Not sure if the "testToQuery()" test is going to be very useful for more complex queries because it seems tricky to do check internals of the constructed lucene queries.

@cbuescher
Copy link
Member Author

@javanna Was able to follow your suggestions on pulling the serialization test up to base test class, this works great now, was also able to use this base class with the other queries I currently have open PRs for (IdsQuery, TermQuery). Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

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

nitpick: can you move the afterClass up close to the beforeClass?

@javanna
Copy link
Member

javanna commented Apr 22, 2015

looks great, left a bunch of minor comments, @dakrone can you have a look too please so we can get it in and move on with other queries?

Copy link
Member

Choose a reason for hiding this comment

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

Change looks good but I would also do: public void assertLuceneQuery(MatchAllQueryBuilder queryBuilder, Query query) throws IOException { and call toQuery in the parent class.just provide the source query as argument and the result of toQuery, then the assert prefix makes more sense too for this method

Copy link
Member

Choose a reason for hiding this comment

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

did you copy paste from my broken code? :) I think we can remove the outer parentheses :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Obviously. They are gone now.

@javanna
Copy link
Member

javanna commented Apr 22, 2015

LGTM

@javanna
Copy link
Member

javanna commented Apr 22, 2015

this got merged right @cbuescher ? if so can you close it?

@cbuescher
Copy link
Member Author

Yes, merged on feature branch with eea7ee5, sorry I didn't close yet.

@cbuescher cbuescher closed this Apr 23, 2015
@clintongormley clintongormley added :Search/Search Search-related issues that do not fall into other categories and removed :Query Refactoring labels Feb 14, 2018
@cbuescher cbuescher deleted the feature/query-refactoring-matchall branch March 20, 2024 20:15
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