2929import org .elasticsearch .search .MultiValueMode ;
3030import org .elasticsearch .search .sort .SortOrder ;
3131import org .elasticsearch .test .ESTestCase ;
32+ import org .elasticsearch .test .index .IndexVersionUtils ;
3233
3334import java .util .ArrayList ;
3435import java .util .Collections ;
@@ -49,6 +50,10 @@ private static IndexSettings indexSettings(Settings settings) {
4950 return new IndexSettings (newIndexMeta ("test" , settings ), Settings .EMPTY );
5051 }
5152
53+ private static IndexSettings indexSettings (Settings settings , IndexVersion indexVersion ) {
54+ return new IndexSettings (newIndexMeta ("test" , settings , indexVersion ), Settings .EMPTY );
55+ }
56+
5257 public void testNoIndexSort () {
5358 IndexSettings indexSettings = indexSettings (Settings .EMPTY );
5459 assertFalse (indexSettings .getIndexSortConfig ().hasIndexSort ());
@@ -257,6 +262,111 @@ public void testTimeSeriesModeNoTimestamp() {
257262 assertThat (e .getMessage (), equalTo ("unknown index sort field:[@timestamp] required by [index.mode=time_series]" ));
258263 }
259264
265+ public void testLogsdbIndexSortWithArrays () {
266+ Settings settings = Settings .builder ()
267+ .put (IndexSettings .MODE .getKey (), "logsdb" )
268+ .putList ("index.sort.field" , "field1" , "field2" )
269+ .putList ("index.sort.order" , "asc" , "desc" )
270+ .putList ("index.sort.missing" , "_last" , "_first" )
271+ .build ();
272+ IndexSettings indexSettings = indexSettings (settings );
273+ IndexSortConfig config = indexSettings .getIndexSortConfig ();
274+ assertTrue (config .hasIndexSort ());
275+ assertThat (config .sortSpecs .length , equalTo (2 ));
276+
277+ assertThat (config .sortSpecs [0 ].field , equalTo ("field1" ));
278+ assertThat (config .sortSpecs [1 ].field , equalTo ("field2" ));
279+ assertThat (config .sortSpecs [0 ].order , equalTo (SortOrder .ASC ));
280+ assertThat (config .sortSpecs [1 ].order , equalTo (SortOrder .DESC ));
281+ assertThat (config .sortSpecs [0 ].missingValue , equalTo ("_last" ));
282+ assertThat (config .sortSpecs [1 ].missingValue , equalTo ("_first" ));
283+ assertNull (config .sortSpecs [0 ].mode );
284+ assertNull (config .sortSpecs [1 ].mode );
285+ }
286+
287+ public void testLogsdbInvalidIndexSortOrder () {
288+ final Settings settings = Settings .builder ()
289+ .put (IndexSettings .MODE .getKey (), "logsdb" )
290+ .putList ("index.sort.order" , new String [] { "asc" , "desc" })
291+ .build ();
292+ IllegalArgumentException exc = expectThrows (IllegalArgumentException .class , () -> indexSettings (settings ));
293+ assertThat (exc .getMessage (), containsString ("setting [index.sort.order] requires [index.sort.field] to be configured" ));
294+ }
295+
296+ public void testLogsdbInvalidIndexSortMode () {
297+ final Settings settings = Settings .builder ()
298+ .put (IndexSettings .MODE .getKey (), "logsdb" )
299+ .putList ("index.sort.mode" , new String [] { "max" })
300+ .build ();
301+ IllegalArgumentException exc = expectThrows (IllegalArgumentException .class , () -> indexSettings (settings ));
302+ assertThat (exc .getMessage (), containsString ("setting [index.sort.mode] requires [index.sort.field] to be configured" ));
303+ }
304+
305+ public void testLogsdbInvalidIndexSortMissing () {
306+ final Settings settings = Settings .builder ()
307+ .put (IndexSettings .MODE .getKey (), "logsdb" )
308+ .putList ("index.sort.missing" , new String [] { "_last" , "_last" })
309+ .build ();
310+ IllegalArgumentException exc = expectThrows (IllegalArgumentException .class , () -> indexSettings (settings ));
311+ assertThat (exc .getMessage (), containsString ("setting [index.sort.missing] requires [index.sort.field] to be configured" ));
312+ }
313+
314+ public void testLogsdbIndexSortWithHostname () {
315+ Settings settings = Settings .builder ()
316+ .put (IndexSettings .MODE .getKey (), "logsdb" )
317+ .put (IndexSettings .LOGSDB_SORT_ON_HOST_NAME .getKey (), true )
318+ .build ();
319+ IndexSettings indexSettings = indexSettings (settings );
320+ IndexSortConfig config = indexSettings .getIndexSortConfig ();
321+ assertTrue (config .hasIndexSort ());
322+ assertThat (config .sortSpecs .length , equalTo (2 ));
323+
324+ assertThat (config .sortSpecs [0 ].field , equalTo ("host.name" ));
325+ assertThat (config .sortSpecs [1 ].field , equalTo ("@timestamp" ));
326+ assertThat (config .sortSpecs [0 ].order , equalTo (SortOrder .ASC ));
327+ assertThat (config .sortSpecs [1 ].order , equalTo (SortOrder .DESC ));
328+ assertThat (config .sortSpecs [0 ].missingValue , equalTo ("_last" ));
329+ assertThat (config .sortSpecs [1 ].missingValue , equalTo ("_last" ));
330+ assertThat (config .sortSpecs [0 ].mode , equalTo (MultiValueMode .MIN ));
331+ assertThat (config .sortSpecs [1 ].mode , equalTo (MultiValueMode .MAX ));
332+ }
333+
334+ public void testLogsdbIndexSortTimestampOnly () {
335+ Settings settings = Settings .builder ()
336+ .put (IndexSettings .MODE .getKey (), "logsdb" )
337+ .put (IndexSettings .LOGSDB_SORT_ON_HOST_NAME .getKey (), false )
338+ .build ();
339+ IndexSettings indexSettings = indexSettings (settings );
340+ IndexSortConfig config = indexSettings .getIndexSortConfig ();
341+ assertTrue (config .hasIndexSort ());
342+ assertThat (config .sortSpecs .length , equalTo (1 ));
343+
344+ assertThat (config .sortSpecs [0 ].field , equalTo ("@timestamp" ));
345+ assertThat (config .sortSpecs [0 ].order , equalTo (SortOrder .DESC ));
346+ assertThat (config .sortSpecs [0 ].missingValue , equalTo ("_last" ));
347+ assertThat (config .sortSpecs [0 ].mode , equalTo (MultiValueMode .MAX ));
348+ }
349+
350+ public void testLogsdbIndexSortTimestampBWC () {
351+ Settings settings = Settings .builder ().put (IndexSettings .MODE .getKey (), "logsdb" ).build ();
352+ IndexSettings indexSettings = indexSettings (
353+ settings ,
354+ IndexVersionUtils .getPreviousVersion (IndexVersions .LOGSB_OPTIONAL_SORTING_ON_HOST_NAME )
355+ );
356+ IndexSortConfig config = indexSettings .getIndexSortConfig ();
357+ assertTrue (config .hasIndexSort ());
358+ assertThat (config .sortSpecs .length , equalTo (2 ));
359+
360+ assertThat (config .sortSpecs [0 ].field , equalTo ("host.name" ));
361+ assertThat (config .sortSpecs [1 ].field , equalTo ("@timestamp" ));
362+ assertThat (config .sortSpecs [0 ].order , equalTo (SortOrder .ASC ));
363+ assertThat (config .sortSpecs [1 ].order , equalTo (SortOrder .ASC ));
364+ assertThat (config .sortSpecs [0 ].missingValue , equalTo ("_last" ));
365+ assertThat (config .sortSpecs [1 ].missingValue , equalTo ("_last" ));
366+ assertThat (config .sortSpecs [0 ].mode , equalTo (MultiValueMode .MIN ));
367+ assertThat (config .sortSpecs [1 ].mode , equalTo (MultiValueMode .MIN ));
368+ }
369+
260370 private Sort buildIndexSort (IndexSettings indexSettings , MappedFieldType ... mfts ) {
261371 Map <String , MappedFieldType > lookup = Maps .newMapWithExpectedSize (mfts .length );
262372 for (MappedFieldType mft : mfts ) {
0 commit comments