Skip to content

Commit 8baf144

Browse files
committed
Stop automatically nesting mappings in index creation requests. (#36924)
Now that we unwrap mappings in DocumentMapperParser#extractMappings, it is not necessary for the mapping definition to always be nested under the type. This leniency around the mapping format was added in 2341825.
1 parent 608f234 commit 8baf144

File tree

5 files changed

+96
-77
lines changed

5 files changed

+96
-77
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.elasticsearch.common.Strings;
3434
import org.elasticsearch.common.bytes.BytesArray;
3535
import org.elasticsearch.common.bytes.BytesReference;
36-
import org.elasticsearch.common.collect.MapBuilder;
3736
import org.elasticsearch.common.io.stream.StreamInput;
3837
import org.elasticsearch.common.io.stream.StreamOutput;
3938
import org.elasticsearch.common.settings.Settings;
@@ -260,10 +259,6 @@ public CreateIndexRequest mapping(String type, Map source) {
260259
if (mappings.containsKey(type)) {
261260
throw new IllegalStateException("mappings for type \"" + type + "\" were already defined");
262261
}
263-
// wrap it in a type map if its not
264-
if (source.size() != 1 || !source.containsKey(type)) {
265-
source = MapBuilder.<String, Object>newMapBuilder().put(type, source).map();
266-
}
267262
try {
268263
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
269264
builder.map(source);
@@ -278,7 +273,7 @@ public CreateIndexRequest mapping(String type, Map source) {
278273
* ("field1", "type=string,store=true").
279274
*/
280275
public CreateIndexRequest mapping(String type, Object... source) {
281-
mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source));
276+
mapping(type, PutMappingRequest.buildFromSimplifiedDef(source));
282277
return this;
283278
}
284279

server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ public PutMappingRequest source(Object... source) {
185185
return source(buildFromSimplifiedDef(type, source));
186186
}
187187

188+
/**
189+
* @param source
190+
* consisting of field/properties pairs (e.g. "field1",
191+
* "type=string,store=true")
192+
* @throws IllegalArgumentException
193+
* if the number of the source arguments is not divisible by two
194+
* @return the mappings definition
195+
*/
196+
public static XContentBuilder buildFromSimplifiedDef(Object... source) {
197+
return buildFromSimplifiedDef(null, source);
198+
}
199+
188200
/**
189201
* @param type
190202
* the mapping type

server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void testNonNestedMappings() throws Exception {
143143
assertFalse(metadata.sourceAsMap().isEmpty());
144144
}
145145

146-
public void testEmptyNestedMappings() throws Exception {
146+
public void testNonNestedEmptyMappings() throws Exception {
147147
assertAcked(prepareCreate("test")
148148
.addMapping("_doc", XContentFactory.jsonBuilder().startObject().endObject()));
149149

@@ -173,6 +173,20 @@ public void testEmptyMappings() throws Exception {
173173
assertTrue(metadata.sourceAsMap().isEmpty());
174174
}
175175

176+
public void testFlatMappingFormat() throws Exception {
177+
assertAcked(prepareCreate("test")
178+
.addMapping("_doc", "field", "type=keyword"));
179+
180+
GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get();
181+
182+
ImmutableOpenMap<String, MappingMetaData> mappings = response.mappings().get("test");
183+
assertNotNull(mappings);
184+
185+
MappingMetaData metadata = mappings.get("_doc");
186+
assertNotNull(metadata);
187+
assertFalse(metadata.sourceAsMap().isEmpty());
188+
}
189+
176190
public void testInvalidShardCountSettings() throws Exception {
177191
int value = randomIntBetween(-10, 0);
178192
try {

server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java

Lines changed: 50 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -21,104 +21,68 @@
2121

2222
import org.elasticsearch.action.admin.indices.alias.Alias;
2323
import org.elasticsearch.common.Strings;
24-
import org.elasticsearch.common.bytes.BytesReference;
2524
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2625
import org.elasticsearch.common.io.stream.StreamInput;
27-
import org.elasticsearch.common.settings.Settings;
2826
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2927
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3028
import org.elasticsearch.common.xcontent.XContentParser;
3129
import org.elasticsearch.common.xcontent.XContentType;
3230
import org.elasticsearch.common.xcontent.json.JsonXContent;
3331
import org.elasticsearch.index.RandomCreateIndexGenerator;
34-
import org.elasticsearch.test.ESTestCase;
35-
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
32+
import org.elasticsearch.test.AbstractXContentTestCase;
3633

3734
import java.io.IOException;
3835
import java.util.Map;
3936
import java.util.Set;
4037

41-
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
42-
import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS;
38+
public class CreateIndexRequestTests extends AbstractXContentTestCase<CreateIndexRequest> {
4339

44-
public class CreateIndexRequestTests extends ESTestCase {
45-
46-
public void testSerialization() throws IOException {
47-
CreateIndexRequest request = new CreateIndexRequest("foo");
48-
String mapping = Strings.toString(JsonXContent.contentBuilder().startObject().startObject("type").endObject().endObject());
49-
request.mapping("my_type", mapping, XContentType.JSON);
50-
51-
try (BytesStreamOutput output = new BytesStreamOutput()) {
52-
request.writeTo(output);
53-
54-
try (StreamInput in = output.bytes().streamInput()) {
55-
CreateIndexRequest serialized = new CreateIndexRequest();
56-
serialized.readFrom(in);
57-
assertEquals(request.index(), serialized.index());
58-
assertEquals(mapping, serialized.mappings().get("my_type"));
59-
}
40+
@Override
41+
protected CreateIndexRequest createTestInstance() {
42+
try {
43+
return RandomCreateIndexGenerator.randomCreateIndexRequest();
44+
} catch (IOException e) {
45+
throw new RuntimeException(e);
6046
}
6147
}
6248

63-
public void testToXContent() throws IOException {
64-
CreateIndexRequest request = new CreateIndexRequest("foo");
65-
66-
String mapping = Strings.toString(JsonXContent.contentBuilder().startObject().startObject("type").endObject().endObject());
67-
request.mapping("my_type", mapping, XContentType.JSON);
68-
69-
Alias alias = new Alias("test_alias");
70-
alias.routing("1");
71-
alias.filter("{\"term\":{\"year\":2016}}");
72-
alias.writeIndex(true);
73-
request.alias(alias);
74-
75-
Settings.Builder settings = Settings.builder();
76-
settings.put(SETTING_NUMBER_OF_SHARDS, 10);
77-
request.settings(settings);
78-
79-
String actualRequestBody = Strings.toString(request);
80-
81-
String expectedRequestBody = "{\"settings\":{\"index\":{\"number_of_shards\":\"10\"}}," +
82-
"\"mappings\":{\"my_type\":{\"type\":{}}}," +
83-
"\"aliases\":{\"test_alias\":{\"filter\":{\"term\":{\"year\":2016}},\"routing\":\"1\",\"is_write_index\":true}}}";
84-
85-
assertEquals(expectedRequestBody, actualRequestBody);
49+
@Override
50+
protected CreateIndexRequest doParseInstance(XContentParser parser) throws IOException {
51+
CreateIndexRequest request = new CreateIndexRequest();
52+
request.source(parser.map(), LoggingDeprecationHandler.INSTANCE);
53+
return request;
8654
}
8755

88-
public void testToAndFromXContent() throws IOException {
89-
90-
final CreateIndexRequest createIndexRequest = RandomCreateIndexGenerator.randomCreateIndexRequest();
91-
92-
boolean humanReadable = randomBoolean();
93-
final XContentType xContentType = randomFrom(XContentType.values());
94-
BytesReference originalBytes = toShuffledXContent(createIndexRequest, xContentType, EMPTY_PARAMS, humanReadable);
95-
96-
CreateIndexRequest parsedCreateIndexRequest = new CreateIndexRequest();
97-
parsedCreateIndexRequest.source(originalBytes, xContentType);
98-
99-
assertMappingsEqual(createIndexRequest.mappings(), parsedCreateIndexRequest.mappings());
100-
assertAliasesEqual(createIndexRequest.aliases(), parsedCreateIndexRequest.aliases());
101-
assertEquals(createIndexRequest.settings(), parsedCreateIndexRequest.settings());
56+
@Override
57+
protected void assertEqualInstances(CreateIndexRequest expectedInstance, CreateIndexRequest newInstance) {
58+
assertEquals(expectedInstance.settings(), newInstance.settings());
59+
assertAliasesEqual(expectedInstance.aliases(), newInstance.aliases());
60+
assertMappingsEqual(expectedInstance.mappings(), newInstance.mappings());
61+
}
10262

103-
BytesReference finalBytes = toShuffledXContent(parsedCreateIndexRequest, xContentType, EMPTY_PARAMS, humanReadable);
104-
ElasticsearchAssertions.assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
63+
@Override
64+
protected boolean supportsUnknownFields() {
65+
return false;
10566
}
10667

107-
public static void assertMappingsEqual(Map<String, String> expected, Map<String, String> actual) throws IOException {
68+
public static void assertMappingsEqual(Map<String, String> expected, Map<String, String> actual) {
10869
assertEquals(expected.keySet(), actual.keySet());
10970

11071
for (Map.Entry<String, String> expectedEntry : expected.entrySet()) {
11172
String expectedValue = expectedEntry.getValue();
11273
String actualValue = actual.get(expectedEntry.getKey());
113-
XContentParser expectedJson = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
114-
LoggingDeprecationHandler.INSTANCE, expectedValue);
115-
XContentParser actualJson = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
116-
LoggingDeprecationHandler.INSTANCE, actualValue);
117-
assertEquals(expectedJson.map(), actualJson.map());
74+
try (XContentParser expectedJson = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
75+
LoggingDeprecationHandler.INSTANCE, expectedValue);
76+
XContentParser actualJson = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
77+
LoggingDeprecationHandler.INSTANCE, actualValue)) {
78+
assertEquals(expectedJson.map(), actualJson.map());
79+
} catch (IOException e) {
80+
throw new RuntimeException(e);
81+
}
11882
}
11983
}
12084

121-
public static void assertAliasesEqual(Set<Alias> expected, Set<Alias> actual) throws IOException {
85+
public static void assertAliasesEqual(Set<Alias> expected, Set<Alias> actual) {
12286
assertEquals(expected, actual);
12387

12488
for (Alias expectedAlias : expected) {
@@ -132,4 +96,22 @@ public static void assertAliasesEqual(Set<Alias> expected, Set<Alias> actual) th
13296
}
13397
}
13498
}
99+
100+
public void testSerialization() throws IOException {
101+
CreateIndexRequest request = new CreateIndexRequest("foo");
102+
String mapping = Strings.toString(JsonXContent.contentBuilder().startObject()
103+
.startObject("type").endObject().endObject());
104+
request.mapping("my_type", mapping, XContentType.JSON);
105+
106+
try (BytesStreamOutput output = new BytesStreamOutput()) {
107+
request.writeTo(output);
108+
109+
try (StreamInput in = output.bytes().streamInput()) {
110+
CreateIndexRequest serialized = new CreateIndexRequest();
111+
serialized.readFrom(in);
112+
assertEquals(request.index(), serialized.index());
113+
assertEquals(mapping, serialized.mappings().get("my_type"));
114+
}
115+
}
116+
}
135117
}

test/framework/src/main/java/org/elasticsearch/index/RandomCreateIndexGenerator.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
3131
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
32+
import static org.elasticsearch.test.ESTestCase.frequently;
3233
import static org.elasticsearch.test.ESTestCase.randomAlphaOfLength;
3334
import static org.elasticsearch.test.ESTestCase.randomBoolean;
3435
import static org.elasticsearch.test.ESTestCase.randomIntBetween;
@@ -45,9 +46,14 @@ public static CreateIndexRequest randomCreateIndexRequest() throws IOException {
4546
String index = randomAlphaOfLength(5);
4647
CreateIndexRequest request = new CreateIndexRequest(index);
4748
randomAliases(request);
48-
if (randomBoolean()) {
49+
if (frequently()) {
4950
String type = randomAlphaOfLength(5);
50-
request.mapping(type, randomMapping(type));
51+
if (randomBoolean()) {
52+
request.mapping(type, randomMapping());
53+
} else {
54+
request.mapping(type, randomMapping(type));
55+
56+
}
5157
}
5258
if (randomBoolean()) {
5359
request.settings(randomIndexSettings());
@@ -76,6 +82,16 @@ public static Settings randomIndexSettings() {
7682
return builder.build();
7783
}
7884

85+
public static XContentBuilder randomMapping() throws IOException {
86+
XContentBuilder builder = XContentFactory.jsonBuilder();
87+
builder.startObject();
88+
89+
randomMappingFields(builder, true);
90+
91+
builder.endObject();
92+
return builder;
93+
}
94+
7995
public static XContentBuilder randomMapping(String type) throws IOException {
8096
XContentBuilder builder = XContentFactory.jsonBuilder();
8197
builder.startObject().startObject(type);

0 commit comments

Comments
 (0)