-
Notifications
You must be signed in to change notification settings - Fork 25.4k
Query refactoring: RangeQueryBuilder and Parser #11108
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
Query refactoring: RangeQueryBuilder and Parser #11108
Conversation
There are two issues with this PR I'd like to get a second opinion on.
|
@cbuescher You can add mappings by constructing your own MapperService and adding types? Right now the dummy parse context you have just has null for the MapperService IIRC, or something effectively like that. |
*/ | ||
public RangeQueryBuilder from(float from) { | ||
this.from = from; | ||
public RangeQueryBuilder from(Object from, boolean includeLower) { |
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 like this simplication, this is a breaking change for java api users though, we need to note this down somewhere, for instance in the migrate_2.0 guide. Actually we could have our own specific file since we don't know yet if this will make it for 2.0.
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.
The 2-arg setter is just an addition, there is still a from(Object from)
that should take care of all the former options (int/long/float/double/String). Those where autoboxed to an Object in the existing code already. Not sure if this breas java api then?
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.
good point, I missed that... I think that is fine then, if the from(Object from)
was already there. thanks!
left a few comments, looks good though! |
@cbuescher to answer your question above:
looks good to me, fail fast fail often ;) if the timezone or format are broken, we want to know asap, let's pay the price for the object creation on the coord node then. |
bca5aeb
to
02c9f9d
Compare
Went through your comments and adressed most of them, rebased on current head of feature branch also. I'd still like to have a look if the |
* @param obj the input object | ||
* @return the same input object or a {@link BytesRef} representation if input was of type string | ||
*/ | ||
public static Object convertToBytesRefIfString(Object obj) { |
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.
protected?
left a couple of minor comments, besides those LGTM though I think it's ready |
c884876
to
a4da5bb
Compare
Added mappings for a field with date type to the base test and extended the RangeQueryBuilderTest to include the code path where date mapper is used. Unfortunately the resulting lucene query is very difficult to access, so I fall back on checking the resulting toString() representation of the query. |
@@ -160,7 +168,7 @@ public void testFromXContent() throws IOException { | |||
public void testToQuery() throws IOException { | |||
testQuery = createTestQueryBuilder(); | |||
QueryParseContext context = createContext(); | |||
context.setMapUnmappedFieldAsString(true); | |||
context.setAllowUnmappedFields(true); |
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 needs to stay cause we want to test unmapped and mapped fields?
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 must be a merge glitch, I think we settled in mapping fields to String by default for now as long as we don't need it otherwise. Will revert that.
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.
but then we would never test the unmapped fields codepath? maybe we should do it just rarely?
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 need to correct myself, we need to set the default to allow unmapped fields, otherwise the String mapper wins over the date mapper we introduced here. Don't understand why, but setAllowUnmappedFields(true) worked with the other tests so far as well.
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.
otherwise the String mapper wins over the date mapper we introduced here
that seems to me like the wrong reason to have it around :)
Which codepaths are we testing now? unmapped fields only, mapped fields only or both?
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.
With the current setup we test unmapped fields when the random setup sets to/from to numeric fields, and mapped fields if we have date string for to/from. Does this make sense of do we need to extend this?
@javanna I went through your last comments one more time and decided to add an additional test mapping for the integer type case. This way we cover all three cases that are possible in RangeQueryBuilder. I'm a little hesitant in these cases with that level of tests because they are likely to break fast with subtle changes in the lucene query code, but maybe thats a good thing. |
Long min = expectedDateLong(queryBuilder.from(), queryBuilder, context); | ||
Long max = expectedDateLong(queryBuilder.to(), queryBuilder, context); | ||
Query expectedQuery = NumericRangeQuery.newLongRange(DATE_FIELD_NAME, min, max, queryBuilder.includeLower(), queryBuilder.includeUpper()); | ||
assertEquals(query.rewrite(null), expectedQuery.rewrite(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.
do we need to rewrite both queries?
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 this case unfortunately its necessary since for reasons I explained above it very tricky to compare the LateParsingQuery, the wrapped query itself has protected contructor. This is the easiest way I found after some digging around.
I went over it again, left a few more comments, it's very close though, just a few minor changes to make I guess |
…r code for String/BytesRef conversion to base class
…d integer and date types
e40dc15
to
a891ca0
Compare
I went through your last comments regarding test class and rebased the whole PR on top of current feature branch. |
public RangeQueryBuilder from(long from) { | ||
this.from = from; | ||
return this; | ||
public String fieldname() { |
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.
s/fieldname/fieldName
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.
sure
left a couple of minor comments, looks great besides those |
@javanna thanks, adressed last couple of changes. |
LGTM |
…est. Split the parse(QueryParseContext ctx) method into a parsing and a query building part, adding Streamable support for serialization and hashCode(), equals() for better testing. This PR also adds test setup for two mappes fields (integer, date) to the BaseQueryTestCase and introduces helper methods for optional conversion of String fields to BytesRef representation that is shared with the already refactored BaseTermQueryBuilder. Relates to #10217 Closes #11108
Merged with feature branch with b99cb21 |
…est. Split the parse(QueryParseContext ctx) method into a parsing and a query building part, adding Streamable support for serialization and hashCode(), equals() for better testing. This PR also adds test setup for two mappes fields (integer, date) to the BaseQueryTestCase and introduces helper methods for optional conversion of String fields to BytesRef representation that is shared with the already refactored BaseTermQueryBuilder. Relates to elastic#10217 Closes elastic#11108
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.