Limit type name length

This commit will limit type name length to be at most 255 characters.

Closes #13021
This commit is contained in:
Jason Tedor 2015-08-21 07:57:27 -04:00
parent f0aae96f11
commit 1579e491d8
2 changed files with 44 additions and 0 deletions

View File

@ -261,6 +261,9 @@ public class MapperService extends AbstractIndexComponent implements Closeable {
if (mapper.type().length() == 0) {
throw new InvalidTypeNameException("mapping type name is empty");
}
if (Version.indexCreated(indexSettings).onOrAfter(Version.V_2_0_0_beta1) && mapper.type().length() > 255) {
throw new InvalidTypeNameException("mapping type name [" + mapper.type() + "] is too long; limit is length 255 but was [" + mapper.type().length() + "]");
}
if (mapper.type().charAt(0) == '_') {
throw new InvalidTypeNameException("mapping type name [" + mapper.type() + "] can't start with '_'");
}

View File

@ -19,12 +19,18 @@
package org.elasticsearch.index.mapper;
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.test.ESSingleNodeTestCase;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.elasticsearch.test.VersionUtils.getFirstVersion;
import static org.elasticsearch.test.VersionUtils.getPreviousVersion;
import static org.elasticsearch.test.VersionUtils.randomVersionBetween;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.hasToString;
public class MapperServiceTest extends ESSingleNodeTestCase {
@ -46,4 +52,39 @@ public class MapperServiceTest extends ESSingleNodeTestCase {
.execute()
.actionGet();
}
@Test
public void testThatLongTypeNameIsNotRejectedOnPreElasticsearchVersionTwo() {
String index = "text-index";
String field = "field";
String type = new String(new char[256]).replace("\0", "a");
CreateIndexResponse response =
client()
.admin()
.indices()
.prepareCreate(index)
.setSettings(settings(randomVersionBetween(random(), getFirstVersion(), getPreviousVersion(Version.V_2_0_0_beta1))))
.addMapping(type, field, "type=string")
.execute()
.actionGet();
assertNotNull(response);
}
@Test
public void testTypeNameTooLong() {
String index = "text-index";
String field = "field";
String type = new String(new char[256]).replace("\0", "a");
expectedException.expect(MapperParsingException.class);
expectedException.expect(hasToString(containsString("mapping type name [" + type + "] is too long; limit is length 255 but was [256]")));
client()
.admin()
.indices()
.prepareCreate(index)
.addMapping(type, field, "type=string")
.execute()
.actionGet();
}
}