put-mapping.asciidoc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. [[java-admin-indices-put-mapping]]
  2. ==== Put Mapping
  3. The PUT mapping API allows you to add a new type while creating an index:
  4. ["source","java",subs="attributes,callouts,macros"]
  5. --------------------------------------------------
  6. include-tagged::{client-tests}/IndicesDocumentationIT.java[index-with-mapping]
  7. --------------------------------------------------
  8. <1> <<java-admin-indices-create-index,Creates an index>> called `twitter`
  9. <2> It also adds a `tweet` mapping type.
  10. The PUT mapping API also allows to add a new type to an existing index:
  11. [source,java]
  12. --------------------------------------------------
  13. client.admin().indices().preparePutMapping("twitter") <1>
  14. .setType("user") <2>
  15. .setSource("{\n" + <3>
  16. " \"properties\": {\n" +
  17. " \"name\": {\n" +
  18. " \"type\": \"text\"\n" +
  19. " }\n" +
  20. " }\n" +
  21. "}")
  22. .get();
  23. // You can also provide the type in the source document
  24. client.admin().indices().preparePutMapping("twitter")
  25. .setType("user")
  26. .setSource("{\n" +
  27. " \"user\":{\n" + <4>
  28. " \"properties\": {\n" +
  29. " \"name\": {\n" +
  30. " \"type\": \"text\"\n" +
  31. " }\n" +
  32. " }\n" +
  33. " }\n" +
  34. "}")
  35. .get();
  36. --------------------------------------------------
  37. <1> Puts a mapping on existing index called `twitter`
  38. <2> Adds a `user` mapping type.
  39. <3> This `user` has a predefined type
  40. <4> type can be also provided within the source
  41. You can use the same API to update an existing mapping:
  42. [source,java]
  43. --------------------------------------------------
  44. client.admin().indices().preparePutMapping("twitter") <1>
  45. .setType("user") <2>
  46. .setSource("{\n" + <3>
  47. " \"properties\": {\n" +
  48. " \"user_name\": {\n" +
  49. " \"type\": \"text\"\n" +
  50. " }\n" +
  51. " }\n" +
  52. "}")
  53. .get();
  54. --------------------------------------------------
  55. <1> Puts a mapping on existing index called `twitter`
  56. <2> Updates the `user` mapping type.
  57. <3> This `user` has now a new field `user_name`