Skip to content

Commit 807a4fb

Browse files
authored
Remove type parameter from PutMappingRequest.buildFromSimplifiedDef() (elastic#50844)
Mappings built by this method should all be wrapped with _doc, so there's no need to pass the type any more. This also renames the method to simpleMapping, in line with CreateIndexRequest, to help migration by causing compilation errors; and changes the signature to take a String... rather than an Object.... Relates to elastic#41059
1 parent 979bfa6 commit 807a4fb

File tree

23 files changed

+43
-52
lines changed

23 files changed

+43
-52
lines changed

modules/mapper-extras/src/test/java/org/elasticsearch/index/query/RankFeatureQueryBuilderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class RankFeatureQueryBuilderTests extends AbstractQueryTestCase<RankFeat
4444

4545
@Override
4646
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
47-
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef("_doc",
47+
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
4848
"my_feature_field", "type=rank_feature",
4949
"my_negative_feature_field", "type=rank_feature,positive_score_impact=false",
5050
"my_feature_vector_field", "type=rank_features"))), MapperService.MergeReason.MAPPING_UPDATE);

modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws
8989
aliasField = randomAlphaOfLength(4);
9090

9191
docType = "_doc";
92-
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(docType,
92+
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
9393
queryField, "type=percolator", aliasField, "type=alias,path=" + queryField
9494
))), MapperService.MergeReason.MAPPING_UPDATE);
95-
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(docType,
95+
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
9696
STRING_FIELD_NAME, "type=text"
9797
))), MapperService.MergeReason.MAPPING_UPDATE);
9898
}

modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateWithNestedQueryBuilderTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public class PercolateWithNestedQueryBuilderTests extends PercolateQueryBuilderT
3535
@Override
3636
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
3737
super.initializeAdditionalMappings(mapperService);
38-
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(
39-
"_doc", "some_nested_object", "type=nested"))), MapperService.MergeReason.MAPPING_UPDATE);
38+
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
39+
"some_nested_object", "type=nested"))), MapperService.MergeReason.MAPPING_UPDATE);
4040
}
4141

