|
42 | 42 | import org.opensearch.action.bulk.BulkRequestBuilder; |
43 | 43 | import org.opensearch.action.index.IndexRequestBuilder; |
44 | 44 | import org.opensearch.action.search.SearchPhaseExecutionException; |
| 45 | +import org.opensearch.action.search.SearchRequestBuilder; |
45 | 46 | import org.opensearch.action.search.SearchResponse; |
46 | 47 | import org.opensearch.action.search.ShardSearchFailure; |
47 | 48 | import org.opensearch.cluster.metadata.IndexMetadata; |
|
90 | 91 | import static org.opensearch.script.MockScriptPlugin.NAME; |
91 | 92 | import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING; |
92 | 93 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 94 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertFailures; |
93 | 95 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertFirstHit; |
94 | 96 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; |
95 | 97 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertNoFailures; |
@@ -919,7 +921,7 @@ public void testSortMissingNumbers() throws Exception { |
919 | 921 | client().prepareIndex("test") |
920 | 922 | .setId("3") |
921 | 923 | .setSource( |
922 | | - jsonBuilder().startObject().field("id", "3").field("i_value", 2).field("d_value", 2.2).field("u_value", 2).endObject() |
| 924 | + jsonBuilder().startObject().field("id", "3").field("i_value", 2).field("d_value", 2.2).field("u_value", 3).endObject() |
923 | 925 | ) |
924 | 926 | .get(); |
925 | 927 |
|
@@ -964,6 +966,18 @@ public void testSortMissingNumbers() throws Exception { |
964 | 966 | assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1")); |
965 | 967 | assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3")); |
966 | 968 |
|
| 969 | + logger.info("--> sort with custom missing value"); |
| 970 | + searchResponse = client().prepareSearch() |
| 971 | + .setQuery(matchAllQuery()) |
| 972 | + .addSort(SortBuilders.fieldSort("i_value").order(SortOrder.ASC).missing(randomBoolean() ? 1 : "1")) |
| 973 | + .get(); |
| 974 | + assertNoFailures(searchResponse); |
| 975 | + |
| 976 | + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(3L)); |
| 977 | + assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); |
| 978 | + assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2")); |
| 979 | + assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3")); |
| 980 | + |
967 | 981 | // FLOAT |
968 | 982 | logger.info("--> sort with no missing (same as missing _last)"); |
969 | 983 | searchResponse = client().prepareSearch() |
@@ -1001,6 +1015,18 @@ public void testSortMissingNumbers() throws Exception { |
1001 | 1015 | assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1")); |
1002 | 1016 | assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3")); |
1003 | 1017 |
|
| 1018 | + logger.info("--> sort with custom missing value"); |
| 1019 | + searchResponse = client().prepareSearch() |
| 1020 | + .setQuery(matchAllQuery()) |
| 1021 | + .addSort(SortBuilders.fieldSort("d_value").order(SortOrder.ASC).missing(randomBoolean() ? 1.1 : "1.1")) |
| 1022 | + .get(); |
| 1023 | + assertNoFailures(searchResponse); |
| 1024 | + |
| 1025 | + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(3L)); |
| 1026 | + assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); |
| 1027 | + assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2")); |
| 1028 | + assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3")); |
| 1029 | + |
1004 | 1030 | // UNSIGNED_LONG |
1005 | 1031 | logger.info("--> sort with no missing (same as missing _last)"); |
1006 | 1032 | searchResponse = client().prepareSearch() |
@@ -1037,6 +1063,24 @@ public void testSortMissingNumbers() throws Exception { |
1037 | 1063 | assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("2")); |
1038 | 1064 | assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("1")); |
1039 | 1065 | assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3")); |
| 1066 | + |
| 1067 | + logger.info("--> sort with custom missing value"); |
| 1068 | + searchResponse = client().prepareSearch() |
| 1069 | + .setQuery(matchAllQuery()) |
| 1070 | + .addSort(SortBuilders.fieldSort("u_value").order(SortOrder.ASC).missing(randomBoolean() ? 2 : "2")) |
| 1071 | + .get(); |
| 1072 | + assertNoFailures(searchResponse); |
| 1073 | + |
| 1074 | + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(3L)); |
| 1075 | + assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1")); |
| 1076 | + assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2")); |
| 1077 | + assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3")); |
| 1078 | + |
| 1079 | + logger.info("--> sort with negative missing value"); |
| 1080 | + SearchRequestBuilder searchRequestBuilder = client().prepareSearch() |
| 1081 | + .setQuery(matchAllQuery()) |
| 1082 | + .addSort(SortBuilders.fieldSort("u_value").order(SortOrder.ASC).missing(randomBoolean() ? -1 : "-1")); |
| 1083 | + assertFailures(searchRequestBuilder, RestStatus.BAD_REQUEST, containsString("Value [-1] is out of range for an unsigned long")); |
1040 | 1084 | } |
1041 | 1085 |
|
1042 | 1086 | public void testSortMissingNumbersMinMax() throws Exception { |
|
0 commit comments