put-mapping.asciidoc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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]
  5. --------------------------------------------------
  6. client.admin().indices().prepareCreate("twitter") <1>
  7. .addMapping("tweet", "{\n" + <2>
  8. " \"tweet\": {\n" +
  9. " \"properties\": {\n" +
  10. " \"message\": {\n" +
  11. " \"type\": \"string\"\n" +
  12. " }\n" +
  13. " }\n" +
  14. " }\n" +
  15. " }")
  16. .get();
  17. --------------------------------------------------
  18. <1> <<java-admin-indices-create-index,Creates an index>> called `twitter`
  19. <2> It also adds a `tweet` mapping type.
  20. The PUT mapping API also allows to add a new type to an existing index:
  21. [source,java]
  22. --------------------------------------------------
  23. client.admin().indices().preparePutMapping("twitter") <1>
  24. .setType("user") <2>
  25. .setSource("{\n" + <3>
  26. " \"properties\": {\n" +
  27. " \"name\": {\n" +
  28. " \"type\": \"string\"\n" +
  29. " }\n" +
  30. " }\n" +
  31. "}")
  32. .get();
  33. // You can also provide the type in the source document
  34. client.admin().indices().preparePutMapping("twitter")
  35. .setType("user")
  36. .setSource("{\n" +
  37. " \"user\":{\n" + <4>
  38. " \"properties\": {\n" +
  39. " \"name\": {\n" +
  40. " \"type\": \"string\"\n" +
  41. " }\n" +
  42. " }\n" +
  43. " }\n" +
  44. "}")
  45. .get();
  46. --------------------------------------------------
  47. <1> Puts a mapping on existing index called `twitter`
  48. <2> Adds a `user` mapping type.
  49. <3> This `user` has a predefined type
  50. <4> type can be also provided within the source
  51. You can use the same API to update an existing mapping:
  52. [source,java]
  53. --------------------------------------------------
  54. client.admin().indices().preparePutMapping("twitter") <1>
  55. .setType("user") <2>
  56. .setSource("{\n" + <3>
  57. " \"properties\": {\n" +
  58. " \"user_name\": {\n" +
  59. " \"type\": \"string\"\n" +
  60. " }\n" +
  61. " }\n" +
  62. "}")
  63. .get();
  64. --------------------------------------------------
  65. <1> Puts a mapping on existing index called `twitter`
  66. <2> Updates the `user` mapping type.
  67. <3> This `user` has now a new field `user_name`