|
9 | 9 | package org.elasticsearch.search.searchafter; |
10 | 10 |
|
11 | 11 | import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; |
| 12 | +import org.elasticsearch.action.index.IndexRequest; |
12 | 13 | import org.elasticsearch.action.index.IndexRequestBuilder; |
13 | 14 | import org.elasticsearch.action.search.SearchPhaseExecutionException; |
14 | 15 | import org.elasticsearch.action.search.SearchRequestBuilder; |
15 | 16 | import org.elasticsearch.action.search.SearchResponse; |
16 | 17 | import org.elasticsearch.action.search.ShardSearchFailure; |
| 18 | +import org.elasticsearch.action.support.WriteRequest; |
| 19 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
17 | 20 | import org.elasticsearch.common.UUIDs; |
| 21 | +import org.elasticsearch.common.settings.Settings; |
18 | 22 | import org.elasticsearch.common.xcontent.XContentBuilder; |
| 23 | +import org.elasticsearch.rest.RestStatus; |
19 | 24 | import org.elasticsearch.search.SearchHit; |
| 25 | +import org.elasticsearch.search.sort.SortBuilders; |
20 | 26 | import org.elasticsearch.search.sort.SortOrder; |
21 | 27 | import org.elasticsearch.test.ESIntegTestCase; |
22 | 28 | import org.hamcrest.Matchers; |
|
30 | 36 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
31 | 37 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
32 | 38 | import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; |
| 39 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFailures; |
| 40 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; |
| 41 | +import static org.hamcrest.Matchers.arrayContaining; |
33 | 42 | import static org.hamcrest.Matchers.containsString; |
34 | 43 | import static org.hamcrest.Matchers.equalTo; |
35 | 44 |
|
@@ -183,6 +192,80 @@ public void testWithSimpleTypes() throws Exception { |
183 | 192 | assertSearchFromWithSortValues(INDEX_NAME, TYPE_NAME, documents, reqSize); |
184 | 193 | } |
185 | 194 |
|
| 195 | + public void testWithCustomFormatSortValueOfDateField() throws Exception { |
| 196 | + final XContentBuilder mappings = jsonBuilder(); |
| 197 | + mappings.startObject().startObject("properties"); |
| 198 | + { |
| 199 | + mappings.startObject("start_date"); |
| 200 | + mappings.field("type", "date"); |
| 201 | + mappings.field("format", "yyyy-MM-dd"); |
| 202 | + mappings.endObject(); |
| 203 | + } |
| 204 | + { |
| 205 | + mappings.startObject("end_date"); |
| 206 | + mappings.field("type", "date"); |
| 207 | + mappings.field("format", "yyyy-MM-dd"); |
| 208 | + mappings.endObject(); |
| 209 | + } |
| 210 | + mappings.endObject().endObject(); |
| 211 | + assertAcked(client().admin().indices().prepareCreate("test") |
| 212 | + .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 3))) |
| 213 | + .addMapping("_doc", mappings)); |
| 214 | + |
| 215 | + |
| 216 | + client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 217 | + .add(new IndexRequest("test").id("1").source("start_date", "2019-03-24", "end_date", "2020-01-21")) |
| 218 | + .add(new IndexRequest("test").id("2").source("start_date", "2018-04-23", "end_date", "2021-02-22")) |
| 219 | + .add(new IndexRequest("test").id("3").source("start_date", "2015-01-22", "end_date", "2022-07-23")) |
| 220 | + .add(new IndexRequest("test").id("4").source("start_date", "2016-02-21", "end_date", "2024-03-24")) |
| 221 | + .add(new IndexRequest("test").id("5").source("start_date", "2017-01-20", "end_date", "2025-05-28")) |
| 222 | + .get(); |
| 223 | + |
| 224 | + SearchResponse resp = client().prepareSearch("test") |
| 225 | + .addSort(SortBuilders.fieldSort("start_date").setFormat("dd/MM/yyyy")) |
| 226 | + .addSort(SortBuilders.fieldSort("end_date").setFormat("yyyy-MM-dd")) |
| 227 | + .setSize(2) |
| 228 | + .get(); |
| 229 | + assertNoFailures(resp); |
| 230 | + assertThat(resp.getHits().getHits()[0].getSortValues(), arrayContaining("22/01/2015", "2022-07-23")); |
| 231 | + assertThat(resp.getHits().getHits()[1].getSortValues(), arrayContaining("21/02/2016", "2024-03-24")); |
| 232 | + |
| 233 | + resp = client().prepareSearch("test") |
| 234 | + .addSort(SortBuilders.fieldSort("start_date").setFormat("dd/MM/yyyy")) |
| 235 | + .addSort(SortBuilders.fieldSort("end_date").setFormat("yyyy-MM-dd")) |
| 236 | + .searchAfter(new String[]{"21/02/2016", "2024-03-24"}) |
| 237 | + .setSize(2) |
| 238 | + .get(); |
| 239 | + assertNoFailures(resp); |
| 240 | + assertThat(resp.getHits().getHits()[0].getSortValues(), arrayContaining("20/01/2017", "2025-05-28")); |
| 241 | + assertThat(resp.getHits().getHits()[1].getSortValues(), arrayContaining("23/04/2018", "2021-02-22")); |
| 242 | + |
| 243 | + resp = client().prepareSearch("test") |
| 244 | + .addSort(SortBuilders.fieldSort("start_date").setFormat("dd/MM/yyyy")) |
| 245 | + .addSort(SortBuilders.fieldSort("end_date")) // it's okay because end_date has the format "yyyy-MM-dd" |
| 246 | + .searchAfter(new String[]{"21/02/2016", "2024-03-24"}) |
| 247 | + .setSize(2) |
| 248 | + .get(); |
| 249 | + assertNoFailures(resp); |
| 250 | + assertThat(resp.getHits().getHits()[0].getSortValues(), arrayContaining("20/01/2017", 1748390400000L)); |
| 251 | + assertThat(resp.getHits().getHits()[1].getSortValues(), arrayContaining("23/04/2018", 1613952000000L)); |
| 252 | + |
| 253 | + SearchRequestBuilder searchRequest = client().prepareSearch("test") |
| 254 | + .addSort(SortBuilders.fieldSort("start_date").setFormat("dd/MM/yyyy")) |
| 255 | + .addSort(SortBuilders.fieldSort("end_date").setFormat("epoch_millis")) |
| 256 | + .searchAfter(new Object[]{"21/02/2016", 1748390400000L}) |
| 257 | + .setSize(2); |
| 258 | + assertNoFailures(searchRequest.get()); |
| 259 | + |
| 260 | + searchRequest = client().prepareSearch("test") |
| 261 | + .addSort(SortBuilders.fieldSort("start_date").setFormat("dd/MM/yyyy")) |
| 262 | + .addSort(SortBuilders.fieldSort("end_date").setFormat("epoch_millis")) // wrong format |
| 263 | + .searchAfter(new Object[]{"21/02/2016", "23/04/2018"}) |
| 264 | + .setSize(2); |
| 265 | + assertFailures(searchRequest, RestStatus.BAD_REQUEST, |
| 266 | + containsString("failed to parse date field [23/04/2018] with format [epoch_millis]")); |
| 267 | + } |
| 268 | + |
186 | 269 | private static class ListComparator implements Comparator<List> { |
187 | 270 | @Override |
188 | 271 | public int compare(List o1, List o2) { |
|
0 commit comments