Skip to content

Commit 457a69e

Browse files
committed
More clean-up in CreateIndexRequest.
1 parent 8519ca2 commit 457a69e

File tree

3 files changed

+69
-55
lines changed

3 files changed

+69
-55
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.ElasticsearchParseException;
2424
import org.elasticsearch.action.IndicesRequest;
2525
import org.elasticsearch.action.admin.indices.alias.Alias;
26-
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
2726
import org.elasticsearch.action.support.ActiveShardCount;
2827
import org.elasticsearch.action.support.IndicesOptions;
2928
import org.elasticsearch.client.TimedRequest;
@@ -34,20 +33,16 @@
3433
import org.elasticsearch.common.bytes.BytesReference;
3534
import org.elasticsearch.common.settings.Settings;
3635
import org.elasticsearch.common.xcontent.DeprecationHandler;
37-
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
3836
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
3937
import org.elasticsearch.common.xcontent.ToXContentObject;
4038
import org.elasticsearch.common.xcontent.XContentBuilder;
4139
import org.elasticsearch.common.xcontent.XContentFactory;
4240
import org.elasticsearch.common.xcontent.XContentHelper;
4341
import org.elasticsearch.common.xcontent.XContentParser;
4442
import org.elasticsearch.common.xcontent.XContentType;
45-
import org.elasticsearch.index.mapper.MapperService;
4643

4744
import java.io.IOException;
4845
import java.io.InputStream;
49-
import java.io.UncheckedIOException;
50-
import java.util.HashMap;
5146
import java.util.HashSet;
5247
import java.util.Map;
5348
import java.util.Objects;
@@ -65,7 +60,10 @@ public class CreateIndexRequest extends TimedRequest implements Validatable, Ind
6560

6661
private final String index;
6762
private Settings settings = EMPTY_SETTINGS;
68-
private final Map<String, String> mappings = new HashMap<>();
63+
64+
private BytesReference mappings;
65+
private XContentType mappingsXContentType;
66+
6967
private final Set<Alias> aliases = new HashSet<>();
7068

