@@ -143,6 +143,10 @@ protected static String[] getCurrentTypes() {
143
143
return currentTypes ;
144
144
}
145
145
146
+ protected static boolean isSingleType () {
147
+ return serviceHolder .idxSettings .isSingleType ();
148
+ }
149
+
146
150
protected Collection <Class <? extends Plugin >> getPlugins () {
147
151
return Collections .emptyList ();
148
152
}
@@ -217,7 +221,7 @@ protected Settings indexSettings() {
217
221
}
218
222
219
223
protected static String expectedFieldName (String builderFieldName ) {
220
- if (currentTypes .length == 0 ) {
224
+ if (currentTypes .length == 0 || ! isSingleType () ) {
221
225
return builderFieldName ;
222
226
}
223
227
return ALIAS_TO_CONCRETE_FIELD_NAME .getOrDefault (builderFieldName , builderFieldName );
@@ -384,20 +388,36 @@ public void onRemoval(ShardId shardId, Accountable accountable) {
384
388
mapperService .merge (type , new CompressedXContent (Strings .toString (PutMappingRequest .buildFromSimplifiedDef (type ,
385
389
STRING_FIELD_NAME , "type=text" ,
386
390
STRING_FIELD_NAME_2 , "type=keyword" ,
387
- STRING_ALIAS_FIELD_NAME , "type=alias,path=" + STRING_FIELD_NAME ,
388
391
INT_FIELD_NAME , "type=integer" ,
389
- INT_ALIAS_FIELD_NAME , "type=alias,path=" + INT_FIELD_NAME ,
390
392
INT_RANGE_FIELD_NAME , "type=integer_range" ,
391
393
DOUBLE_FIELD_NAME , "type=double" ,
392
394
BOOLEAN_FIELD_NAME , "type=boolean" ,
393
395
DATE_FIELD_NAME , "type=date" ,
394
- DATE_ALIAS_FIELD_NAME , "type=alias,path=" + DATE_FIELD_NAME ,
395
396
DATE_RANGE_FIELD_NAME , "type=date_range" ,
396
397
OBJECT_FIELD_NAME , "type=object" ,
397
398
GEO_POINT_FIELD_NAME , "type=geo_point" ,
398
- GEO_POINT_ALIAS_FIELD_NAME , "type=alias,path=" + GEO_POINT_FIELD_NAME ,
399
399
GEO_SHAPE_FIELD_NAME , "type=geo_shape"
400
400
))), MapperService .MergeReason .MAPPING_UPDATE , false );
401
+
402
+ // Field aliases are only supported on indexes with a single type. If the index has multiple types, we
403
+ // still create fields with the same names as the alias fields, but with a concrete definition. This
404
+ // avoids the need for various test classes to check whether the index contains a single type.
405
+ if (idxSettings .isSingleType ()) {
406
+ mapperService .merge (type , new CompressedXContent (Strings .toString (PutMappingRequest .buildFromSimplifiedDef (type ,
407
+ STRING_ALIAS_FIELD_NAME , "type=alias,path=" + STRING_FIELD_NAME ,
408
+ INT_ALIAS_FIELD_NAME , "type=alias,path=" + INT_FIELD_NAME ,
409
+ DATE_ALIAS_FIELD_NAME , "type=alias,path=" + DATE_FIELD_NAME ,
410
+ GEO_POINT_ALIAS_FIELD_NAME , "type=alias,path=" + GEO_POINT_FIELD_NAME
411
+ ))), MapperService .MergeReason .MAPPING_UPDATE , false );
412
+ } else {
413
+ mapperService .merge (type , new CompressedXContent (Strings .toString (PutMappingRequest .buildFromSimplifiedDef (type ,
414
+ STRING_ALIAS_FIELD_NAME , "type=text" ,
415
+ INT_ALIAS_FIELD_NAME , "type=integer" ,
416
+ DATE_ALIAS_FIELD_NAME , "type=date" ,
417
+ GEO_POINT_ALIAS_FIELD_NAME , "type=geo_point"
418
+ ))), MapperService .MergeReason .MAPPING_UPDATE , false );
419
+ }
420
+
401
421
// also add mappings for two inner field in the object field
402
422
mapperService .merge (type , new CompressedXContent ("{\" properties\" :{\" " + OBJECT_FIELD_NAME + "\" :{\" type\" :\" object\" ,"
403
423
+ "\" properties\" :{\" " + DATE_FIELD_NAME + "\" :{\" type\" :\" date\" },\" " +
0 commit comments