@@ -270,6 +270,37 @@ public void testFieldAliasWithMismatchedNestedScope() throws Throwable {
270270 assertThat (e .getMessage (), containsString ("Invalid [path] value [nested.field] for field alias [alias]" ));
271271 }
272272
273+ public void testTotalFieldsLimitWithFieldAlias () throws Throwable {
274+ String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" )
275+ .startObject ("properties" )
276+ .startObject ("alias" )
277+ .field ("type" , "alias" )
278+ .field ("path" , "field" )
279+ .endObject ()
280+ .startObject ("field" )
281+ .field ("type" , "text" )
282+ .endObject ()
283+ .endObject ()
284+ .endObject ().endObject ());
285+
286+ DocumentMapper documentMapper = createIndex ("test1" ).mapperService ()
287+ .merge ("type" , new CompressedXContent (mapping ), MergeReason .MAPPING_UPDATE );
288+
289+ // Set the total fields limit to the number of non-alias fields, to verify that adding
290+ // a field alias pushes the mapping over the limit.
291+ int numFields = documentMapper .mapping ().metadataMappers .length + 2 ;
292+ int numNonAliasFields = numFields - 1 ;
293+
294+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> {
295+ Settings settings = Settings .builder ()
296+ .put (MapperService .INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING .getKey (), numNonAliasFields )
297+ .build ();
298+ createIndex ("test2" , settings ).mapperService ()
299+ .merge ("type" , new CompressedXContent (mapping ), MergeReason .MAPPING_UPDATE );
300+ });
301+ assertEquals ("Limit of total fields [" + numNonAliasFields + "] in index [test2] has been exceeded" , e .getMessage ());
302+ }
303+
273304 public void testForbidMultipleTypes () throws IOException {
274305 String mapping = Strings .toString (XContentFactory .jsonBuilder ().startObject ().startObject ("type" ).endObject ().endObject ());
275306 MapperService mapperService = createIndex ("test" ).mapperService ();
0 commit comments