34
34
35
35
import org .apache .lucene .search .MatchAllDocsQuery ;
36
36
import org .apache .lucene .search .SortField ;
37
+ import org .apache .lucene .search .SortedNumericSortField ;
37
38
import org .apache .lucene .search .TopDocs ;
38
39
import org .opensearch .Version ;
39
40
import org .opensearch .action .support .ActiveShardCount ;
61
62
import org .opensearch .threadpool .ThreadPool ;
62
63
63
64
import java .io .IOException ;
65
+ import java .util .Arrays ;
64
66
import java .util .Collection ;
65
67
import java .util .Collections ;
68
+ import java .util .Map ;
66
69
import java .util .concurrent .CountDownLatch ;
67
70
import java .util .concurrent .atomic .AtomicInteger ;
68
71
import java .util .concurrent .atomic .AtomicReference ;
72
+ import java .util .stream .Collectors ;
69
73
70
74
import static org .opensearch .index .shard .IndexShardTestCase .getEngine ;
71
75
import static org .opensearch .test .InternalSettingsPlugin .TRANSLOG_RETENTION_CHECK_INTERVAL_SETTING ;
72
76
import static org .opensearch .test .hamcrest .OpenSearchAssertions .assertAcked ;
73
77
import static org .opensearch .test .hamcrest .OpenSearchAssertions .assertHitCount ;
78
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
74
79
import static org .hamcrest .core .IsEqual .equalTo ;
75
80
76
81
/** Unit test(s) for IndexService */
@@ -81,6 +86,11 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
81
86
return Collections .singleton (InternalSettingsPlugin .class );
82
87
}
83
88
89
+ @ Override
90
+ protected boolean forbidPrivateIndexSettings () {
91
+ return false ;
92
+ }
93
+
84
94
public static CompressedXContent filter (QueryBuilder filterBuilder ) throws IOException {
85
95
XContentBuilder builder = XContentFactory .jsonBuilder ();
86
96
filterBuilder .toXContent (builder , ToXContent .EMPTY_PARAMS );
@@ -535,64 +545,41 @@ public void testUpdateRemoteTranslogBufferIntervalDynamically() {
535
545
}
536
546
537
547
public void testIndexSort () {
538
- Settings settings = Settings .builder ()
539
- .put (IndexSettings .INDEX_TRANSLOG_SYNC_INTERVAL_SETTING .getKey (), "0ms" ) // disable
540
- .putList ("index.sort.field" , "sortfield" )
541
- .build ();
542
- try {
543
- // Integer index sort should be remained to int sort type
544
- IndexService index = createIndex ("test" , settings , createTestMapping ("integer" ));
545
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .INT );
546
-
547
- // Long index sort should be remained to long sort type
548
- index = createIndex ("test" , settings , createTestMapping ("long" ));
549
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .LONG );
550
-
551
- // Float index sort should be remained to float sort type
552
- index = createIndex ("test" , settings , createTestMapping ("float" ));
553
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .FLOAT );
554
-
555
- // Double index sort should be remained to double sort type
556
- index = createIndex ("test" , settings , createTestMapping ("double" ));
557
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .DOUBLE );
558
-
559
- // String index sort should be remained to string sort type
560
- index = createIndex ("test" , settings , createTestMapping ("string" ));
561
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .STRING );
562
- } catch (IllegalArgumentException ex ) {
563
- assertEquals ("failed to parse value [0ms] for setting [index.translog.sync_interval], must be >= [100ms]" , ex .getMessage ());
564
- }
548
+ runSortFieldTest (Settings .builder (), SortField .Type .INT );
565
549
}
566
550
567
551
public void testIndexSortBackwardCompatible () {
568
- Settings settings = Settings .builder ()
569
- .put (IndexSettings .INDEX_TRANSLOG_SYNC_INTERVAL_SETTING .getKey (), "0ms" ) // disable
570
- .put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), Version .V_2_6_1 )
571
- .putList ("index.sort.field" , "sortfield" )
572
- .build ();
573
- try {
574
- // Integer index sort should be converted to long sort type
575
- IndexService index = createIndex ("test" , settings , createTestMapping ("integer" ));
576
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .LONG );
577
-
578
- // Long index sort should be remained to long sort type
579
- index = createIndex ("test" , settings , createTestMapping ("long" ));
580
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .LONG );
581
-
582
- // Float index sort should be remained to float sort type
583
- index = createIndex ("test" , settings , createTestMapping ("float" ));
584
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .FLOAT );
585
-
586
- // Double index sort should be remained to double sort type
587
- index = createIndex ("test" , settings , createTestMapping ("double" ));
588
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .DOUBLE );
552
+ runSortFieldTest (
553
+ Settings .builder ().put (IndexMetadata .SETTING_INDEX_VERSION_CREATED .getKey (), Version .V_2_6_1 ),
554
+ SortField .Type .LONG
555
+ );
556
+ }
589
557
590
- // String index sort should be remained to string sort type
591
- index = createIndex ("test" , settings , createTestMapping ("string" ));
592
- assertTrue (index .getIndexSortSupplier ().get ().getSort ()[0 ].getType () == SortField .Type .STRING );
593
- } catch (IllegalArgumentException ex ) {
594
- assertEquals ("failed to parse value [0ms] for setting [index.translog.sync_interval], must be >= [100ms]" , ex .getMessage ());
595
- }
558
+ private void runSortFieldTest (Settings .Builder settingsBuilder , SortField .Type expectedIntType ) {
559
+ String [] sortFieldNames = new String [] { "int-sortfield" , "long-sortfield" , "double-sortfield" , "float-sortfield" };
560
+ Settings settings = settingsBuilder .putList ("index.sort.field" , sortFieldNames ).build ();
561
+ IndexService index = createIndexWithSimpleMappings (
562
+ "sort-field-test-index" ,
563
+ settings ,
564
+ "int-sortfield" ,
565
+ "type=integer" ,
566
+ "long-sortfield" ,
567
+ "type=long" ,
568
+ "double-sortfield" ,
569
+ "type=double" ,
570
+ "float-sortfield" ,
571
+ "type=float"
572
+ );
573
+ SortField [] sortFields = index .getIndexSortSupplier ().get ().getSort ();
574
+ Map <String , SortField .Type > map = Arrays .stream (sortFields )
575
+ .filter (s -> s instanceof SortedNumericSortField )
576
+ .map (s -> (SortedNumericSortField ) s )
577
+ .collect (Collectors .toMap (SortField ::getField , SortedNumericSortField ::getNumericType ));
578
+ assertThat (map .keySet (), containsInAnyOrder (sortFieldNames ));
579
+ assertThat (map .get ("int-sortfield" ), equalTo (expectedIntType ));
580
+ assertThat (map .get ("long-sortfield" ), equalTo (SortField .Type .LONG ));
581
+ assertThat (map .get ("float-sortfield" ), equalTo (SortField .Type .FLOAT ));
582
+ assertThat (map .get ("double-sortfield" ), equalTo (SortField .Type .DOUBLE ));
596
583
}
597
584
598
585
public void testReplicationTask () throws Exception {
@@ -843,15 +830,4 @@ public void testRefreshTaskUpdatesWithDynamicShardLevelRefreshes() throws Except
843
830
client ().admin ().cluster ().prepareUpdateSettings ().setTransientSettings (Settings .builder ().putNull ("*" )).get ();
844
831
845
832
}
846
-
847
- private static String createTestMapping (String type ) {
848
- return " \" properties\" : {\n "
849
- + " \" test\" : {\n "
850
- + " \" type\" : \" text\" \n "
851
- + " },\n "
852
- + " \" sortfield\" : {\n "
853
- + " \" type\" : \" + type + \" \n "
854
- + " }\n "
855
- + " }" ;
856
- }
857
833
}
0 commit comments