fields.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. [[modules-scripting-fields]]
  2. == Accessing document fields and special variables
  3. Depending on where a script is used, it will have access to certain special
  4. variables and document fields.
  5. [discrete]
  6. == Update scripts
  7. A script used in the <<docs-update,update>>,
  8. <<docs-update-by-query,update-by-query>>, or <<docs-reindex,reindex>>
  9. API will have access to the `ctx` variable which exposes:
  10. [horizontal]
  11. `ctx._source`:: Access to the document <<mapping-source-field,`_source` field>>.
  12. `ctx.op`:: The operation that should be applied to the document: `index` or `delete`.
  13. `ctx._index` etc:: Access to <<mapping-fields,document metadata fields>>, some of which may be read-only.
  14. These scripts do not have access to the `doc` variable and have to use `ctx` to access the documents they operate on.
  15. [discrete]
  16. == Search and aggregation scripts
  17. With the exception of <<script-fields,script fields>> which are
  18. executed once per search hit, scripts used in search and aggregations will be
  19. executed once for every document which might match a query or an aggregation.
  20. Depending on how many documents you have, this could mean millions or billions
  21. of executions: these scripts need to be fast!
  22. Field values can be accessed from a script using
  23. <<modules-scripting-doc-vals,doc-values>>,
  24. <<modules-scripting-source, the `_source` field>>, or
  25. <<modules-scripting-stored, stored fields>>,
  26. each of which is explained below.
  27. [[scripting-score]]
  28. [discrete]
  29. === Accessing the score of a document within a script
  30. Scripts used in the <<query-dsl-function-score-query,`function_score` query>>,
  31. in <<sort-search-results,script-based sorting>>, or in
  32. <<search-aggregations,aggregations>> have access to the `_score` variable which
  33. represents the current relevance score of a document.
  34. Here's an example of using a script in a
  35. <<query-dsl-function-score-query,`function_score` query>> to alter the
  36. relevance `_score` of each document:
  37. [source,console]
  38. -------------------------------------
  39. PUT my-index-000001/_doc/1?refresh
  40. {
  41. "text": "quick brown fox",
  42. "popularity": 1
  43. }
  44. PUT my-index-000001/_doc/2?refresh
  45. {
  46. "text": "quick fox",
  47. "popularity": 5
  48. }
  49. GET my-index-000001/_search
  50. {
  51. "query": {
  52. "function_score": {
  53. "query": {
  54. "match": {
  55. "text": "quick brown fox"
  56. }
  57. },
  58. "script_score": {
  59. "script": {
  60. "lang": "expression",
  61. "source": "_score * doc['popularity']"
  62. }
  63. }
  64. }
  65. }
  66. }
  67. -------------------------------------
  68. [discrete]
  69. [[modules-scripting-doc-vals]]
  70. === Doc values
  71. By far the fastest most efficient way to access a field value from a
  72. script is to use the `doc['field_name']` syntax, which retrieves the field
  73. value from <<doc-values,doc values>>. Doc values are a columnar field value
  74. store, enabled by default on all fields except for <<text,analyzed `text` fields>>.
  75. [source,console]
  76. -------------------------------
  77. PUT my-index-000001/_doc/1?refresh
  78. {
  79. "cost_price": 100
  80. }
  81. GET my-index-000001/_search
  82. {
  83. "script_fields": {
  84. "sales_price": {
  85. "script": {
  86. "lang": "expression",
  87. "source": "doc['cost_price'] * markup",
  88. "params": {
  89. "markup": 0.2
  90. }
  91. }
  92. }
  93. }
  94. }
  95. -------------------------------
  96. Doc-values can only return "simple" field values like numbers, dates, geo-
  97. points, terms, etc, or arrays of these values if the field is multi-valued.
  98. It cannot return JSON objects.
  99. [NOTE]
  100. .Missing fields
  101. ===================================================
  102. The `doc['field']` will throw an error if `field` is missing from the mappings.
  103. In `painless`, a check can first be done with `doc.containsKey('field')` to guard
  104. accessing the `doc` map. Unfortunately, there is no way to check for the
  105. existence of the field in mappings in an `expression` script.
  106. ===================================================
  107. [NOTE]
  108. .Doc values and `text` fields
  109. ===================================================
  110. The `doc['field']` syntax can also be used for <<text,analyzed `text` fields>>
  111. if <<fielddata-mapping-param,`fielddata`>> is enabled, but *BEWARE*: enabling fielddata on a
  112. `text` field requires loading all of the terms into the JVM heap, which can be
  113. very expensive both in terms of memory and CPU. It seldom makes sense to
  114. access `text` fields from scripts.
  115. ===================================================
  116. [discrete]
  117. [[modules-scripting-source]]
  118. === The document `_source`
  119. The document <<mapping-source-field,`_source`>> can be accessed using the
  120. `_source.field_name` syntax. The `_source` is loaded as a map-of-maps, so
  121. properties within object fields can be accessed as, for example,
  122. `_source.name.first`.
  123. [IMPORTANT]
  124. .Prefer doc-values to _source
  125. =========================================================
  126. Accessing the `_source` field is much slower than using doc-values. The
  127. _source field is optimised for returning several fields per result, while doc
  128. values are optimised for accessing the value of a specific field in many
  129. documents.
  130. It makes sense to use `_source` when generating a
  131. <<script-fields,script field>> for the top ten hits from a
  132. search result but, for other search and aggregation use cases, always prefer
  133. using doc values.
  134. =========================================================
  135. For instance:
  136. [source,console]
  137. -------------------------------
  138. PUT my-index-000001
  139. {
  140. "mappings": {
  141. "properties": {
  142. "first_name": {
  143. "type": "text"
  144. },
  145. "last_name": {
  146. "type": "text"
  147. }
  148. }
  149. }
  150. }
  151. PUT my-index-000001/_doc/1?refresh
  152. {
  153. "first_name": "Barry",
  154. "last_name": "White"
  155. }
  156. GET my-index-000001/_search
  157. {
  158. "script_fields": {
  159. "full_name": {
  160. "script": {
  161. "lang": "painless",
  162. "source": "params._source.first_name + ' ' + params._source.last_name"
  163. }
  164. }
  165. }
  166. }
  167. -------------------------------
  168. [discrete]
  169. [[modules-scripting-stored]]
  170. === Stored fields
  171. _Stored fields_ -- fields explicitly marked as
  172. <<mapping-store,`"store": true`>> in the mapping -- can be accessed using the
  173. `_fields['field_name'].value` or `_fields['field_name']` syntax:
  174. [source,console]
  175. -------------------------------
  176. PUT my-index-000001
  177. {
  178. "mappings": {
  179. "properties": {
  180. "full_name": {
  181. "type": "text",
  182. "store": true
  183. },
  184. "title": {
  185. "type": "text",
  186. "store": true
  187. }
  188. }
  189. }
  190. }
  191. PUT my-index-000001/_doc/1?refresh
  192. {
  193. "full_name": "Alice Ball",
  194. "title": "Professor"
  195. }
  196. GET my-index-000001/_search
  197. {
  198. "script_fields": {
  199. "name_with_title": {
  200. "script": {
  201. "lang": "painless",
  202. "source": "params._fields['title'].value + ' ' + params._fields['full_name'].value"
  203. }
  204. }
  205. }
  206. }
  207. -------------------------------
  208. [TIP]
  209. .Stored vs `_source`
  210. =======================================================
  211. The `_source` field is just a special stored field, so the performance is
  212. similar to that of other stored fields. The `_source` provides access to the
  213. original document body that was indexed (including the ability to distinguish
  214. `null` values from empty fields, single-value arrays from plain scalars, etc).
  215. The only time it really makes sense to use stored fields instead of the
  216. `_source` field is when the `_source` is very large and it is less costly to
  217. access a few small stored fields instead of the entire `_source`.
  218. =======================================================