Skip to content

Commit 565032d

Browse files
author
Christoph Büscher
committed
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 5c4ca13 commit 565032d

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
[[java-admin-indices-put-mapping]]
22
==== Put Mapping
33

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

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

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

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

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

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,13 @@ public PutMappingRequest source(Object... source) {
183183
}
184184

185185
/**
186-
* @param type the mapping type
187-
* @param source consisting of field/properties pairs (e.g. "field1",
188-
* "type=string,store=true"). If the number of arguments is not
189-
* divisible by two an {@link IllegalArgumentException} is thrown
186+
* @param type
187+
* the mapping type
188+
* @param source
189+
* consisting of field/properties pairs (e.g. "field1",
190+
* "type=string,store=true")
191+
* @throws IllegalArgumentException
192+
* if the number of the source arguments is not divisible by two
190193
* @return the mappings definition
191194
*/
192195
public static XContentBuilder buildFromSimplifiedDef(String type, Object... source) {

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ public void testValidation() {
6767
" concrete index: [[foo/bar]] and indices: [myindex];");
6868
}
6969

70+
/**
71+
* Test that {@link PutMappingRequest#buildFromSimplifiedDef(String, Object...)}
72+
* rejects inputs where the {@code Object...} varargs of field name and properties are not
73+
* paired correctly
74+
*/
7075
public void testBuildFromSimplifiedDef() {
71-
// test that method rejects input where input varargs fieldname/properites are not paired correctly
7276
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
7377
() -> PutMappingRequest.buildFromSimplifiedDef("type", "only_field"));
7478
assertEquals("mapping source must be pairs of fieldnames and properties definition.", e.getMessage());

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

+27-8
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)