|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client.documentation; |
| 21 | + |
| 22 | +import org.elasticsearch.client.Client; |
| 23 | +import org.elasticsearch.common.xcontent.XContentType; |
| 24 | +import org.elasticsearch.test.ESIntegTestCase; |
| 25 | + |
| 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 | + */ |
| 40 | +public class IndicesDocumentationIT extends ESIntegTestCase { |
| 41 | + |
| 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 { |
| 48 | + Client client = client(); |
| 49 | + |
| 50 | + // 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 | + "}") |
| 59 | + .get(); |
| 60 | + // 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 |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments