Skip to content

Commit c737954

Browse files
committed
Make sure that field aliases count towards the total fields limit.
1 parent a9a9598 commit c737954

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ private synchronized Map<String, DocumentMapper> internalMerge(@Nullable Documen
427427
// the master node restoring mappings from disk or data nodes
428428
// deserializing cluster state that was sent by the master node,
429429
// this check will be skipped.
430-
checkTotalFieldsLimit(objectMappers.size() + fieldMappers.size());
430+
checkTotalFieldsLimit(objectMappers.size() + fieldMappers.size() + fieldAliasMappers.size());
431431
}
432432

433433
results.put(newMapper.type(), newMapper);

server/src/test/java/org/elasticsearch/index/mapper/MapperServiceTests.java

+31
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)