|
@@ -323,4 +323,115 @@ public class MapperServiceTests extends ESSingleNodeTestCase {
|
|
|
+ " that indices can have at most one type.", e.getMessage());
|
|
|
}
|
|
|
|
|
|
+ public void testFieldNameLengthLimit() throws Throwable {
|
|
|
+ int maxFieldNameLength = randomIntBetween(15, 20);
|
|
|
+ String testString = new String(new char[maxFieldNameLength + 1]).replace("\0", "a");
|
|
|
+ Settings settings = Settings.builder().put(MapperService.INDEX_MAPPING_FIELD_NAME_LENGTH_LIMIT_SETTING.getKey(), maxFieldNameLength)
|
|
|
+ .build();
|
|
|
+ MapperService mapperService = createIndex("test1", settings).mapperService();
|
|
|
+
|
|
|
+ CompressedXContent mapping = new CompressedXContent(BytesReference.bytes(
|
|
|
+ XContentFactory.jsonBuilder().startObject().startObject("type")
|
|
|
+ .startObject("properties")
|
|
|
+ .startObject("field")
|
|
|
+ .field("type", "text")
|
|
|
+ .endObject()
|
|
|
+ .endObject()
|
|
|
+ .endObject().endObject()));
|
|
|
+
|
|
|
+ mapperService.merge("type", mapping, MergeReason.MAPPING_UPDATE);
|
|
|
+
|
|
|
+ CompressedXContent mappingUpdate = new CompressedXContent(BytesReference.bytes(
|
|
|
+ XContentFactory.jsonBuilder().startObject()
|
|
|
+ .startObject("properties")
|
|
|
+ .startObject(testString)
|
|
|
+ .field("type", "text")
|
|
|
+ .endObject()
|
|
|
+ .endObject()
|
|
|
+ .endObject()));
|
|
|
+
|
|
|
+ IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
|
|
|
+ mapperService.merge("type", mappingUpdate, MergeReason.MAPPING_UPDATE);
|
|
|
+ });
|
|
|
+
|
|
|
+ assertEquals("Field name [" + testString + "] in index [test1] is too long. " +
|
|
|
+ "The limit is set to [" + maxFieldNameLength + "] characters but was ["
|
|
|
+ + testString.length() + "] characters", e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testObjectNameLengthLimit() throws Throwable {
|
|
|
+ int maxFieldNameLength = randomIntBetween(15, 20);
|
|
|
+ String testString = new String(new char[maxFieldNameLength + 1]).replace("\0", "a");
|
|
|
+ Settings settings = Settings.builder().put(MapperService.INDEX_MAPPING_FIELD_NAME_LENGTH_LIMIT_SETTING.getKey(), maxFieldNameLength)
|
|
|
+ .build();
|
|
|
+ MapperService mapperService = createIndex("test1", settings).mapperService();
|
|
|
+
|
|
|
+ CompressedXContent mapping = new CompressedXContent(BytesReference.bytes(
|
|
|
+ XContentFactory.jsonBuilder().startObject().startObject("type")
|
|
|
+ .startObject("properties")
|
|
|
+ .startObject(testString)
|
|
|
+ .field("type", "object")
|
|
|
+ .endObject()
|
|
|
+ .endObject()
|
|
|
+ .endObject().endObject()));
|
|
|
+
|
|
|
+ IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
|
|
|
+ mapperService.merge("type", mapping, MergeReason.MAPPING_UPDATE);
|
|
|
+ });
|
|
|
+
|
|
|
+ assertEquals("Field name [" + testString + "] in index [test1] is too long. " +
|
|
|
+ "The limit is set to [" + maxFieldNameLength + "] characters but was ["
|
|
|
+ + testString.length() + "] characters", e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testAliasFieldNameLengthLimit() throws Throwable {
|
|
|
+ int maxFieldNameLength = randomIntBetween(15, 20);
|
|
|
+ String testString = new String(new char[maxFieldNameLength + 1]).replace("\0", "a");
|
|
|
+ Settings settings = Settings.builder().put(MapperService.INDEX_MAPPING_FIELD_NAME_LENGTH_LIMIT_SETTING.getKey(), maxFieldNameLength)
|
|
|
+ .build();
|
|
|
+ MapperService mapperService = createIndex("test1", settings).mapperService();
|
|
|
+
|
|
|
+ CompressedXContent mapping = new CompressedXContent(BytesReference.bytes(
|
|
|
+ XContentFactory.jsonBuilder().startObject().startObject("type")
|
|
|
+ .startObject("properties")
|
|
|
+ .startObject(testString)
|
|
|
+ .field("type", "alias")
|
|
|
+ .field("path", "field")
|
|
|
+ .endObject()
|
|
|
+ .startObject("field")
|
|
|
+ .field("type", "text")
|
|
|
+ .endObject()
|
|
|
+ .endObject()
|
|
|
+ .endObject().endObject()));
|
|
|
+
|
|
|
+ IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
|
|
|
+ mapperService.merge("type", mapping, MergeReason.MAPPING_UPDATE);
|
|
|
+ });
|
|
|
+
|
|
|
+ assertEquals("Field name [" + testString + "] in index [test1] is too long. " +
|
|
|
+ "The limit is set to [" + maxFieldNameLength + "] characters but was ["
|
|
|
+ + testString.length() + "] characters", e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testMappingRecoverySkipFieldNameLengthLimit() throws Throwable {
|
|
|
+ int maxFieldNameLength = randomIntBetween(15, 20);
|
|
|
+ String testString = new String(new char[maxFieldNameLength + 1]).replace("\0", "a");
|
|
|
+ Settings settings = Settings.builder().put(MapperService.INDEX_MAPPING_FIELD_NAME_LENGTH_LIMIT_SETTING.getKey(), maxFieldNameLength)
|
|
|
+ .build();
|
|
|
+ MapperService mapperService = createIndex("test1", settings).mapperService();
|
|
|
+
|
|
|
+ CompressedXContent mapping = new CompressedXContent(BytesReference.bytes(
|
|
|
+ XContentFactory.jsonBuilder().startObject().startObject("type")
|
|
|
+ .startObject("properties")
|
|
|
+ .startObject(testString)
|
|
|
+ .field("type", "text")
|
|
|
+ .endObject()
|
|
|
+ .endObject()
|
|
|
+ .endObject().endObject()));
|
|
|
+
|
|
|
+ DocumentMapper documentMapper = mapperService.merge("type", mapping, MergeReason.MAPPING_RECOVERY);
|
|
|
+
|
|
|
+ assertEquals(testString, documentMapper.mappers().getMapper(testString).simpleName());
|
|
|
+ }
|
|
|
+
|
|
|
}
|