access-fields.asciidoc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. [[script-fields-api]]
  2. == Access fields in a document with the `field` API
  3. ++++
  4. <titleabbrev>Access fields in a document</titleabbrev>
  5. ++++
  6. beta::["The `field` API is still in development and should be considered a beta feature. The API is subject to change and this iteration is likely not the final state. For feature status, refer to {es-issue}78920[#78920]."]
  7. Use the `field` API to access document fields:
  8. [source,painless]
  9. ----
  10. field('my_field').get(<default_value>)
  11. ----
  12. This API fundamentally changes how you access documents in Painless. Previously,
  13. you had to access the `doc` map with the field name that you wanted to access:
  14. [source,painless]
  15. ----
  16. doc['my_field'].value
  17. ----
  18. Accessing document fields this way didn't handle missing values or missing
  19. mappings, which meant that to write robust Painless scripts, you needed to
  20. include logic to check that both fields and values exist.
  21. Instead, use the `field` API, which is the preferred approach to access
  22. documents in Painless. The `field` API handles missing values, and will evolve
  23. to abstract access to `_source` and `doc_values`.
  24. NOTE: Some fields aren't yet compatible with the `fields` API, such as `text` or
  25. `geo` fields. Continue using `doc` to access field types that the `field` API
  26. doesn't support.
  27. The `field` API returns a `Field` object that iterates over fields with
  28. multiple values, providing access to the underlying value through the
  29. `get(<default_value>)` method, as well as type conversion and helper methods.
  30. The `field` API returns the default value that you specify, regardless of
  31. whether the field exists or has any values for the current document.
  32. This means that the `field` API can handle missing values without requiring
  33. additional logic. For a reference type such as `keyword`, the default
  34. value can be `null`. For a primitive type such as `boolean` or `long`, the
  35. default value must be a matching primitive type, such as `false` or `1`.
  36. [discrete]
  37. === Convenient, simpler access
  38. Instead of explicitly calling the `field` API with the `get()` method, you can
  39. include the `$` shortcut. Just include the `$` symbol, field name, and a default
  40. value, in case the field doesn't have a value:
  41. [source,painless]
  42. ----
  43. $(‘field’, <default_value>)
  44. ----
  45. With these enhanced capabilities and simplified syntax, you can write scripts
  46. that are shorter, less complex, and easier to read. For example, the following
  47. script uses the outdated syntax to determine the difference in milliseconds
  48. between two complex `datetime` values from an indexed document:
  49. [source,painless]
  50. ----
  51. if (doc.containsKey('start') && doc.containsKey('end')) {
  52. if (doc['start'].size() > 0 && doc['end'].size() > 0) {
  53. ZonedDateTime start = doc['start'].value;
  54. ZonedDateTime end = doc['end'].value;
  55. return ChronoUnit.MILLIS.between(start, end);
  56. } else {
  57. return -1;
  58. }
  59. } else {
  60. return -1;
  61. }
  62. ----
  63. Using the `field` API, you can write this same script much more succinctly,
  64. without requiring additional logic to determine whether fields exist before
  65. operating on them:
  66. [source,painless]
  67. ----
  68. ZonedDateTime start = field('start').get(null);
  69. ZonedDateTime end = field('end').get(null);
  70. return start == null || end == null ? -1 : ChronoUnit.MILLIS.between(start, end)
  71. ----
  72. [discrete]
  73. === Supported mapped field types
  74. The following table indicates the mapped field types that the `field` API
  75. supports. For each supported type, values are listed that are returned by the
  76. `field` API (from the `get` and `as<Type>` methods) and the `doc` map (from the
  77. `getValue` and `get` methods).
  78. NOTE: The `fields` API currently doesn't support some fields, but you can still
  79. access those fields through the `doc` map. For the most current list of
  80. supported fields, refer to {es-issue}79105[#79105].
  81. [cols="1,1,1,1,1",options="header",]
  82. |========
  83. |Mapped field type
  84. 2+|Returned type from `field`
  85. 2+|Returned type from `doc`
  86. h| h|`get` h|`as<Type>` h|`getValue` h|`get`
  87. |`binary` |`ByteBuffer` |- |`BytesRef` |`BytesRef`
  88. |`boolean` |`boolean` |- |`boolean` |`Boolean`
  89. |`keyword` |`String` |- |`String` |`String`
  90. |`long` |`long` |- |`long` |`Long`
  91. |`integer` |`int` |- |`long` |`Long`
  92. |`short` |`short` |- |`long` |`Long`
  93. |`byte` |`byte` |- |`long` |`Long`
  94. |`double` |`double` |- |`double` |`Double`
  95. |`scaled_float` |`double` |- |`double` |`Double`
  96. |`half_float` |`float` |- |`double` |`Double`
  97. |`unsigned_long` |`long` |`BigInteger` |`long` |`Long`
  98. |`date` |`ZonedDateTime` |- |`ZonedDateTime` |`ZonedDateTime`
  99. |`date_nanos` |`ZonedDateTime` |- |`ZonedDateTime` |`ZonedDateTime`
  100. |`ip` |`IpAddress` |`String` |`String` |`String`
  101. |`_version` |`long` |- |`long` |`Long`
  102. |`_seq_no` |`long` |- |`long` |`Long`
  103. |`version` |`Version` |`String` |`String` |`String`
  104. |`murmur3` |`long` |- |`long` |`Long`
  105. |`constant_keyword` |`String` |- |`String` |`String`
  106. |`wildcard` |`String` |- |`String` |`String`
  107. |`flattened` |`String` |- |`String` |`String`
  108. |========