1
0

dynamic.asciidoc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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,console]
  7. --------------------------------------------------
  8. PUT my_index/_doc/1 <1>
  9. {
  10. "username": "johnsmith",
  11. "name": {
  12. "first": "John",
  13. "last": "Smith"
  14. }
  15. }
  16. GET my_index/_mapping <2>
  17. PUT my_index/_doc/2 <3>
  18. {
  19. "username": "marywhite",
  20. "email": "mary@white.com",
  21. "name": {
  22. "first": "Mary",
  23. "middle": "Alice",
  24. "last": "White"
  25. }
  26. }
  27. GET my_index/_mapping <4>
  28. --------------------------------------------------
  29. <1> This document introduces the string field `username`, the object field
  30. `name`, and two string fields under the `name` object which can be
  31. referred to as `name.first` and `name.last`.
  32. <2> Check the mapping to verify the above.
  33. <3> This document adds two string fields: `email` and `name.middle`.
  34. <4> Check the mapping to verify the changes.
  35. The details of how new fields are detected and added to the mapping is explained in <<dynamic-mapping>>.
  36. The `dynamic` setting controls whether new fields can be added dynamically or
  37. not. It accepts three settings:
  38. [horizontal]
  39. `true`:: Newly detected fields are added to the mapping. (default)
  40. `false`:: Newly detected fields are ignored. These fields will not be indexed so will not be searchable
  41. but will still appear in the `_source` field of returned hits. These fields will not be added
  42. to the mapping, new fields must be added explicitly.
  43. `strict`:: If new fields are detected, an exception is thrown and the document is rejected. New fields
  44. must be explicitly added to the mapping.
  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,console]
  49. --------------------------------------------------
  50. PUT my_index
  51. {
  52. "mappings": {
  53. "dynamic": false, <1>
  54. "properties": {
  55. "user": { <2>
  56. "properties": {
  57. "name": {
  58. "type": "text"
  59. },
  60. "social_networks": { <3>
  61. "dynamic": true,
  62. "properties": {}
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. --------------------------------------------------
  70. <1> Dynamic mapping is disabled at the type level, so no new top-level fields will be added dynamically.
  71. <2> The `user` object inherits the type-level setting.
  72. <3> The `user.social_networks` object enables dynamic mapping, so new fields may be added to this inner object.
  73. TIP: The `dynamic` setting can be updated on existing fields
  74. using the <<indices-put-mapping,PUT mapping API>>.