Skip to content

Commit b40d72f

Browse files
committed
Update IndexSortSettingsTests
1 parent 0585741 commit b40d72f

File tree

3 files changed

+129
-5
lines changed

3 files changed

+129
-5
lines changed

server/src/test/java/org/elasticsearch/index/IndexSettingsTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ public static IndexMetadata newIndexMeta(String name, Settings indexSettings) {
268268
return IndexMetadata.builder(name).settings(indexSettings(IndexVersion.current(), 1, 1).put(indexSettings)).build();
269269
}
270270

271+
public static IndexMetadata newIndexMeta(String name, Settings indexSettings, IndexVersion indexVersion) {
272+
return IndexMetadata.builder(name).settings(indexSettings(indexVersion, 1, 1).put(indexSettings)).build();
273+
}
274+
271275
public void testUpdateDurability() {
272276
IndexMetadata metadata = newIndexMeta(
273277
"index",

server/src/test/java/org/elasticsearch/index/IndexSortSettingsTests.java

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ private static IndexSettings indexSettings(Settings settings) {
5050
return new IndexSettings(newIndexMeta("test", settings), Settings.EMPTY);
5151
}
5252

53+
private static IndexSettings indexSettings(Settings settings, IndexVersion indexVersion) {
54+
return new IndexSettings(newIndexMeta("test", settings, indexVersion), Settings.EMPTY);
55+
}
56+
5357
public void testNoIndexSort() {
5458
IndexSettings indexSettings = indexSettings(Settings.EMPTY);
5559
assertFalse(indexSettings.getIndexSortConfig().hasIndexSort());
@@ -230,34 +234,150 @@ public void testSortMissingValueDateNanoFieldPre714() {
230234
}
231235
}
232236

233-
public void testTimeSeriesMode() {
237+
public void testLegacyTimeSeriesMode() {
234238
IndexSettings indexSettings = indexSettings(
235239
Settings.builder()
236240
.put(IndexSettings.MODE.getKey(), "time_series")
237241
.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "some_dimension")
238242
.put(IndexSettings.TIME_SERIES_START_TIME.getKey(), "2021-04-28T00:00:00Z")
239243
.put(IndexSettings.TIME_SERIES_END_TIME.getKey(), "2021-04-29T00:00:00Z")
240-
.build()
244+
.build(),
245+
IndexVersions.UPGRADE_TO_LUCENE_10_2_2
241246
);
242247
Sort sort = buildIndexSort(indexSettings, TimeSeriesIdFieldMapper.FIELD_TYPE, new DateFieldMapper.DateFieldType("@timestamp"));
243248
assertThat(sort.getSort(), arrayWithSize(2));
244249
assertThat(sort.getSort()[0].getField(), equalTo("_tsid"));
245250
assertThat(sort.getSort()[1].getField(), equalTo("@timestamp"));
246251
}
247252

248-
public void testTimeSeriesModeNoTimestamp() {
253+
public void testLegacyTimeSeriesModeNoTimestamp() {
249254
IndexSettings indexSettings = indexSettings(
250255
Settings.builder()
251256
.put(IndexSettings.MODE.getKey(), "time_series")
252257
.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "some_dimension")
253258
.put(IndexSettings.TIME_SERIES_START_TIME.getKey(), "2021-04-28T00:00:00Z")
254259
.put(IndexSettings.TIME_SERIES_END_TIME.getKey(), "2021-04-29T00:00:00Z")
255-
.build()
260+
.build(),
261+
IndexVersions.UPGRADE_TO_LUCENE_10_2_2
256262
);
257263
Exception e = expectThrows(IllegalArgumentException.class, () -> buildIndexSort(indexSettings, TimeSeriesIdFieldMapper.FIELD_TYPE));
258264
assertThat(e.getMessage(), equalTo("unknown index sort field:[@timestamp] required by [index.mode=time_series]"));
259265
}
260266

267+
public void testTimeSeriesMode() {
268+
IndexSettings indexSettings = indexSettings(
269+
Settings.builder()
270+
.put(IndexSettings.MODE.getKey(), "time_series")
271+
.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "some_dimension")
272+
.put(IndexSettings.TIME_SERIES_START_TIME.getKey(), "2021-04-28T00:00:00Z")
273+
.put(IndexSettings.TIME_SERIES_END_TIME.getKey(), "2021-04-29T00:00:00Z")
274+
.build()
275+
);
276+
assertFalse(indexSettings.getIndexSortConfig().hasIndexSort());
277+
}
278+
279+
public void testLegacyLogsdbIndexSortWithArrays() {
280+
Settings settings = Settings.builder()
281+
.put(IndexSettings.MODE.getKey(), "logsdb")
282+
.putList("index.sort.field", "field1", "field2")
283+
.putList("index.sort.order", "asc", "desc")
284+
.putList("index.sort.missing", "_last", "_first")
285+
.build();
286+
IndexSettings indexSettings = indexSettings(settings, IndexVersions.UPGRADE_TO_LUCENE_10_2_2);
287+
IndexSortConfig config = indexSettings.getIndexSortConfig();
288+
assertTrue(config.hasIndexSort());
289+
assertThat(config.sortSpecs.length, equalTo(2));
290+
291+
assertThat(config.sortSpecs[0].field, equalTo("field1"));
292+
assertThat(config.sortSpecs[1].field, equalTo("field2"));
293+
assertThat(config.sortSpecs[0].order, equalTo(SortOrder.ASC));
294+
assertThat(config.sortSpecs[1].order, equalTo(SortOrder.DESC));
295+
assertThat(config.sortSpecs[0].missingValue, equalTo("_last"));
296+
assertThat(config.sortSpecs[1].missingValue, equalTo("_first"));
297+
assertNull(config.sortSpecs[0].mode);
298+
assertNull(config.sortSpecs[1].mode);
299+
}
300+
301+
public void testLegacyLogsdbInvalidIndexSortOrder() {
302+
final Settings settings = Settings.builder()
303+
.put(IndexSettings.MODE.getKey(), "logsdb")
304+
.putList("index.sort.order", new String[] { "asc", "desc" })
305+
.build();
306+
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> indexSettings(settings));
307+
assertThat(exc.getMessage(), containsString("index.sort.field:[] index.sort.order:[asc, desc], size mismatch"));
308+
}
309+
310+
public void testLegacyLogsdbInvalidIndexSortMode() {
311+
final Settings settings = Settings.builder()
312+
.put(IndexSettings.MODE.getKey(), "logsdb")
313+
.putList("index.sort.mode", new String[] { "max" })
314+
.build();
315+
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> indexSettings(settings));
316+
assertThat(exc.getMessage(), containsString("index.sort.field:[] index.sort.mode:[max], size mismatch"));
317+
}
318+
319+
public void testLegacyLogsdbInvalidIndexSortMissing() {
320+
final Settings settings = Settings.builder()
321+
.put(IndexSettings.MODE.getKey(), "logsdb")
322+
.putList("index.sort.missing", new String[] { "_last", "_last" })
323+
.build();
324+
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> indexSettings(settings));
325+
assertThat(exc.getMessage(), containsString("index.sort.field:[] index.sort.missing:[_last, _last], size mismatch"));
326+
}
327+
328+
public void testLegacyLogsdbIndexSortWithHostname() {
329+
Settings settings = Settings.builder()
330+
.put(IndexSettings.MODE.getKey(), "logsdb")
331+
.put(IndexSettings.LOGSDB_SORT_ON_HOST_NAME.getKey(), true)
332+
.build();
333+
IndexSettings indexSettings = indexSettings(settings, IndexVersions.UPGRADE_TO_LUCENE_10_2_2);
334+
IndexSortConfig config = indexSettings.getIndexSortConfig();
335+
assertTrue(config.hasIndexSort());
336+
assertThat(config.sortSpecs.length, equalTo(2));
337+
338+
assertThat(config.sortSpecs[0].field, equalTo("host.name"));
339+
assertThat(config.sortSpecs[1].field, equalTo("@timestamp"));
340+
assertThat(config.sortSpecs[0].order, equalTo(SortOrder.ASC));
341+
assertThat(config.sortSpecs[1].order, equalTo(SortOrder.DESC));
342+
assertThat(config.sortSpecs[0].missingValue, equalTo("_last"));
343+
assertNull(config.sortSpecs[1].missingValue);
344+
assertThat(config.sortSpecs[0].mode, equalTo(MultiValueMode.MIN));
345+
assertNull(config.sortSpecs[1].mode);
346+
}
347+
348+
public void testLegacyLogsdbIndexSortTimestampOnly() {
349+
Settings settings = Settings.builder()
350+
.put(IndexSettings.MODE.getKey(), "logsdb")
351+
.put(IndexSettings.LOGSDB_SORT_ON_HOST_NAME.getKey(), false)
352+
.build();
353+
IndexSettings indexSettings = indexSettings(settings, IndexVersions.UPGRADE_TO_LUCENE_10_2_2);
354+
IndexSortConfig config = indexSettings.getIndexSortConfig();
355+
assertTrue(config.hasIndexSort());
356+
assertThat(config.sortSpecs.length, equalTo(1));
357+
358+
assertThat(config.sortSpecs[0].field, equalTo("@timestamp"));
359+
assertThat(config.sortSpecs[0].order, equalTo(SortOrder.DESC));
360+
assertNull(config.sortSpecs[0].missingValue);
361+
assertNull(config.sortSpecs[0].mode);
362+
}
363+
364+
public void testLegacyLogsdbIndexSortTimestampBWC() {
365+
Settings settings = Settings.builder().put(IndexSettings.MODE.getKey(), "logsdb").build();
366+
IndexSettings indexSettings = indexSettings(settings, IndexVersions.UPGRADE_TO_LUCENE_10_0_0);
367+
IndexSortConfig config = indexSettings.getIndexSortConfig();
368+
assertTrue(config.hasIndexSort());
369+
assertThat(config.sortSpecs.length, equalTo(2));
370+
371+
assertThat(config.sortSpecs[0].field, equalTo("host.name"));
372+
assertThat(config.sortSpecs[1].field, equalTo("@timestamp"));
373+
assertNull(config.sortSpecs[0].order);
374+
assertNull(config.sortSpecs[1].order);
375+
assertNull(config.sortSpecs[0].missingValue);
376+
assertNull(config.sortSpecs[1].missingValue);
377+
assertNull(config.sortSpecs[0].mode);
378+
assertNull(config.sortSpecs[1].mode);
379+
}
380+
261381
private Sort buildIndexSort(IndexSettings indexSettings, MappedFieldType... mfts) {
262382
Map<String, MappedFieldType> lookup = Maps.newMapWithExpectedSize(mfts.length);
263383
for (MappedFieldType mft : mfts) {

server/src/test/java/org/elasticsearch/search/aggregations/support/TimeSeriesIndexSearcherTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import java.util.concurrent.Executors;
4444
import java.util.concurrent.atomic.AtomicInteger;
4545

46-
import static org.elasticsearch.index.IndexSortConfig.TIME_SERIES_SORT;
46+
import static org.elasticsearch.index.IndexSortConfig.LegacyIndexSortConfigDefaults.TIME_SERIES_SORT;
4747
import static org.hamcrest.Matchers.greaterThan;
4848

4949
public class TimeSeriesIndexSearcherTests extends ESTestCase {

0 commit comments

Comments
 (0)