|
20 | 20 | package org.elasticsearch.action.admin.indices.create;
|
21 | 21 |
|
22 | 22 | import com.carrotsearch.hppc.cursors.ObjectCursor;
|
| 23 | + |
23 | 24 | import org.elasticsearch.ElasticsearchException;
|
24 | 25 | import org.elasticsearch.action.ActionListener;
|
25 | 26 | import org.elasticsearch.action.UnavailableShardsException;
|
|
28 | 29 | import org.elasticsearch.action.search.SearchResponse;
|
29 | 30 | import org.elasticsearch.action.support.ActiveShardCount;
|
30 | 31 | import org.elasticsearch.action.support.IndicesOptions;
|
| 32 | +import org.elasticsearch.client.Client; |
31 | 33 | import org.elasticsearch.cluster.ClusterState;
|
32 | 34 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
33 | 35 | import org.elasticsearch.cluster.metadata.MetaData;
|
34 | 36 | import org.elasticsearch.cluster.node.DiscoveryNode;
|
35 | 37 | import org.elasticsearch.common.collect.ImmutableOpenMap;
|
36 | 38 | import org.elasticsearch.common.settings.Settings;
|
37 | 39 | import org.elasticsearch.common.unit.TimeValue;
|
| 40 | +import org.elasticsearch.common.xcontent.XContentType; |
38 | 41 | import org.elasticsearch.env.NodeEnvironment;
|
39 | 42 | import org.elasticsearch.index.IndexNotFoundException;
|
40 | 43 | import org.elasticsearch.index.query.RangeQueryBuilder;
|
@@ -399,4 +402,69 @@ public Settings onNodeStopped(String nodeName) throws Exception {
|
399 | 402 | assertThat(e, hasToString(containsString("unknown setting [index.foo]")));
|
400 | 403 | }
|
401 | 404 |
|
| 405 | + /** |
| 406 | + * This test method is used to generate the Put Mapping Java Indices API documentation |
| 407 | + * at "docs/java-api/admin/indices/put-mapping.asciidoc" so the documentation gets tested |
| 408 | + * so that it compiles and runs without throwing errors at runtime. |
| 409 | + */ |
| 410 | + public void testPutMappingDocumentation() throws Exception { |
| 411 | + Client client = client(); |
| 412 | + // tag::addMapping-create-index-request |
| 413 | + client.admin().indices().prepareCreate("twitter") // <1> |
| 414 | + .addMapping("tweet", "{\n" + // <2> |
| 415 | + " \"tweet\": {\n" + |
| 416 | + " \"properties\": {\n" + |
| 417 | + " \"message\": {\n" + |
| 418 | + " \"type\": \"text\"\n" + |
| 419 | + " }\n" + |
| 420 | + " }\n" + |
| 421 | + " }\n" + |
| 422 | + " }", XContentType.JSON) |
| 423 | + .get(); |
| 424 | + // end::addMapping-create-index-request |
| 425 | + |
| 426 | + // we need to delete in order to create a fresh new index with another type |
| 427 | + client.admin().indices().prepareDelete("twitter").get(); |
| 428 | + client.admin().indices().prepareCreate("twitter").get(); |
| 429 | + |
| 430 | + // tag::putMapping-request-source |
| 431 | + client.admin().indices().preparePutMapping("twitter") // <1> |
| 432 | + .setType("user") // <2> |
| 433 | + .setSource("{\n" + // <3> |
| 434 | + " \"properties\": {\n" + |
| 435 | + " \"name\": {\n" + |
| 436 | + " \"type\": \"text\"\n" + |
| 437 | + " }\n" + |
| 438 | + " }\n" + |
| 439 | + "}", XContentType.JSON) |
| 440 | + .get(); |
| 441 | + |
| 442 | + // You can also provide the type in the source document |
| 443 | + client.admin().indices().preparePutMapping("twitter") |
| 444 | + .setType("user") |
| 445 | + .setSource("{\n" + |
| 446 | + " \"user\":{\n" + // <4> |
| 447 | + " \"properties\": {\n" + |
| 448 | + " \"name\": {\n" + |
| 449 | + " \"type\": \"text\"\n" + |
| 450 | + " }\n" + |
| 451 | + " }\n" + |
| 452 | + " }\n" + |
| 453 | + "}", XContentType.JSON) |
| 454 | + .get(); |
| 455 | + // end::putMapping-request-source |
| 456 | + |
| 457 | + // tag::putMapping-request-source-append |
| 458 | + client.admin().indices().preparePutMapping("twitter") // <1> |
| 459 | + .setType("user") // <2> |
| 460 | + .setSource("{\n" + // <3> |
| 461 | + " \"properties\": {\n" + |
| 462 | + " \"user_name\": {\n" + |
| 463 | + " \"type\": \"text\"\n" + |
| 464 | + " }\n" + |
| 465 | + " }\n" + |
| 466 | + "}", XContentType.JSON) |
| 467 | + .get(); |
| 468 | + // end::putMapping-request-source-append |
| 469 | + } |
402 | 470 | }
|
0 commit comments