OpenSearch/docs/java-api/admin/indices/put-mapping.asciidoc
Christoph Büscher 36165265ce
Fix put mappings java API documentation (#31955)
The current docs of the put-mapping Java API is currently broken. It its current
form, it creates an index and uses the whole mapping definition given as a JSON
string as the type name. Since we didn't check the index created in the
IndicesDocumentationIT so far this went unnoticed.

This change adds test to catch this error to the documentation test, changes the
documentation so it works correctly now and adds an input validation to
PutMappingRequest#buildFromSimplifiedDef() which was used internally to reject
calls where no mapping definition is given.

Closes #31906
2018-07-17 09:09:03 +02:00

42 lines
1.7 KiB
Plaintext

[[java-admin-indices-put-mapping]]
==== Put Mapping
You can add mappings for a new type at index creation time:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{client-tests}/IndicesDocumentationIT.java[index-with-mapping]
--------------------------------------------------
<1> <<java-admin-indices-create-index,Creates an index>> called `twitter`
<2> Add a `tweet` type with a field called `message` that has the datatype `text`.
There are several variants of the above `addMapping` method, some taking an
`XContentBuilder` or a `Map` with the mapping definition as arguments. Make sure
to check the javadocs to pick the simplest one for your use case.
The PUT mapping API also allows to specify the mapping of a type after index
creation. In this case you can provide the mapping as a String similar to the
Rest API syntax:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{client-tests}/IndicesDocumentationIT.java[putMapping-request-source]
--------------------------------------------------
<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",subs="attributes,callouts,macros"]
--------------------------------------------------
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!: