1
0

fields.asciidoc 6.9 KB

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