2015-12-30 11:50:46 -05:00
|
|
|
[[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" +
|
2017-12-04 05:51:00 -05:00
|
|
|
" \"type\": \"text\"\n" +
|
2015-12-30 11:50:46 -05:00
|
|
|
" }\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" +
|
2017-12-04 05:51:00 -05:00
|
|
|
" \"type\": \"text\"\n" +
|
2015-12-30 11:50:46 -05:00
|
|
|
" }\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" +
|
2017-12-04 05:51:00 -05:00
|
|
|
" \"type\": \"text\"\n" +
|
2015-12-30 11:50:46 -05:00
|
|
|
" }\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>
|
2017-03-20 15:04:31 -04:00
|
|
|
.setType("user") <2>
|
2015-12-30 11:50:46 -05:00
|
|
|
.setSource("{\n" + <3>
|
|
|
|
" \"properties\": {\n" +
|
|
|
|
" \"user_name\": {\n" +
|
2017-12-04 05:51:00 -05:00
|
|
|
" \"type\": \"text\"\n" +
|
2015-12-30 11:50:46 -05:00
|
|
|
" }\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`
|
|
|
|
|