Browse Source

Add documentation for Java API create index and put mapping

Starting documentation about the admin client.

* Create index with settings
* put and update mapping. Closes #10816
David Pilato 9 years ago
parent
commit
c4a84b730a

+ 10 - 0
docs/java-api/admin/cluster/index.asciidoc

@@ -0,0 +1,10 @@
+[[java-admin-cluster]]
+=== Cluster Administration
+
+To access cluster Java API, you need to call `cluster()` method from an <<java-admin,`AdminClient`>>:
+
+[source,java]
+--------------------------------------------------
+ClusterAdminClient clusterAdminClient = adminClient.cluster();
+--------------------------------------------------
+

+ 18 - 0
docs/java-api/admin/index.asciidoc

@@ -0,0 +1,18 @@
+[[java-admin]]
+== Java API Administration
+
+Elasticsearch provides a full Java API to deal with administration tasks.
+
+To access them, you need to call `admin()` method from a client to get an `AdminClient`:
+
+[source,java]
+--------------------------------------------------
+AdminClient adminClient = client.admin();
+--------------------------------------------------
+
+[NOTE]
+In the rest of this guide, we will use `client.admin()`.
+
+include::indices/index.asciidoc[]
+
+include::cluster/index.asciidoc[]

+ 28 - 0
docs/java-api/admin/indices/create-index.asciidoc

@@ -0,0 +1,28 @@
+[[java-admin-indices-create-index]]
+==== Create Index
+
+Using an <<java-admin-indices,`IndicesAdminClient`>>, you can create an index with all default settings and no mapping:
+
+[source,java]
+--------------------------------------------------
+client.admin().indices().prepareCreate("twitter").get();
+--------------------------------------------------
+
+[float]
+[[java-admin-indices-create-index-settings]]
+===== Index Settings
+
+Each index created can have specific settings associated with it.
+
+[source,java]
+--------------------------------------------------
+client.admin().indices().prepareCreate("twitter")
+        .setSettings(Settings.builder()             <1>
+                .put("index.number_of_shards", 3)
+                .put("index.number_of_replicas", 2)
+        )
+        .get();                                     <2>
+--------------------------------------------------
+<1> Settings for this index
+<2> Execute the action and wait for the result
+

+ 16 - 0
docs/java-api/admin/indices/index.asciidoc

@@ -0,0 +1,16 @@
+[[java-admin-indices]]
+=== Indices Administration
+
+To access indices Java API, you need to call `indices()` method from an <<java-admin,`AdminClient`>>:
+
+[source,java]
+--------------------------------------------------
+IndicesAdminClient indicesAdminClient = client.admin().indices();
+--------------------------------------------------
+
+[NOTE]
+In the rest of this guide, we will use `client.admin().indices()`.
+
+include::create-index.asciidoc[]
+
+include::put-mapping.asciidoc[]

+ 76 - 0
docs/java-api/admin/indices/put-mapping.asciidoc

@@ -0,0 +1,76 @@
+[[java-admin-indices-put-mapping]]
+==== Put Mapping
+
+The PUT mapping API allows you to add a new type while creating an index:
+
+[source,java]
+--------------------------------------------------
+client.admin().indices().prepareCreate("twitter")   <1>
+        .addMapping("tweet", "{\n" +                <2>
+                "    \"tweet\": {\n" +
+                "      \"properties\": {\n" +
+                "        \"message\": {\n" +
+                "          \"type\": \"string\"\n" +
+                "        }\n" +
+                "      }\n" +
+                "    }\n" +
+                "  }")
+        .get();
+--------------------------------------------------
+<1> <<java-admin-indices-create-index,Creates an index>> called `twitter`
+<2> It also adds a `tweet` mapping type.
+
+
+The PUT mapping API also allows to add a new type to an existing index:
+
+[source,java]
+--------------------------------------------------
+client.admin().indices().preparePutMapping("twitter")   <1>
+        .setType("user")                                <2>
+        .setSource("{\n" +                              <3>
+                "  \"properties\": {\n" +
+                "    \"name\": {\n" +
+                "      \"type\": \"string\"\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\": \"string\"\n" +
+                "            }\n" +
+                "        }\n" +
+                "    }\n" +
+                "}")
+        .get();
+--------------------------------------------------
+<1> Puts a mapping on existing index called `twitter`
+<2> Adds a `user` mapping type.
+<3> This `user` has a predefined type
+<4> type can be also provided within the source
+
+You can use the same API to update an existing mapping:
+
+[source,java]
+--------------------------------------------------
+client.admin().indices().preparePutMapping("twitter")   <1>
+        .setType("tweet")                               <2>
+        .setSource("{\n" +                              <3>
+                "  \"properties\": {\n" +
+                "    \"user_name\": {\n" +
+                "      \"type\": \"string\"\n" +
+                "    }\n" +
+                "  }\n" +
+                "}")
+        .get();
+--------------------------------------------------
+<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`
+

+ 2 - 0
docs/java-api/index.asciidoc

@@ -147,3 +147,5 @@ include::percolate.asciidoc[]
 include::query-dsl.asciidoc[]
 
 include::indexed-scripts.asciidoc[]
+
+include::admin/index.asciidoc[]