Skip to content

Commit 0ac9c16

Browse files
author
Christoph Büscher
committed
[Docs] Fix Java Api index administration usage (#28133)
The Java API documentation for index administration currenty is wrong because the PutMappingRequestBuilder#setSource(Object... source) an CreateIndexRequestBuilder#addMapping(String type, Object... source) methods delegate to methods that check that the input arguments are valid key/value pairs: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-admin-indices.html This changes the docs so the java api code examples are included from documentation integration tests so we detect compile and runtime issues earlier. Closes #28131
1 parent 9046912 commit 0ac9c16

File tree

2 files changed

+71
-38
lines changed

2 files changed

+71
-38
lines changed
Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[[java-admin-indices-put-mapping]]
2+
23
==== Put Mapping
34

45
The PUT mapping API allows you to add a new type while creating an index:
@@ -13,32 +14,9 @@ include-tagged::{client-tests}/IndicesDocumentationIT.java[index-with-mapping]
1314

1415
The PUT mapping API also allows to add a new type to an existing index:
1516

16-
[source,java]
17+
["source","java",subs="attributes,callouts,macros"]
1718
--------------------------------------------------
18-
client.admin().indices().preparePutMapping("twitter") <1>
19-
.setType("user") <2>
20-
.setSource("{\n" + <3>
21-
" \"properties\": {\n" +
22-
" \"name\": {\n" +
23-
" \"type\": \"text\"\n" +
24-
" }\n" +
25-
" }\n" +
26-
"}")
27-
.get();
28-
29-
// You can also provide the type in the source document
30-
client.admin().indices().preparePutMapping("twitter")
31-
.setType("user")
32-
.setSource("{\n" +
33-
" \"user\":{\n" + <4>
34-
" \"properties\": {\n" +
35-
" \"name\": {\n" +
36-
" \"type\": \"text\"\n" +
37-
" }\n" +
38-
" }\n" +
39-
" }\n" +
40-
"}")
41-
.get();
19+
include-tagged::{client-tests}/IndicesDocumentationIT.java[putMapping-request-source]
4220
--------------------------------------------------
4321
<1> Puts a mapping on existing index called `twitter`
4422
<2> Adds a `user` mapping type.
@@ -47,20 +25,12 @@ client.admin().indices().preparePutMapping("twitter")
4725

4826
You can use the same API to update an existing mapping:
4927

50-
[source,java]
28+
["source","java",subs="attributes,callouts,macros"]
5129
--------------------------------------------------
52-
client.admin().indices().preparePutMapping("twitter") <1>
53-
.setType("user") <2>
54-
.setSource("{\n" + <3>
55-
" \"properties\": {\n" +
56-
" \"user_name\": {\n" +
57-
" \"type\": \"text\"\n" +
58-
" }\n" +
59-
" }\n" +
60-
"}")
61-
.get();
30+
include-tagged::{client-tests}/IndicesDocumentationIT.java[putMapping-request-source-append]
6231
--------------------------------------------------
6332
<1> Puts a mapping on existing index called `twitter`
6433
<2> Updates the `user` mapping type.
6534
<3> This `user` has now a new field `user_name`
6635

36+
:base-dir!:

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

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,32 @@
1919

2020
package org.elasticsearch.client.documentation;
2121

22-
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
2322
import org.elasticsearch.client.Client;
23+
import org.elasticsearch.common.xcontent.XContentType;
2424
import org.elasticsearch.test.ESIntegTestCase;
2525

26+
/**
27+
* This class is used to generate the Java indices administration documentation.
28+
* You need to wrap your code between two tags like:
29+
* // tag::example[]
30+
* // end::example[]
31+
*
32+
* Where example is your tag name.
33+
*
34+
* Then in the documentation, you can extract what is between tag and end tags
35+
* with ["source","java",subs="attributes,callouts,macros"]
36+
* --------------------------------------------------
37+
* include-tagged::{client-tests}/IndicesDocumentationIT.java[your-example-tag-here]
38+
* --------------------------------------------------
39+
*/
2640
public class IndicesDocumentationIT extends ESIntegTestCase {
2741

28-
public void createMappings() {
42+
/**
43+
* This test method is used to generate the Put Mapping Java Indices API documentation
44+
* at "docs/java-api/admin/indices/put-mapping.asciidoc" so the documentation gets tested
45+
* so that it compiles and runs without throwing errors at runtime.
46+
*/
47+
public void testPutMappingDocumentation() throws Exception {
2948
Client client = client();
3049

3150
// tag::index-with-mapping
@@ -39,6 +58,50 @@ public void createMappings() {
3958
"}")
4059
.get();
4160
// end::index-with-mapping
61+
62+
// we need to delete in order to create a fresh new index with another type
63+
client.admin().indices().prepareDelete("twitter").get();
64+
client.admin().indices().prepareCreate("twitter").get();
65+
66+
// tag::putMapping-request-source
67+
client.admin().indices().preparePutMapping("twitter") // <1>
68+
.setType("user") // <2>
69+
.setSource("{\n" + // <3>
70+
" \"properties\": {\n" +
71+
" \"name\": {\n" +
72+
" \"type\": \"text\"\n" +
73+
" }\n" +
74+
" }\n" +
75+
"}", XContentType.JSON)
76+
.get();
77+
78+
// You can also provide the type in the source document
79+
client.admin().indices().preparePutMapping("twitter")
80+
.setType("user")
81+
.setSource("{\n" +
82+
" \"user\":{\n" + // <4>
83+
" \"properties\": {\n" +
84+
" \"name\": {\n" +
85+
" \"type\": \"text\"\n" +
86+
" }\n" +
87+
" }\n" +
88+
" }\n" +
89+
"}", XContentType.JSON)
90+
.get();
91+
// end::putMapping-request-source
92+
93+
// tag::putMapping-request-source-append
94+
client.admin().indices().preparePutMapping("twitter") // <1>
95+
.setType("user") // <2>
96+
.setSource("{\n" + // <3>
97+
" \"properties\": {\n" +
98+
" \"user_name\": {\n" +
99+
" \"type\": \"text\"\n" +
100+
" }\n" +
101+
" }\n" +
102+
"}", XContentType.JSON)
103+
.get();
104+
// end::putMapping-request-source-append
42105
}
43106

44107
}

0 commit comments

Comments
 (0)