-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add format support for date range filter and queries
#7821
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/test/java/org/elasticsearch/index/query/IndexQueryParserFilterDateRangeFormatTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.query; | ||
|
|
||
|
|
||
| import org.apache.lucene.search.NumericRangeQuery; | ||
| import org.apache.lucene.search.Query; | ||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.compress.CompressedString; | ||
| import org.elasticsearch.common.inject.Injector; | ||
| import org.elasticsearch.index.mapper.MapperService; | ||
| import org.elasticsearch.index.service.IndexService; | ||
| import org.elasticsearch.test.ElasticsearchSingleNodeTest; | ||
| import org.joda.time.DateTime; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.elasticsearch.common.io.Streams.copyToBytesFromClasspath; | ||
| import static org.elasticsearch.common.io.Streams.copyToStringFromClasspath; | ||
| import static org.hamcrest.Matchers.*; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public class IndexQueryParserFilterDateRangeFormatTests extends ElasticsearchSingleNodeTest { | ||
|
|
||
| private Injector injector; | ||
| private IndexQueryParserService queryParser; | ||
|
|
||
| @Before | ||
| public void setup() throws IOException { | ||
| IndexService indexService = createIndex("test"); | ||
| injector = indexService.injector(); | ||
|
|
||
| MapperService mapperService = indexService.mapperService(); | ||
| String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/mapping.json"); | ||
| mapperService.merge("person", new CompressedString(mapping), true); | ||
| mapperService.documentMapper("person").parse(new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/query/data.json"))); | ||
| queryParser = injector.getInstance(IndexQueryParserService.class); | ||
| } | ||
|
|
||
| private IndexQueryParserService queryParser() throws IOException { | ||
| return this.queryParser; | ||
| } | ||
|
|
||
| @Test | ||
| public void testDateRangeFilterFormat() throws IOException { | ||
| IndexQueryParserService queryParser = queryParser(); | ||
| String query = copyToStringFromClasspath("/org/elasticsearch/index/query/date_range_filter_format.json"); | ||
| queryParser.parse(query).query(); | ||
| // Sadly from NoCacheFilter, we can not access to the delegate filter so we can not check | ||
| // it's the one we are expecting | ||
|
|
||
| // Test Invalid format | ||
| query = copyToStringFromClasspath("/org/elasticsearch/index/query/date_range_filter_format_invalid.json"); | ||
| try { | ||
| queryParser.parse(query).query(); | ||
| fail("A Range Filter with a specific format but with an unexpected date should raise a QueryParsingException"); | ||
| } catch (QueryParsingException e) { | ||
| // We expect it | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testDateRangeQueryFormat() throws IOException { | ||
| IndexQueryParserService queryParser = queryParser(); | ||
| // We test 01/01/2012 from gte and 2030 for lt | ||
| String query = copyToStringFromClasspath("/org/elasticsearch/index/query/date_range_query_format.json"); | ||
| Query parsedQuery = queryParser.parse(query).query(); | ||
| assertThat(parsedQuery, instanceOf(NumericRangeQuery.class)); | ||
|
|
||
| // Min value was 01/01/2012 (dd/MM/yyyy) | ||
| DateTime min = DateTime.parse("2012-01-01T00:00:00.000+00"); | ||
| assertThat(((NumericRangeQuery) parsedQuery).getMin().longValue(), is(min.getMillis())); | ||
|
|
||
| // Max value was 2030 (yyyy) | ||
| DateTime max = DateTime.parse("2030-01-01T00:00:00.000+00"); | ||
| assertThat(((NumericRangeQuery) parsedQuery).getMax().longValue(), is(max.getMillis())); | ||
|
|
||
| // Test Invalid format | ||
| query = copyToStringFromClasspath("/org/elasticsearch/index/query/date_range_query_format_invalid.json"); | ||
| try { | ||
| queryParser.parse(query).query(); | ||
| fail("A Range Query with a specific format but with an unexpected date should raise a QueryParsingException"); | ||
| } catch (QueryParsingException e) { | ||
| // We expect it | ||
| } | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/test/java/org/elasticsearch/index/query/date_range_filter_format.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "constant_score": { | ||
| "filter": { | ||
| "range" : { | ||
| "born" : { | ||
| "gte": "01/01/2012", | ||
| "lt": "2030", | ||
| "format": "dd/MM/yyyy||yyyy" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think it's fine to add such a method to NoCacheFilter
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. Will do but I think it should be in another PR, right?
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 think it's ok to do it here, I don't think it is a big change? If you want to keep both changes isolated, let's just have two commits?
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 ok with that. I should have done it before with d28b8c6#diff-5e91cfb5ed97f20ebbd96bdd077acad9R72 :)