Skip to content

Commit 3616526

Browse files
author
Christoph Büscher
authored
Fix put mappings java API documentation (#31955)
The current docs of the put-mapping Java API is currently broken. It its current form, it creates an index and uses the whole mapping definition given as a JSON string as the type name. Since we didn't check the index created in the IndicesDocumentationIT so far this went unnoticed. This change adds test to catch this error to the documentation test, changes the documentation so it works correctly now and adds an input validation to PutMappingRequest#buildFromSimplifiedDef() which was used internally to reject calls where no mapping definition is given. Closes #31906
1 parent 6148668 commit 3616526

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

docs/java-api/admin/indices/put-mapping.asciidoc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
==== Put Mapping
44

5-
The PUT mapping API allows you to add a new type while creating an index:
5+
You can add mappings for a new type at index creation time:
66

77
["source","java",subs="attributes,callouts,macros"]
88
--------------------------------------------------
99
include-tagged::{client-tests}/IndicesDocumentationIT.java[index-with-mapping]
1010
--------------------------------------------------
1111
<1> <<java-admin-indices-create-index,Creates an index>> called `twitter`
12-
<2> It also adds a `tweet` mapping type.
12+
<2> Add a `tweet` type with a field called `message` that has the datatype `text`.
1313

14+
There are several variants of the above `addMapping` method, some taking an
15+
`XContentBuilder` or a `Map` with the mapping definition as arguments. Make sure
16+
to check the javadocs to pick the simplest one for your use case.
1417

15-
The PUT mapping API also allows to add a new type to an existing index:
18+
The PUT mapping API also allows to specify the mapping of a type after index
19+
creation. In this case you can provide the mapping as a String similar to the
20+
Rest API syntax:
1621

1722
["source","java",subs="attributes,callouts,macros"]
1823
--------------------------------------------------

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,13 @@ public PutMappingRequest source(Object... source) {
184184
}
185185

186186
/**
187-
* @param type the mapping type
188-
* @param source consisting of field/properties pairs (e.g. "field1",
189-
* "type=string,store=true"). If the number of arguments is not
190-
* divisible by two an {@link IllegalArgumentException} is thrown
187+
* @param type
188+
* the mapping type
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
191194
* @return the mappings definition
192195
*/
193196
public static XContentBuilder buildFromSimplifiedDef(String type, Object... source) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ public void testValidation() {
7676
" concrete index: [[foo/bar]] and indices: [myindex];");
7777
}
7878

79+
/**
80+
* Test that {@link PutMappingRequest#buildFromSimplifiedDef(String, Object...)}
81+
* rejects inputs where the {@code Object...} varargs of field name and properties are not
82+
* paired correctly
83+
*/
7984
public void testBuildFromSimplifiedDef() {
80-
// test that method rejects input where input varargs fieldname/properites are not paired correctly
8185
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
8286
() -> PutMappingRequest.buildFromSimplifiedDef("type", "only_field"));
8387
assertEquals("mapping source must be pairs of fieldnames and properties definition.", e.getMessage());

server/src/test/java/org/elasticsearch/client/documentation/IndicesDocumentationIT.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@
1919

2020
package org.elasticsearch.client.documentation;
2121

22+
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
2223
import org.elasticsearch.client.Client;
24+
import org.elasticsearch.cluster.metadata.MappingMetaData;
25+
import org.elasticsearch.common.collect.ImmutableOpenMap;
2326
import org.elasticsearch.common.xcontent.XContentType;
2427
import org.elasticsearch.test.ESIntegTestCase;
2528

29+
import java.util.HashMap;
30+
import java.util.Map;
31+
32+
import static java.util.Collections.singletonMap;
33+
import static org.hamcrest.Matchers.instanceOf;
34+
2635
/**
2736
* This class is used to generate the Java indices administration documentation.
2837
* You need to wrap your code between two tags like:
@@ -48,16 +57,14 @@ public void testPutMappingDocumentation() throws Exception {
4857
Client client = client();
4958

5059
// tag::index-with-mapping
51-
client.admin().indices().prepareCreate("twitter") // <1>
52-
.addMapping("\"tweet\": {\n" + // <2>
53-
" \"properties\": {\n" +
54-
" \"message\": {\n" +
55-
" \"type\": \"text\"\n" +
56-
" }\n" +
57-
" }\n" +
58-
"}")
60+
client.admin().indices().prepareCreate("twitter") // <1>
61+
.addMapping("tweet", "message", "type=text") // <2>
5962
.get();
6063
// end::index-with-mapping
64+
GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get();
65+
assertEquals(1, getMappingsResponse.getMappings().size());
66+
ImmutableOpenMap<String, MappingMetaData> indexMapping = getMappingsResponse.getMappings().get("twitter");
67+
assertThat(indexMapping.get("tweet"), instanceOf(MappingMetaData.class));
6168

6269
// we need to delete in order to create a fresh new index with another type
6370
client.admin().indices().prepareDelete("twitter").get();
@@ -89,6 +96,11 @@ public void testPutMappingDocumentation() throws Exception {
8996
"}", XContentType.JSON)
9097
.get();
9198
// end::putMapping-request-source
99+
getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get();
100+
assertEquals(1, getMappingsResponse.getMappings().size());
101+
indexMapping = getMappingsResponse.getMappings().get("twitter");
102+
assertEquals(singletonMap("properties", singletonMap("name", singletonMap("type", "text"))),
103+
indexMapping.get("user").getSourceAsMap());
92104

93105
// tag::putMapping-request-source-append
94106
client.admin().indices().preparePutMapping("twitter") // <1>
@@ -102,6 +114,13 @@ public void testPutMappingDocumentation() throws Exception {
102114
"}", XContentType.JSON)
103115
.get();
104116
// end::putMapping-request-source-append
117+
getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get();
118+
assertEquals(1, getMappingsResponse.getMappings().size());
119+
indexMapping = getMappingsResponse.getMappings().get("twitter");
120+
Map<String, Map<?,?>> expected = new HashMap<>();
121+
expected.put("name", singletonMap("type", "text"));
122+
expected.put("user_name", singletonMap("type", "text"));
123+
assertEquals(expected, indexMapping.get("user").getSourceAsMap().get("properties"));
105124
}
106125

107126
}

0 commit comments

Comments
 (0)