dynamic.asciidoc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. [[dynamic]]
  2. === `dynamic`
  3. By default, fields can be added _dynamically_ to a document, or to
  4. <<object,inner objects>> within a document, just by indexing a document
  5. containing the new field. For instance:
  6. [source,js]
  7. --------------------------------------------------
  8. DELETE my_index <1>
  9. PUT my_index/my_type/1 <2>
  10. {
  11. "username": "johnsmith",
  12. "name": {
  13. "first": "John",
  14. "last": "Smith"
  15. }
  16. }
  17. GET my_index/_mapping <3>
  18. PUT my_index/my_type/2 <4>
  19. {
  20. "username": "marywhite",
  21. "email": "mary@white.com",
  22. "name": {
  23. "first": "Mary",
  24. "middle": "Alice",
  25. "last": "White"
  26. }
  27. }
  28. GET my_index/_mapping <5>
  29. --------------------------------------------------
  30. // AUTOSENSE
  31. <1> First delete the index, in case it already exists.
  32. <2> This document introduces the string field `username`, the object field
  33. `name`, and two string fields under the `name` object which can be
  34. referred to as `name.first` and `name.last`.
  35. <3> Check the mapping to verify the above.
  36. <4> This document adds two string fields: `email` and `name.middle`.
  37. <5> Check the mapping to verify the changes.
  38. The details of how new fields are detected and added to the mapping is explained in <<dynamic-mapping>>.
  39. The `dynamic` setting controls whether new fields can be added dynamically or
  40. not. It accepts three settings:
  41. [horizontal]
  42. `true`:: Newly detected fields are added to the mapping. (default)
  43. `false`:: Newly detected fields are ignored. New fields must be added explicitly.
  44. `strict`:: If new fields are detected, an exception is thrown and the document is rejected.
  45. The `dynamic` setting may be set at the mapping type level, and on each
  46. <<object,inner object>>. Inner objects inherit the setting from their parent
  47. object or from the mapping type. For instance:
  48. [source,js]
  49. --------------------------------------------------
  50. PUT my_index
  51. {
  52. "mappings": {
  53. "my_type": {
  54. "dynamic": false, <1>
  55. "properties": {
  56. "user": { <2>
  57. "properties": {
  58. "name": {
  59. "type": "string"
  60. },
  61. "social_networks": { <3>
  62. "dynamic": true,
  63. "properties": {}
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. --------------------------------------------------
  72. // AUTOSENSE
  73. <1> Dynamic mapping is disabled at the type level, so no new top-level fields will be added dynamically.
  74. <2> The `user` object inherits the type-level setting.
  75. <3> The `user.social_networks` object enables dynamic mapping, so new fields may be added to this inner object.
  76. TIP: The `dynamic` setting is allowed to have different settings for fields of
  77. the same name in the same index. Its value can be updated on existing fields
  78. using the <<indices-put-mapping,PUT mapping API>>.