|
26 | 26 | import org.elasticsearch.action.search.SearchResponse;
|
27 | 27 | import org.elasticsearch.action.support.ActiveShardCount;
|
28 | 28 | import org.elasticsearch.action.support.IndicesOptions;
|
| 29 | +import org.elasticsearch.client.Client; |
29 | 30 | import org.elasticsearch.cluster.ClusterState;
|
30 | 31 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
31 | 32 | import org.elasticsearch.cluster.metadata.MetaData;
|
32 | 33 | import org.elasticsearch.common.collect.ImmutableOpenMap;
|
33 | 34 | import org.elasticsearch.common.settings.Settings;
|
34 | 35 | import org.elasticsearch.common.unit.TimeValue;
|
| 36 | +import org.elasticsearch.common.xcontent.XContentType; |
35 | 37 | import org.elasticsearch.index.IndexNotFoundException;
|
36 | 38 | import org.elasticsearch.index.query.RangeQueryBuilder;
|
37 | 39 | import org.elasticsearch.test.ESIntegTestCase;
|
@@ -343,4 +345,70 @@ public void testIndexNameInResponse() {
|
343 | 345 |
|
344 | 346 | assertEquals("Should have index name in response", "foo", response.index());
|
345 | 347 | }
|
| 348 | + |
| 349 | + /** |
| 350 | + * This test method is used to generate the Put Mapping Java Indices API documentation |
| 351 | + * at "docs/java-api/admin/indices/put-mapping.asciidoc" so the documentation gets tested |
| 352 | + * so that it compiles and runs without throwing errors at runtime. |
| 353 | + */ |
| 354 | + public void testPutMappingDocumentation() throws Exception { |
| 355 | + Client client = client(); |
| 356 | + // tag::addMapping-create-index-request |
| 357 | + client.admin().indices().prepareCreate("twitter") // <1> |
| 358 | + .addMapping("tweet", "{\n" + // <2> |
| 359 | + " \"tweet\": {\n" + |
| 360 | + " \"properties\": {\n" + |
| 361 | + " \"message\": {\n" + |
| 362 | + " \"type\": \"text\"\n" + |
| 363 | + " }\n" + |
| 364 | + " }\n" + |
| 365 | + " }\n" + |
| 366 | + " }", XContentType.JSON) |
| 367 | + .get(); |
| 368 | + // end::addMapping-create-index-request |
| 369 | + |
| 370 | + // we need to delete in order to create a fresh new index with another type |
| 371 | + client.admin().indices().prepareDelete("twitter").get(); |
| 372 | + client.admin().indices().prepareCreate("twitter").get(); |
| 373 | + |
| 374 | + // tag::putMapping-request-source |
| 375 | + client.admin().indices().preparePutMapping("twitter") // <1> |
| 376 | + .setType("user") // <2> |
| 377 | + .setSource("{\n" + // <3> |
| 378 | + " \"properties\": {\n" + |
| 379 | + " \"name\": {\n" + |
| 380 | + " \"type\": \"text\"\n" + |
| 381 | + " }\n" + |
| 382 | + " }\n" + |
| 383 | + "}", XContentType.JSON) |
| 384 | + .get(); |
| 385 | + |
| 386 | + // You can also provide the type in the source document |
| 387 | + client.admin().indices().preparePutMapping("twitter") |
| 388 | + .setType("user") |
| 389 | + .setSource("{\n" + |
| 390 | + " \"user\":{\n" + // <4> |
| 391 | + " \"properties\": {\n" + |
| 392 | + " \"name\": {\n" + |
| 393 | + " \"type\": \"text\"\n" + |
| 394 | + " }\n" + |
| 395 | + " }\n" + |
| 396 | + " }\n" + |
| 397 | + "}", XContentType.JSON) |
| 398 | + .get(); |
| 399 | + // end::putMapping-request-source |
| 400 | + |
| 401 | + // tag::putMapping-request-source-append |
| 402 | + client.admin().indices().preparePutMapping("twitter") // <1> |
| 403 | + .setType("user") // <2> |
| 404 | + .setSource("{\n" + // <3> |
| 405 | + " \"properties\": {\n" + |
| 406 | + " \"user_name\": {\n" + |
| 407 | + " \"type\": \"text\"\n" + |
| 408 | + " }\n" + |
| 409 | + " }\n" + |
| 410 | + "}", XContentType.JSON) |
| 411 | + .get(); |
| 412 | + // end::putMapping-request-source-append |
| 413 | + } |
346 | 414 | }
|
0 commit comments