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.

@cbuescher
Copy link
Member Author

There are two issues with this PR I'd like to get a second opinion on.

  • In order to stream the timezone and the corresponding format field I chose to use internal String representation and just create the DateMathParser and DateTimeZone objects if needed right before the query is build. However, for early validation of these fields I saw no other way than to create the same objects in validate() already. Would like to hear thoughts about that.
  • In order to test the whole query construction part when there are mappers for the field, I'd have to add mappings to the parseContext. Not sure how to do that in the setup of the Base test though.

@rjernst
Copy link
Member

rjernst commented May 12, 2015

@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) {
Copy link
Member

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.

Copy link
Member Author

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?

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, I missed that... I think that is fine then, if the from(Object from) was already there. thanks!

@javanna
Copy link
Member

javanna commented May 13, 2015

left a few comments, looks good though!

@javanna
Copy link
Member

javanna commented May 13, 2015

@cbuescher to answer your question above:

In order to stream the timezone and the corresponding format field I chose to use internal String representation and just create the DateMathParser and DateTimeZone objects if needed right before the query is build. However, for early validation of these fields I saw no other way than to create the same objects in validate() already. Would like to hear thoughts about that.

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.

@cbuescher cbuescher force-pushed the feature/query-refactoring-rangequery branch from bca5aeb to 02c9f9d Compare May 13, 2015 13:55
@cbuescher
Copy link
Member Author

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 toQuery() branch for the DateFieldMappercase can be tested somehow.

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

Choose a reason for hiding this comment

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

protected?

@javanna
Copy link
Member

javanna commented May 13, 2015

left a couple of minor comments, besides those LGTM though I think it's ready

@cbuescher cbuescher force-pushed the feature/query-refactoring-rangequery branch 2 times, most recently from c884876 to a4da5bb Compare May 15, 2015 13:20
@cbuescher
Copy link
Member Author

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

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?

Copy link
Member Author

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.

Copy link
Member

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?

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

Copy link
Member

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?

Copy link
Member Author

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?

@cbuescher
Copy link
Member Author

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

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?

Copy link
Member Author

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.

@javanna
Copy link
Member

javanna commented May 15, 2015

I went over it again, left a few more comments, it's very close though, just a few minor changes to make I guess

@cbuescher cbuescher force-pushed the feature/query-refactoring-rangequery branch from e40dc15 to a891ca0 Compare May 18, 2015 09:03
@cbuescher
Copy link
Member Author

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() {
Copy link
Member

Choose a reason for hiding this comment

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

s/fieldname/fieldName

Copy link
Member Author

Choose a reason for hiding this comment

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

sure

@javanna
Copy link
Member

javanna commented May 18, 2015

left a couple of minor comments, looks great besides those

@cbuescher
Copy link
Member Author

@javanna thanks, adressed last couple of changes.

@javanna
Copy link
Member

javanna commented May 18, 2015

LGTM

cbuescher added a commit that referenced this pull request May 18, 2015
…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
@cbuescher cbuescher removed the review label May 18, 2015
@cbuescher
Copy link
Member Author

Merged with feature branch with b99cb21

@cbuescher cbuescher closed this May 18, 2015
mute pushed a commit to mute/elasticsearch that referenced this pull request Jul 29, 2015
…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
@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-rangequery 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.

4 participants