5050import org .elasticsearch .index .fielddata .LeafFieldData ;
5151import org .elasticsearch .index .fielddata .ScriptDocValues ;
5252import org .elasticsearch .index .fielddata .plain .AbstractLeafOrdinalsFieldData ;
53+ import org .elasticsearch .index .mapper .FieldMapper ;
5354import org .elasticsearch .index .mapper .IndexFieldMapper ;
5455import org .elasticsearch .index .mapper .KeywordFieldMapper ;
5556import org .elasticsearch .index .mapper .MappedFieldType ;
5657import org .elasticsearch .index .mapper .Mapper ;
58+ import org .elasticsearch .index .mapper .Mapper .BuilderContext ;
59+ import org .elasticsearch .index .mapper .Mapper .TypeParser ;
5760import org .elasticsearch .index .mapper .MapperService ;
5861import org .elasticsearch .index .mapper .NumberFieldMapper ;
62+ import org .elasticsearch .index .mapper .ParseContext ;
5963import org .elasticsearch .index .mapper .TextFieldMapper ;
64+ import org .elasticsearch .index .mapper .TextSearchInfo ;
65+ import org .elasticsearch .index .mapper .ValueFetcher ;
6066import org .elasticsearch .indices .IndicesModule ;
67+ import org .elasticsearch .plugins .MapperPlugin ;
6168import org .elasticsearch .search .lookup .LeafDocLookup ;
6269import org .elasticsearch .search .lookup .LeafSearchLookup ;
6370import org .elasticsearch .search .lookup .SearchLookup ;
6673import java .io .IOException ;
6774import java .util .ArrayList ;
6875import java .util .Collections ;
76+ import java .util .HashMap ;
6977import java .util .List ;
7078import java .util .Map ;
79+ import java .util .Set ;
7180import java .util .function .BiFunction ;
7281import java .util .function .Supplier ;
7382
@@ -305,12 +314,20 @@ public void testFielddataLookupOneFieldManyReferences() throws IOException {
305314 }
306315
307316 public void testRuntimeFields () throws IOException {
308- MapperService mapperService = mockMapperService ("test" );
309- Map <String , Object > runtimeFields = Map .ofEntries (
310- Map .entry ("cat" , Map .of ("type" , "keyword" , "script" , "emit('cat')" )),
311- Map .entry ("dog" , Map .of ("type" , "keyword" , "script" , "emit('dog')" )),
312- Map .entry ("catdog" , Map .of ("type" , "keyword" , "script" , "emit(doc['cat'].value + doc['dog'].value)" ))
313- );
317+ MapperService mapperService = mockMapperService ("test" , List .of (new MapperPlugin () {
318+ @ Override
319+ public Map <String , TypeParser > getMappers () {
320+ return Map .of ("runtime" , (name , node , parserContext ) -> new Mapper .Builder (name ) {
321+ @ Override
322+ public Mapper build (BuilderContext context ) {
323+ return new DummyMapper (name , new DummyMappedFieldType (name ));
324+ }
325+ });
326+ }
327+ }));
328+ Map <String , Object > runtimeFields = new HashMap <>();
329+ runtimeFields .put ("cat" , new HashMap <>(Map .of ("type" , "keyword" )));
330+ runtimeFields .put ("dog" , new HashMap <>(Map .of ("type" , "keyword" )));
314331 QueryShardContext qsc = new QueryShardContext (
315332 0 ,
316333 mapperService .getIndexSettings (),
@@ -321,7 +338,7 @@ public void testRuntimeFields() throws IOException {
321338 null ,
322339 null ,
323340 NamedXContentRegistry .EMPTY ,
324- new NamedWriteableRegistry (Collections . emptyList ()),
341+ new NamedWriteableRegistry (List . of ()),
325342 null ,
326343 null ,
327344 () -> 0 ,
@@ -332,7 +349,12 @@ public void testRuntimeFields() throws IOException {
332349 runtimeFields
333350 );
334351 assertTrue (qsc .isFieldMapped ("cat" ));
335- assertThat (qsc .getFieldType (name ))
352+ assertThat (qsc .getFieldType ("cat" ), instanceOf (DummyMappedFieldType .class ));
353+ assertThat (qsc .simpleMatchToIndexNames ("cat" ), equalTo (Set .of ("cat" )));
354+ assertTrue (qsc .isFieldMapped ("dog" ));
355+ assertThat (qsc .getFieldType ("dog" ), instanceOf (DummyMappedFieldType .class ));
356+ assertThat (qsc .simpleMatchToIndexNames ("dog" ), equalTo (Set .of ("dog" )));
357+ assertThat (qsc .simpleMatchToIndexNames ("*" ), equalTo (Set .of ("cat" , "dog" )));
336358 }
337359
338360 public static QueryShardContext createQueryShardContext (String indexUuid , String clusterAlias ) {
@@ -344,7 +366,7 @@ private static QueryShardContext createQueryShardContext(
344366 String clusterAlias ,
345367 TriFunction <String , LeafSearchLookup , Integer , String > runtimeDocValues
346368 ) {
347- MapperService mapperService = mockMapperService (indexUuid );
369+ MapperService mapperService = mockMapperService (indexUuid , List . of () );
348370 if (runtimeDocValues != null ) {
349371 when (mapperService .fieldType (any ())).thenAnswer (fieldTypeInv -> {
350372 String fieldName = (String )fieldTypeInv .getArguments ()[0 ];
@@ -359,7 +381,7 @@ mapperService, null, null, NamedXContentRegistry.EMPTY, new NamedWriteableRegist
359381 null , null , () -> nowInMillis , clusterAlias , null , () -> true , null );
360382 }
361383
362- private static MapperService mockMapperService (String indexUuid ) {
384+ private static MapperService mockMapperService (String indexUuid , List < MapperPlugin > mapperPlugins ) {
363385 IndexMetadata .Builder indexMetadataBuilder = new IndexMetadata .Builder ("index" );
364386 indexMetadataBuilder .settings (Settings .builder ().put ("index.version.created" , Version .CURRENT )
365387 .put ("index.number_of_shards" , 1 )
@@ -377,7 +399,7 @@ private static MapperService mockMapperService(String indexUuid) {
377399 when (mapperService .getIndexSettings ()).thenReturn (indexSettings );
378400 when (mapperService .index ()).thenReturn (indexMetadata .getIndex ());
379401 when (mapperService .getIndexAnalyzers ()).thenReturn (indexAnalyzers );
380- Map <String , Mapper .TypeParser > typeParserMap = IndicesModule .getMappers (Collections . emptyList () );
402+ Map <String , Mapper .TypeParser > typeParserMap = IndicesModule .getMappers (mapperPlugins );
381403 Mapper .TypeParser .ParserContext parserContext = new Mapper .TypeParser .ParserContext (name -> null , typeParserMap ::get ,
382404 Version .CURRENT , () -> null , null , null , mapperService .getIndexAnalyzers (), mapperService .getIndexSettings (),
383405 () -> {
@@ -482,4 +504,45 @@ public void collect(int doc) throws IOException {
482504 }
483505 }
484506
507+ private static class DummyMapper extends FieldMapper {
508+ protected DummyMapper (String simpleName , MappedFieldType mappedFieldType ) {
509+ super (simpleName , mappedFieldType , Map .of (), MultiFields .empty (), CopyTo .empty ());
510+ }
511+
512+ @ Override
513+ protected void parseCreateField (ParseContext context ) throws IOException {
514+ throw new UnsupportedOperationException ();
515+ }
516+
517+ @ Override
518+ public Builder getMergeBuilder () {
519+ throw new UnsupportedOperationException ();
520+ }
521+
522+ @ Override
523+ protected String contentType () {
524+ throw new UnsupportedOperationException ();
525+ }
526+ }
527+
528+ private static class DummyMappedFieldType extends MappedFieldType {
529+ public DummyMappedFieldType (String name ) {
530+ super (name , true , false , true , TextSearchInfo .SIMPLE_MATCH_ONLY , null );
531+ }
532+
533+ @ Override
534+ public ValueFetcher valueFetcher (QueryShardContext context , SearchLookup searchLookup , String format ) {
535+ throw new UnsupportedOperationException ();
536+ }
537+
538+ @ Override
539+ public String typeName () {
540+ return "runtime" ;
541+ }
542+
543+ @ Override
544+ public Query termQuery (Object value , QueryShardContext context ) {
545+ throw new UnsupportedOperationException ();
546+ }
547+ }
485548}
0 commit comments