-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Fix sort on nanosecond date fields with missing values #74760
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e9a86a8
Fix sort on nanosecond date fields with missing values
20a9c44
Merge branch 'master' into fix-73763
elasticmachine c690836
add tiebreaker to tests
ab95f0b
Merge branch 'master' into fix-73763
6ad0f23
Merge branch 'master' into fix-73763
97cbefa
move logic to LongValuesComparator#missingObject
9037428
checkstyle
8c6df48
iter
56eccbb
Merge branch 'master' into fix-73763
elasticmachine 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -868,6 +868,146 @@ public void testSortMissingStrings() throws IOException { | |
| assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3")); | ||
| } | ||
|
|
||
| public void testSortMissingDates() throws IOException { | ||
| for (String type : List.of("date", "date_nanos")) { | ||
| String index = "test_" + type; | ||
| assertAcked( | ||
| prepareCreate(index).setMapping( | ||
| XContentFactory.jsonBuilder() | ||
| .startObject() | ||
| .startObject("_doc") | ||
| .startObject("properties") | ||
| .startObject("mydate") | ||
| .field("type", type) | ||
| .endObject() | ||
| .endObject() | ||
| .endObject() | ||
| .endObject() | ||
| ) | ||
| ); | ||
| ensureGreen(); | ||
| client().prepareIndex(index).setId("1").setSource("mydate", "2021-01-01").get(); | ||
| client().prepareIndex(index).setId("2").setSource("mydate", "2021-02-01").get(); | ||
| client().prepareIndex(index).setId("3").setSource("other_field", "value").get(); | ||
|
|
||
| refresh(); | ||
|
|
||
| for (boolean withFormat : List.of(true, false)) { | ||
| String format = null; | ||
| if (withFormat) { | ||
| format = type.equals("date") ? "strict_date_optional_time" : "strict_date_optional_time_nanos"; | ||
| } | ||
|
|
||
| SearchResponse searchResponse = client().prepareSearch(index) | ||
| .addSort(SortBuilders.fieldSort("mydate").order(SortOrder.ASC).setFormat(format)) | ||
| .get(); | ||
| assertHitsInOrder(searchResponse, new String[] { "1", "2", "3" }); | ||
|
|
||
| searchResponse = client().prepareSearch(index) | ||
| .addSort(SortBuilders.fieldSort("mydate").order(SortOrder.ASC).missing("_first").setFormat(format)) | ||
| .get(); | ||
| assertHitsInOrder(searchResponse, new String[] { "3", "1", "2" }); | ||
|
|
||
| searchResponse = client().prepareSearch(index) | ||
| .addSort(SortBuilders.fieldSort("mydate").order(SortOrder.DESC).setFormat(format)) | ||
| .get(); | ||
| assertHitsInOrder(searchResponse, new String[] { "2", "1", "3" }); | ||
|
|
||
| searchResponse = client().prepareSearch(index) | ||
| .addSort(SortBuilders.fieldSort("mydate").order(SortOrder.DESC).missing("_first").setFormat(format)) | ||
| .get(); | ||
| assertHitsInOrder(searchResponse, new String[] { "3", "2", "1" }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sort across two indices with both "date" and "date_nanos" type using "numeric_type" set to "date_nanos" | ||
| */ | ||
| public void testSortMissingDatesMixedTypes() throws IOException { | ||
| for (String type : List.of("date", "date_nanos")) { | ||
| String index = "test_" + type; | ||
| assertAcked( | ||
| prepareCreate(index).setMapping( | ||
| XContentFactory.jsonBuilder() | ||
| .startObject() | ||
| .startObject("_doc") | ||
| .startObject("properties") | ||
| .startObject("mydate") | ||
| .field("type", type) | ||
| .endObject() | ||
| .endObject() | ||
| .endObject() | ||
| .endObject() | ||
| ) | ||
| ); | ||
|
|
||
| } | ||
| ensureGreen(); | ||
|
|
||
| client().prepareIndex("test_date").setId("1").setSource("mydate", "2021-01-01").get(); | ||
| client().prepareIndex("test_date").setId("2").setSource("mydate", "2021-02-01").get(); | ||
| client().prepareIndex("test_date").setId("3").setSource("other_field", 1).get(); | ||
| client().prepareIndex("test_date_nanos").setId("4").setSource("mydate", "2021-03-01").get(); | ||
| client().prepareIndex("test_date_nanos").setId("5").setSource("mydate", "2021-04-01").get(); | ||
| client().prepareIndex("test_date_nanos").setId("6").setSource("other_field", 2).get(); | ||
| refresh(); | ||
|
|
||
| for (boolean withFormat : List.of(true, false)) { | ||
| String format = null; | ||
| if (withFormat) { | ||
| format = "strict_date_optional_time_nanos"; | ||
| } | ||
|
|
||
| String index = "test*"; | ||
| SearchResponse searchResponse = client().prepareSearch(index) | ||
| .addSort(SortBuilders.fieldSort("mydate").order(SortOrder.ASC).setFormat(format).setNumericType("date_nanos")) | ||
| .addSort(SortBuilders.fieldSort("other_field").order(SortOrder.ASC)) | ||
| .get(); | ||
| assertHitsInOrder(searchResponse, new String[] { "1", "2", "4", "5", "3", "6" }); | ||
|
|
||
| searchResponse = client().prepareSearch(index) | ||
| .addSort( | ||
| SortBuilders.fieldSort("mydate") | ||
| .order(SortOrder.ASC) | ||
| .missing("_first") | ||
| .setFormat(format) | ||
| .setNumericType("date_nanos") | ||
| ) | ||
| .addSort(SortBuilders.fieldSort("other_field").order(SortOrder.ASC)) | ||
| .get(); | ||
| assertHitsInOrder(searchResponse, new String[] { "3", "6", "1", "2", "4", "5" }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
|
|
||
| searchResponse = client().prepareSearch(index) | ||
| .addSort(SortBuilders.fieldSort("mydate").order(SortOrder.DESC).setFormat(format).setNumericType("date_nanos")) | ||
| .addSort(SortBuilders.fieldSort("other_field").order(SortOrder.ASC)) | ||
| .get(); | ||
| assertHitsInOrder(searchResponse, new String[] { "5", "4", "2", "1", "3", "6" }); | ||
|
|
||
| searchResponse = client().prepareSearch(index) | ||
| .addSort( | ||
| SortBuilders.fieldSort("mydate") | ||
| .order(SortOrder.DESC) | ||
| .missing("_first") | ||
| .setFormat(format) | ||
| .setNumericType("date_nanos") | ||
| ) | ||
| .addSort(SortBuilders.fieldSort("other_field").order(SortOrder.ASC)) | ||
| .get(); | ||
| assertHitsInOrder(searchResponse, new String[] { "3", "6", "5", "4", "2", "1" }); | ||
| } | ||
| } | ||
|
|
||
| private void assertHitsInOrder(SearchResponse response, String[] expectedIds) { | ||
| SearchHit[] hits = response.getHits().getHits(); | ||
| assertEquals(expectedIds.length, hits.length); | ||
| int i = 0; | ||
| for (String id : expectedIds) { | ||
| assertEquals(id, hits[i].getId()); | ||
| i++; | ||
| } | ||
| } | ||
|
|
||
| public void testIgnoreUnmapped() throws Exception { | ||
| createIndex("test"); | ||
|
|
||
|
|
||
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
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
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.
A tiebreaker should be used to ensure that
3is always sorted before6(same value otherwise).