4242
public void testDetectsNestedDocuments() throws IOException {

modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ public void testPercolatorQueryWithHighlighting() throws Exception {
447447
fieldMapping.append(",index_options=offsets");
448448
}
449449
assertAcked(client().admin().indices().prepareCreate("test")
450-
.setMapping("id", "type=keyword", "field1", fieldMapping, "query", "type=percolator")
450+
.setMapping("id", "type=keyword", "field1", fieldMapping.toString(), "query", "type=percolator")
451451
);
452452
client().prepareIndex("test").setId("1")
453453
.setSource(jsonBuilder().startObject()

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ public CreateIndexRequest mapping(Map<String, ?> source) {
253253
* A specialized simplified mapping source method, takes the form of simple properties definition:
254254
* ("field1", "type=string,store=true").
255255
*/
256-
public CreateIndexRequest simpleMapping(Object... source) {
257-
mapping(PutMappingRequest.buildFromSimplifiedDef(MapperService.SINGLE_MAPPING_NAME, source));
256+
public CreateIndexRequest simpleMapping(String... source) {
257+
mapping(PutMappingRequest.simpleMapping(source));
258258
return this;
259259
}
260260

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public CreateIndexRequestBuilder setMapping(Map<String, Object> source) {
135135
* A specialized simplified mapping source method, takes the form of simple properties definition:
136136
* ("field1", "type=string,store=true").
137137
*/
138-
public CreateIndexRequestBuilder setMapping(Object... source) {
138+
public CreateIndexRequestBuilder setMapping(String... source) {
139139
request.simpleMapping(source);
140140
return this;
141141
}

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

+7-15
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ public String source() {
172172
* Also supports metadata mapping fields such as `_all` and `_parent` as property definition, these metadata
173173
* mapping fields will automatically be put on the top level mapping object.
174174
*/
175-
public PutMappingRequest source(Object... source) {
176-
return source(buildFromSimplifiedDef(MapperService.SINGLE_MAPPING_NAME, source));
175+
public PutMappingRequest source(String... source) {
176+
return source(simpleMapping(source));
177177
}
178178

179179
public String origin() {
@@ -187,31 +187,26 @@ public PutMappingRequest origin(String origin) {
187187
}
188188

189189
/**
190-
* @param type
191-
* the mapping type
192190
* @param source
193191
* consisting of field/properties pairs (e.g. "field1",
194192
* "type=string,store=true")
195193
* @throws IllegalArgumentException
196194
* if the number of the source arguments is not divisible by two
197195
* @return the mappings definition
198196
*/
199-
public static XContentBuilder buildFromSimplifiedDef(String type, Object... source) {
197+
public static XContentBuilder simpleMapping(String... source) {
200198
if (source.length % 2 != 0) {
201199
throw new IllegalArgumentException("mapping source must be pairs of fieldnames and properties definition.");
202200
}
203201
try {
204202
XContentBuilder builder = XContentFactory.jsonBuilder();
205203
builder.startObject();
206-
if (type != null) {
207-
builder.startObject(type);
208-
}
209204

210205
for (int i = 0; i < source.length; i++) {
211-
String fieldName = source[i++].toString();
206+
String fieldName = source[i++];
212207
if (RESERVED_FIELDS.contains(fieldName)) {
213208
builder.startObject(fieldName);
214-
String[] s1 = Strings.splitStringByCommaToArray(source[i].toString());
209+
String[] s1 = Strings.splitStringByCommaToArray(source[i]);
215210
for (String s : s1) {
216211
String[] s2 = Strings.split(s, "=");
217212
if (s2.length != 2) {
@@ -225,13 +220,13 @@ public static XContentBuilder buildFromSimplifiedDef(String type, Object... sour
225220

226221
builder.startObject("properties");
227222
for (int i = 0; i < source.length; i++) {
228-
String fieldName = source[i++].toString();
223+
String fieldName = source[i++];
229224
if (RESERVED_FIELDS.contains(fieldName)) {
230225
continue;
231226
}
232227

233228
builder.startObject(fieldName);
234-
String[] s1 = Strings.splitStringByCommaToArray(source[i].toString());
229+
String[] s1 = Strings.splitStringByCommaToArray(source[i]);
235230
for (String s : s1) {
236231
String[] s2 = Strings.split(s, "=");
237232
if (s2.length != 2) {
@@ -242,9 +237,6 @@ public static XContentBuilder buildFromSimplifiedDef(String type, Object... sour
242237
builder.endObject();
243238
}
244239
builder.endObject();
245-
if (type != null) {
246-
builder.endObject();
247-
}
248240
builder.endObject();
249241
return builder;
250242
} catch (Exception e) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public PutMappingRequestBuilder setSource(String mappingSource, XContentType xCo
8787
* A specialized simplified mapping source method, takes the form of simple properties definition:
8888
* ("field1", "type=string,store=true").
8989
*/
90-
public PutMappingRequestBuilder setSource(Object... source) {
90+
public PutMappingRequestBuilder setSource(String... source) {
9191
request.source(source);
9292
return this;
9393
}

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestBuilder.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.common.settings.Settings;
2626
import org.elasticsearch.common.unit.ByteSizeValue;
2727
import org.elasticsearch.common.unit.TimeValue;
28-
import org.elasticsearch.common.xcontent.XContentType;
2928

3029

3130
public class RolloverRequestBuilder extends MasterNodeOperationRequestBuilder<RolloverRequest, RolloverResponse,
@@ -74,12 +73,12 @@ public RolloverRequestBuilder alias(Alias alias) {
7473
return this;
7574
}
7675

77-
public RolloverRequestBuilder mapping(String type, Object... source) {
76+
public RolloverRequestBuilder simpleMapping(String... source) {
7877
this.request.getCreateIndexRequest().simpleMapping(source);
7978
return this;
8079
}
8180

82-
public RolloverRequestBuilder mapping(String type, String source, XContentType xContentType) {
81+
public RolloverRequestBuilder mapping(String source) {
8382
this.request.getCreateIndexRequest().mapping(source);
8483
return this;
8584
}

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.elasticsearch.common.xcontent.XContentParser;
4444
import org.elasticsearch.common.xcontent.XContentType;
4545
import org.elasticsearch.common.xcontent.support.XContentMapValues;
46+
import org.elasticsearch.index.mapper.MapperService;
4647

4748
import java.io.IOException;
4849
import java.util.Collections;
@@ -295,8 +296,8 @@ public PutIndexTemplateRequest mapping(String type, Map<String, Object> source)
295296
* A specialized simplified mapping source method, takes the form of simple properties definition:
296297
* ("field1", "type=string,store=true").
297298
*/
298-
public PutIndexTemplateRequest mapping(String type, Object... source) {
299-
mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source));
299+
public PutIndexTemplateRequest mapping(String... source) {
300+
mapping(MapperService.SINGLE_MAPPING_NAME, PutMappingRequest.simpleMapping(source));
300301
return this;
301302
}
302303

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public PutIndexTemplateRequestBuilder addMapping(String type, String source, XCo
122122
* A specialized simplified mapping source method, takes the form of simple properties definition:
123123
* ("field1", "type=string,store=true").
124124
*/
125-
public PutIndexTemplateRequestBuilder addMapping(String type, Object... source) {
126-
request.mapping(type, source);
125+
public PutIndexTemplateRequestBuilder setMapping(String... source) {
126+
request.mapping(source);
127127
return this;
128128
}
129129

server/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequestTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public void testValidation() {
5050
}
5151

5252
/**
53-
* Test that {@link PutMappingRequest#buildFromSimplifiedDef(String, Object...)}
53+
* Test that {@link PutMappingRequest#simpleMapping(String...)}
5454
* rejects inputs where the {@code Object...} varargs of field name and properties are not
5555
* paired correctly
5656
*/
5757
public void testBuildFromSimplifiedDef() {
5858
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
59-
() -> PutMappingRequest.buildFromSimplifiedDef("type", "only_field"));
59+
() -> PutMappingRequest.simpleMapping("only_field"));
6060
assertEquals("mapping source must be pairs of fieldnames and properties definition.", e.getMessage());
6161
}
6262
}

server/src/test/java/org/elasticsearch/index/mapper/RangeFieldQueryStringQueryBuilderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class RangeFieldQueryStringQueryBuilderTests extends AbstractQueryTestCas
5454

5555
@Override
5656
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
57-
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef("_doc",
57+
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
5858
INTEGER_RANGE_FIELD_NAME, "type=integer_range",
5959
LONG_RANGE_FIELD_NAME, "type=long_range",
6060
FLOAT_RANGE_FIELD_NAME, "type=float_range",

server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ public void testExceptionUsingAnalyzerOnNumericField() {
348348

349349
@Override
350350
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
351-
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(
352-
"_doc",
351+
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
353352
"string_boost", "type=text,boost=4", "string_no_pos",
354353
"type=text,index_options=docs"))
355354
),

server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase<NestedQueryBu
6060

6161
@Override
6262
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
63-
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef("_doc",
63+
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
6464
STRING_FIELD_NAME, "type=text",
6565
INT_FIELD_NAME, "type=integer",
6666
DOUBLE_FIELD_NAME, "type=double",

server/src/test/java/org/elasticsearch/index/query/TermsSetQueryBuilderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
7777
@Override
7878
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
7979
String docType = "_doc";
80-
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(docType,
80+
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
8181
"m_s_m", "type=long"
8282
))), MapperService.MergeReason.MAPPING_UPDATE);
8383
}

server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public void testIndexTemplateWithAliases() throws Exception {
382382

383383
client().admin().indices().preparePutTemplate("template_with_aliases")
384384
.setPatterns(Collections.singletonList("te*"))
385-
.addMapping("_doc", "type", "type=keyword", "field", "type=text")
385+
.setMapping("type", "type=keyword", "field", "type=text")
386386
.addAlias(new Alias("simple_alias"))
387387
.addAlias(new Alias("templated_alias-{index}"))
388388
.addAlias(new Alias("filtered_alias").filter("{\"term\":{\"type\":\"type2\"}}"))
@@ -622,7 +622,7 @@ public void testStrictAliasParsingInIndicesCreatedViaTemplates() throws Exceptio
622622
client().admin().indices().preparePutTemplate("template1")
623623
.setPatterns(Collections.singletonList("a*"))
624624
.setOrder(0)
625-
.addMapping("test", "field", "type=text")
625+
.setMapping("field", "type=text")
626626
.addAlias(new Alias("alias1").filter(termQuery("field", "value"))).get();
627627
// Indexing into b index should fail, since there is field with name 'field' in the mapping
628628
client().admin().indices().preparePutTemplate("template4")
@@ -706,7 +706,7 @@ public void testOrderAndVersion() {
706706
.setPatterns(Collections.singletonList("te*"))
707707
.setVersion(version)
708708
.setOrder(order)
709-
.addMapping("test", "field", "type=text")
709+
.setMapping("field", "type=text")
710710
.get());
711711

712712
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates("versioned_template").get();

server/src/test/java/org/elasticsearch/search/searchafter/SearchAfterIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private void createIndexMappingsFromObjectType(String indexName, List<Object> ty
293293
fail("Can't match type [" + type + "]");
294294
}
295295
}
296-
indexRequestBuilder.setMapping(mappings.toArray()).get();
296+
indexRequestBuilder.setMapping(mappings.toArray(new String[0])).get();
297297
ensureGreen();
298298
}
299299

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public void onRemoval(ShardId shardId, Accountable accountable) {
374374
});
375375

376376
if (registerType) {
377-
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef("_doc",
377+
mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
378378
STRING_FIELD_NAME, "type=text",
379379
STRING_FIELD_NAME_2, "type=keyword",
380380
STRING_ALIAS_FIELD_NAME, "type=alias,path=" + STRING_FIELD_NAME,
@@ -400,11 +400,11 @@ public void onRemoval(ShardId shardId, Accountable accountable) {
400400
testCase.initializeAdditionalMappings(mapperService);
401401
}
402402
}
403-
403+
404404
public static Predicate<String> indexNameMatcher() {
405405
// Simplistic index name matcher used for testing
406406
return pattern -> Regex.simpleMatch(pattern, index.getName());
407-
}
407+
}
408408

409409
@Override
410410
public void close() throws IOException {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ protected IndexService createIndex(String index, Settings settings, XContentBuil
284284
/**
285285
* Create a new index on the singleton node with the provided index settings.
286286
*/
287-
protected IndexService createIndex(String index, Settings settings, String type, Object... mappings) {
287+
protected IndexService createIndex(String index, Settings settings, String type, String... mappings) {
288288
CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings);
289289
if (type != null) {
290290
createIndexRequestBuilder.setMapping(mappings);

x-pack/plugin/core/src/test/java/org/elasticsearch/snapshots/SourceOnlySnapshotIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private IndexRequestBuilder[] snashotAndRestore(final String sourceIdx,
227227

228228
CreateIndexRequestBuilder createIndexRequestBuilder = prepareCreate(sourceIdx, 0, Settings.builder()
229229
.put("number_of_shards", numShards).put("number_of_replicas", 0));
230-
List<Object> mappings = new ArrayList<>();
230+
List<String> mappings = new ArrayList<>();
231231
if (requireRouting) {
232232
mappings.addAll(Arrays.asList("_routing", "required=true"));
233233
}
@@ -236,7 +236,7 @@ private IndexRequestBuilder[] snashotAndRestore(final String sourceIdx,
236236
mappings.addAll(Arrays.asList("nested", "type=nested", "incorrect", "type=object"));
237237
}
238238
if (mappings.isEmpty() == false) {
239-
createIndexRequestBuilder.setMapping(mappings.toArray());
239+
createIndexRequestBuilder.setMapping(mappings.toArray(new String[0]));
240240
}
241241
assertAcked(createIndexRequestBuilder);
242242
ensureGreen();

x-pack/plugin/security/src/test/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public Settings nodeSettings(int nodeOrdinal) {
126126
.build();
127127
}
128128

129-
public void testRandom() throws Exception {
129+
public void testRandom() {
130130
int j = 0;
131131
Map<String, Object> doc = new HashMap<>();
132132
String[] fieldMappers = new String[(allowedFields.size() + disAllowedFields.size()) * 2];
@@ -141,7 +141,7 @@ public void testRandom() throws Exception {
141141
doc.put(field, "value");
142142
}
143143
assertAcked(client().admin().indices().prepareCreate("test")
144-
.setMapping((Object[])fieldMappers)
144+
.setMapping(fieldMappers)
145145
);
146146
client().prepareIndex("test").setId("1").setSource(doc).setRefreshPolicy(IMMEDIATE).get();
147147

x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
6767

6868
@Override
6969
protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
70-
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(docType,
70+
mapperService.merge(docType, new CompressedXContent(Strings.toString(PutMappingRequest.simpleMapping(
7171
fieldName(), "type=shape"))), MapperService.MergeReason.MAPPING_UPDATE);
7272
}
7373

0 commit comments

Comments
 (0)