subobjects.asciidoc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. [[subobjects]]
  2. === `subobjects`
  3. When indexing a document or updating mappings, Elasticsearch accepts fields that contain dots in their names,
  4. which get expanded to their corresponding object structure. For instance, the field `metrics.time.max`
  5. is mapped as a `max` leaf field with a parent `time` object, belonging to its parent `metrics` object.
  6. The described default behaviour is reasonable for most scenarios, but causes problems in certain situations
  7. where for instance a field `metrics.time` holds a value too, which is common when indexing metrics data.
  8. A document holding a value for both `metrics.time.max` and `metrics.time` gets rejected given that `time`
  9. would need to be a leaf field to hold a value as well as an object to hold the `max` sub-field.
  10. The `subobjects` setting, which can be applied only to the top-level mapping definition and
  11. to <<object,`object`>> fields, disables the ability for an object to hold further subobjects and makes it possible
  12. to store documents where field names contain dots and share common prefixes. From the example above, if the object
  13. container `metrics` has `subobjects` set to `false`, it can hold values for both `time` and `time.max` directly
  14. without the need for any intermediate object, as dots in field names are preserved.
  15. [source,console]
  16. --------------------------------------------------
  17. PUT my-index-000001
  18. {
  19. "mappings": {
  20. "properties": {
  21. "metrics": {
  22. "type": "object",
  23. "subobjects": false <1>
  24. }
  25. }
  26. }
  27. }
  28. PUT my-index-000001/_doc/metric_1
  29. {
  30. "metrics.time" : 100, <2>
  31. "metrics.time.min" : 10,
  32. "metrics.time.max" : 900
  33. }
  34. PUT my-index-000001/_doc/metric_2
  35. {
  36. "metrics" : {
  37. "time" : 100, <3>
  38. "time.min" : 10,
  39. "time.max" : 900
  40. }
  41. }
  42. GET my-index-000001/_mapping
  43. --------------------------------------------------
  44. [source,console-result]
  45. --------------------------------------------------
  46. {
  47. "my-index-000001" : {
  48. "mappings" : {
  49. "properties" : {
  50. "metrics" : {
  51. "subobjects" : false,
  52. "properties" : {
  53. "time" : {
  54. "type" : "long"
  55. },
  56. "time.min" : { <4>
  57. "type" : "long"
  58. },
  59. "time.max" : {
  60. "type" : "long"
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. --------------------------------------------------
  69. <1> The `metrics` field cannot hold other objects.
  70. <2> Sample document holding flat paths
  71. <3> Sample document holding an object (configured to not hold subobjects) and its leaf sub-fields
  72. <4> The resulting mapping where dots in field names were preserved
  73. The entire mapping may be configured to not support subobjects as well, in which case the document can
  74. only ever hold leaf sub-fields:
  75. [source,console]
  76. --------------------------------------------------
  77. PUT my-index-000001
  78. {
  79. "mappings": {
  80. "subobjects": false <1>
  81. }
  82. }
  83. PUT my-index-000001/_doc/metric_1
  84. {
  85. "time" : "100ms", <2>
  86. "time.min" : "10ms",
  87. "time.max" : "900ms"
  88. }
  89. --------------------------------------------------
  90. <1> The entire mapping is configured to not support objects.
  91. <2> The document does not support objects
  92. The `subobjects` setting for existing fields and the top-level mapping definition cannot be updated.