7169
private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;
@@ -153,6 +151,8 @@ public CreateIndexRequest settings(Map<String, ?> source) {
153151
/**
154152
* Adds mapping that will be added when the index gets created.
155153
*
154+
* Note that the definition should *not* be nested under a type name.
155+
*
156156
* @param source The mapping source
157157
* @param xContentType The content type of the source
158158
*/
@@ -163,22 +163,7 @@ public CreateIndexRequest mapping(String source, XContentType xContentType) {
163163
/**
164164
* Adds mapping that will be added when the index gets created.
165165
*
166-
* @param source The mapping source
167-
* @param xContentType the content type of the mapping source
168-
*/
169-
private CreateIndexRequest mapping(BytesReference source, XContentType xContentType) {
170-
Objects.requireNonNull(xContentType);
171-
try {
172-
mappings.put(MapperService.SINGLE_MAPPING_NAME,
173-
XContentHelper.convertToJson(source, false, false, xContentType));
174-
return this;
175-
} catch (IOException e) {
176-
throw new UncheckedIOException("failed to convert to json", e);
177-
}
178-
}
179-
180-
/**
181-
* Adds mapping that will be added when the index gets created.
166+
* Note that the definition should *not* be nested under a type name.
182167
*
183168
* @param source The mapping source
184169
*/
@@ -189,26 +174,43 @@ public CreateIndexRequest mapping(XContentBuilder source) {
189174
/**
190175
* Adds mapping that will be added when the index gets created.
191176
*
177+
* Note that the definition should *not* be nested under a type name.
178+
*
192179
* @param source The mapping source
193180
*/
194181
public CreateIndexRequest mapping(Map<String, ?> source) {
195182
try {
196183
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
197184
builder.map(source);
198-
return mapping(builder);
185+
return mapping(BytesReference.bytes(builder), builder.contentType());
199186
} catch (IOException e) {
200187
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
201188
}
202189
}
203-
190+
191+
/**
192+
* Adds mapping that will be added when the index gets created.
193+
*
194+
* Note that the definition should *not* be nested under a type name.
195+
*
196+
* @param source The mapping source
197+
* @param xContentType the content type of the mapping source
198+
*/
199+
private CreateIndexRequest mapping(BytesReference source, XContentType xContentType) {
200+
Objects.requireNonNull(xContentType);
201+
mappings = source;
202+
mappingsXContentType = xContentType;
203+
return this;
204+
}
205+
204206
/**
205207
* Sets the aliases that will be associated with the index when it gets created
206208
*/
207209
public CreateIndexRequest aliases(Map<String, ?> source) {
208210
try {
209211
XContentBuilder builder = XContentFactory.jsonBuilder();
210212
builder.map(source);
211-
return aliases(BytesReference.bytes(builder));
213+
return aliases(BytesReference.bytes(builder), builder.contentType());
212214
} catch (IOException e) {
213215
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
214216
}
@@ -218,23 +220,23 @@ public CreateIndexRequest aliases(Map<String, ?> source) {
218220
* Sets the aliases that will be associated with the index when it gets created
219221
*/
220222
public CreateIndexRequest aliases(XContentBuilder source) {
221-
return aliases(BytesReference.bytes(source));
223+
return aliases(BytesReference.bytes(source), source.contentType());
222224
}
223225

224226
/**
225227
* Sets the aliases that will be associated with the index when it gets created
226228
*/
227-
public CreateIndexRequest aliases(String source) {
228-
return aliases(new BytesArray(source));
229+
public CreateIndexRequest aliases(String source, XContentType contentType) {
230+
return aliases(new BytesArray(source), contentType);
229231
}
230232

231233
/**
232234
* Sets the aliases that will be associated with the index when it gets created
233235
*/
234-
public CreateIndexRequest aliases(BytesReference source) {
236+
public CreateIndexRequest aliases(BytesReference source, XContentType contentType) {
235237
// EMPTY is safe here because we never call namedObject
236-
try (XContentParser parser = XContentHelper
237-
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, source)) {
238+
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY,
239+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, source, contentType)) {
238240
//move to the first alias
239241
parser.nextToken();
240242
while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) {
@@ -256,43 +258,56 @@ public CreateIndexRequest alias(Alias alias) {
256258

257259
/**
258260
* Sets the settings and mappings as a single source.
261+
*
262+
* Note that the mapping definition should *not* be nested under a type name.
259263
*/
260264
public CreateIndexRequest source(String source, XContentType xContentType) {
261265
return source(new BytesArray(source), xContentType);
262266
}
263267

264268
/**
265269
* Sets the settings and mappings as a single source.
270+
*
271+
* Note that the mapping definition should *not* be nested under a type name.
266272
*/
267273
public CreateIndexRequest source(XContentBuilder source) {
268274
return source(BytesReference.bytes(source), source.contentType());
269275
}
270276

271277
/**
272278
* Sets the settings and mappings as a single source.
279+
*
280+
* Note that the mapping definition should *not* be nested under a type name.
273281
*/
274282
public CreateIndexRequest source(byte[] source, XContentType xContentType) {
275283
return source(source, 0, source.length, xContentType);
276284
}
277285

278286
/**
279287
* Sets the settings and mappings as a single source.
288+
*
289+
* Note that the mapping definition should *not* be nested under a type name.
280290
*/
281291
public CreateIndexRequest source(byte[] source, int offset, int length, XContentType xContentType) {
282292
return source(new BytesArray(source, offset, length), xContentType);
283293
}
284294

285295
/**
286296
* Sets the settings and mappings as a single source.
297+
*
298+
* Note that the mapping definition should *not* be nested under a type name.
287299
*/
288300
public CreateIndexRequest source(BytesReference source, XContentType xContentType) {
289301
Objects.requireNonNull(xContentType);
290-
source(XContentHelper.convertToMap(source, false, xContentType).v2(), LoggingDeprecationHandler.INSTANCE);
302+
source(XContentHelper.convertToMap(source, false, xContentType).v2(),
303+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION);
291304
return this;
292305
}
293306

294307
/**
295308
* Sets the settings and mappings as a single source.
309+
*
310+
* Note that the mapping definition should *not* be nested under a type name.
296311
*/
297312
@SuppressWarnings("unchecked")
298313
public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler deprecationHandler) {
@@ -301,17 +316,20 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
301316
if (SETTINGS.match(name, deprecationHandler)) {
302317
settings((Map<String, Object>) entry.getValue());
303318
} else if (MAPPINGS.match(name, deprecationHandler)) {
304-
Map<String, Object> mappings = (Map<String, Object>) entry.getValue();
305-
mapping(mappings);
319+
mapping((Map<String, Object>) entry.getValue());
306320
} else if (ALIASES.match(name, deprecationHandler)) {
307321
aliases((Map<String, Object>) entry.getValue());
308322
}
309323
}
310324
return this;
311325
}
312326

313-
public Map<String, String> mappings() {
314-
return this.mappings;
327+
public BytesReference mappings() {
328+
return mappings;
329+
}
330+
331+
public XContentType mappingsXContentType() {
332+
return mappingsXContentType;
315333
}
316334

317335
public Set<Alias> aliases() {
@@ -358,10 +376,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
358376
settings.toXContent(builder, params);
359377
builder.endObject();
360378

361-
if (!mappings.isEmpty()) {
362-
String mapping = mappings.get(MapperService.SINGLE_MAPPING_NAME);
363-
try (InputStream stream = new BytesArray(mapping).streamInput()) {
364-
builder.rawField(MAPPINGS.getPreferredName(), stream, XContentType.JSON);
379+
if (mappings != null) {
380+
try (InputStream stream = mappings.streamInput()) {
381+
builder.rawField(MAPPINGS.getPreferredName(), stream, mappingsXContentType);
365382
}
366383
}
367384

client/rest-high-level/src/test/java/org/elasticsearch/client/indices/CreateIndexRequestTests.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
import org.elasticsearch.action.admin.indices.alias.Alias;
2323
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2424
import org.elasticsearch.common.xcontent.XContentParser;
25-
import org.elasticsearch.common.xcontent.json.JsonXContent;
2625
import org.elasticsearch.test.AbstractXContentTestCase;
2726

2827
import java.io.IOException;
29-
import java.util.Map;
3028
import java.util.Set;
3129
import java.util.function.Predicate;
3230

@@ -49,20 +47,19 @@ protected CreateIndexRequest doParseInstance(XContentParser parser) throws IOExc
4947
}
5048

5149
@Override
52-
protected void assertEqualInstances(CreateIndexRequest expectedInstance, CreateIndexRequest newInstance) {
53-
assertEquals(expectedInstance.settings(), newInstance.settings());
54-
assertAliasesEqual(expectedInstance.aliases(), newInstance.aliases());
55-
assertMappingsEqual(expectedInstance.mappings(), newInstance.mappings());
50+
protected void assertEqualInstances(CreateIndexRequest expected, CreateIndexRequest actual) {
51+
assertEquals(expected.settings(), actual.settings());
52+
assertAliasesEqual(expected.aliases(), actual.aliases());
53+
assertMappingsEqual(expected, actual);
5654
}
5755

58-
private void assertMappingsEqual(Map<String, String> expected, Map<String, String> actual) {
59-
assertEquals(expected.keySet(), actual.keySet());
60-
61-
for (Map.Entry<String, String> expectedEntry : expected.entrySet()) {
62-
String expectedValue = expectedEntry.getValue();
63-
String actualValue = actual.get(expectedEntry.getKey());
64-
try (XContentParser expectedJson = createParser(JsonXContent.jsonXContent, expectedValue);
65-
XContentParser actualJson = createParser(JsonXContent.jsonXContent, actualValue)) {
56+
private void assertMappingsEqual(CreateIndexRequest expected, CreateIndexRequest actual) {
57+
if (expected.mappings() == null) {
58+
assertNull(actual.mappings());
59+
} else {
60+
assertNotNull(actual.mappings());
61+
try (XContentParser expectedJson = createParser(expected.mappingsXContentType().xContent(), expected.mappings());
62+
XContentParser actualJson = createParser(actual.mappingsXContentType().xContent(), actual.mappings())) {
6663
assertEquals(expectedJson.map(), actualJson.map());
6764
} catch (IOException e) {
6865
throw new RuntimeException(e);

test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
package org.elasticsearch.test;
2121

22-
import org.elasticsearch.common.CheckedBiFunction;
2322
import org.elasticsearch.common.CheckedBiConsumer;
23+
import org.elasticsearch.common.CheckedBiFunction;
2424
import org.elasticsearch.common.CheckedFunction;
2525
import org.elasticsearch.common.Strings;
2626
import org.elasticsearch.common.bytes.BytesReference;

0 commit comments

Comments
 (0)