Skip to content

[Docs] Fix Java Api index administration usage (#28133) #28260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 6 additions & 36 deletions docs/java-api/admin/indices/put-mapping.asciidoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[[java-admin-indices-put-mapping]]

==== Put Mapping

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

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

[source,java]
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
client.admin().indices().preparePutMapping("twitter") <1>
.setType("user") <2>
.setSource("{\n" + <3>
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}")
.get();
// You can also provide the type in the source document
client.admin().indices().preparePutMapping("twitter")
.setType("user")
.setSource("{\n" +
" \"user\":{\n" + <4>
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}")
.get();
include-tagged::{client-tests}/IndicesDocumentationIT.java[putMapping-request-source]
--------------------------------------------------
<1> Puts a mapping on existing index called `twitter`
<2> Adds a `user` mapping type.
Expand All @@ -47,20 +25,12 @@ client.admin().indices().preparePutMapping("twitter")

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

[source,java]
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
client.admin().indices().preparePutMapping("twitter") <1>
.setType("user") <2>
.setSource("{\n" + <3>
" \"properties\": {\n" +
" \"user_name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}")
.get();
include-tagged::{client-tests}/IndicesDocumentationIT.java[putMapping-request-source-append]
--------------------------------------------------
<1> Puts a mapping on existing index called `twitter`
<2> Updates the `user` mapping type.
<3> This `user` has now a new field `user_name`

:base-dir!:
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,32 @@

package org.elasticsearch.client.documentation;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESIntegTestCase;

/**
* This class is used to generate the Java indices administration documentation.
* You need to wrap your code between two tags like:
* // tag::example[]
* // end::example[]
*
* Where example is your tag name.
*
* Then in the documentation, you can extract what is between tag and end tags
* with ["source","java",subs="attributes,callouts,macros"]
* --------------------------------------------------
* include-tagged::{client-tests}/IndicesDocumentationIT.java[your-example-tag-here]
* --------------------------------------------------
*/
public class IndicesDocumentationIT extends ESIntegTestCase {

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

// tag::index-with-mapping
Expand All @@ -39,6 +58,50 @@ public void createMappings() {
"}")
.get();
// end::index-with-mapping

// we need to delete in order to create a fresh new index with another type
client.admin().indices().prepareDelete("twitter").get();
client.admin().indices().prepareCreate("twitter").get();

// tag::putMapping-request-source
client.admin().indices().preparePutMapping("twitter") // <1>
.setType("user") // <2>
.setSource("{\n" + // <3>
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON)
.get();

// You can also provide the type in the source document
client.admin().indices().preparePutMapping("twitter")
.setType("user")
.setSource("{\n" +
" \"user\":{\n" + // <4>
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON)
.get();
// end::putMapping-request-source

// tag::putMapping-request-source-append
client.admin().indices().preparePutMapping("twitter") // <1>
.setType("user") // <2>
.setSource("{\n" + // <3>
" \"properties\": {\n" +
" \"user_name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON)
.get();
// end::putMapping-request-source-append
}

}