Add limit to total number of fields in mapping. #17357 · elastic/elasticsearch@361adcf (original) (raw)

`@@ -30,15 +30,21 @@

`

30

30

`import org.elasticsearch.ExceptionsHelper;

`

31

31

`import org.elasticsearch.common.compress.CompressedXContent;

`

32

32

`import org.elasticsearch.common.lucene.search.Queries;

`

``

33

`+

import org.elasticsearch.common.settings.Settings;

`

``

34

`+

import org.elasticsearch.common.xcontent.XContentFactory;

`

33

35

`import org.elasticsearch.index.IndexService;

`

``

36

`+

import org.elasticsearch.index.mapper.MapperService.MergeReason;

`

34

37

`import org.elasticsearch.index.mapper.internal.TypeFieldMapper;

`

35

38

`import org.elasticsearch.test.ESSingleNodeTestCase;

`

36

39

`import org.junit.Rule;

`

37

40

`import org.junit.rules.ExpectedException;

`

38

41

``

``

42

`+

import java.io.IOException;

`

``

43

`+

import java.io.UncheckedIOException;

`

39

44

`import java.util.Arrays;

`

40

45

`import java.util.Collections;

`

41

46

`import java.util.HashSet;

`

``

47

`+

import java.util.function.Function;

`

42

48

`import java.util.concurrent.ExecutionException;

`

43

49

``

44

50

`import static org.hamcrest.CoreMatchers.containsString;

`

`@@ -135,4 +141,24 @@ public void testIndexIntoDefaultMapping() throws Throwable {

`

135

141

`assertFalse(indexService.mapperService().hasMapping(MapperService.DEFAULT_MAPPING));

`

136

142

` }

`

137

143

``

``

144

`+

public void testTotalFieldsExceedsLimit() throws Throwable {

`

``

145

`+

Function<String, String> mapping = type -> {

`

``

146

`+

try {

`

``

147

`+

return XContentFactory.jsonBuilder().startObject().startObject(type).startObject("properties")

`

``

148

`+

.startObject("field1").field("type", "string")

`

``

149

`+

.endObject().endObject().endObject().endObject().string();

`

``

150

`+

} catch (IOException e) {

`

``

151

`+

throw new UncheckedIOException(e);

`

``

152

`+

}

`

``

153

`+

};

`

``

154

`+

createIndex("test1").mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE, false);

`

``

155

`+

//set total number of fields to 1 to trigger an exception

`

``

156

`+

try {

`

``

157

`+

createIndex("test2", Settings.builder().put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 1).build())

`

``

158

`+

.mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE, false);

`

``

159

`+

fail("Expected IllegalArgumentException");

`

``

160

`+

} catch (IllegalArgumentException e) {

`

``

161

`+

assertThat(e.getMessage(), containsString("Limit of total fields [1] in index [test2] has been exceeded"));

`

``

162

`+

}

`

``

163

`+

}

`

138

164

`}

`