fields.asciidoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <<search-request-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. Scripts may also have access to the document's relevance
  25. <<scripting-score,`_score`>> and, via the experimental `_index` variable,
  26. to term statistics for <<modules-advanced-scripting,advanced text scoring>>.
  27. [[scripting-score]]
  28. [float]
  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 <<search-request-sort,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,js]
  38. -------------------------------------
  39. PUT my_index/my_type/1
  40. {
  41. "text": "quick brown fox",
  42. "popularity": 1
  43. }
  44. PUT my_index/my_type/2
  45. {
  46. "text": "quick fox",
  47. "popularity": 5
  48. }
  49. GET my_index/_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. "inline": "_score * doc['popularity']"
  62. }
  63. }
  64. }
  65. }
  66. }
  67. -------------------------------------
  68. // AUTOSENSE
  69. [float]
  70. [[modules-scripting-doc-vals]]
  71. === Doc Values
  72. By far the fastest most efficient way to access a field value from a
  73. script is to use the `doc['field_name']` syntax, which retrieves the field
  74. value from <<doc-values,doc values>>. Doc values are a columnar field value
  75. store, enabled by default on all fields except for <<text,analyzed `text` fields>>.
  76. [source,js]
  77. -------------------------------
  78. PUT my_index/my_type/1
  79. {
  80. "cost_price": 100
  81. }
  82. GET my_index/_search
  83. {
  84. "script_fields": {
  85. "sales_price": {
  86. "script": {
  87. "lang": "expression",
  88. "inline": "doc['cost_price'] * markup",
  89. "params": {
  90. "markup": 0.2
  91. }
  92. }
  93. }
  94. }
  95. }
  96. -------------------------------
  97. // AUTOSENSE
  98. Doc-values can only return "simple" field values like numbers, dates, geo-
  99. points, terms, etc, or arrays of these values if the field is multi-valued.
  100. It cannot return JSON objects.
  101. [NOTE]
  102. .Doc values and `text` fields
  103. ===================================================
  104. The `doc['field']` syntax can also be used for <<text,analyzed `text` fields>>
  105. if <<fielddata,`fielddata`>> is enabled, but *BEWARE*: enabling fielddata on a
  106. `text` field requires loading all of the terms into the JVM heap, which can be
  107. very expensive both in terms of memory and CPU. It seldom makes sense to
  108. access `text` fields from scripts.
  109. ===================================================
  110. [float]
  111. [[modules-scripting-stored]]
  112. === Stored Fields and `_source`
  113. _Stored fields_ -- fields explicitly marked as
  114. <<mapping-store,`"store": true`>> -- can be accessed using the
  115. `_fields['field_name'].value` or `_fields['field_name'].values` syntax.
  116. The document <<mapping-source-field,`_source`>>, which is really just a
  117. special stored field, can be accessed using the `_source.field_name` syntax.
  118. The `_source` is loaded as a map-of-maps, so properties within object fields
  119. can be accessed as, for example, `_source.name.first`.
  120. [IMPORTANT]
  121. .Prefer doc-values to stored fields
  122. =========================================================
  123. Stored fields (which includes the stored `_source` field) are much slower than
  124. doc-values. They are optimised for returning several fields per result,
  125. while doc values are optimised for accessing the value of a specific field in
  126. many documents.
  127. It makes sense to use `_source` or stored fields when generating a
  128. <<search-request-script-fields,script field>> for the top ten hits from a search
  129. result but, for other search and aggregation use cases, always prefer using
  130. doc values.
  131. =========================================================
  132. For instance:
  133. [source,js]
  134. -------------------------------
  135. PUT my_index
  136. {
  137. "mappings": {
  138. "my_type": {
  139. "properties": {
  140. "title": { <1>
  141. "type": "text"
  142. },
  143. "first_name": {
  144. "type": "text",
  145. "store": true
  146. },
  147. "last_name": {
  148. "type": "text",
  149. "store": true
  150. }
  151. }
  152. }
  153. }
  154. }
  155. PUT my_index/my_type/1
  156. {
  157. "title": "Mr",
  158. "first_name": "Barry",
  159. "last_name": "White"
  160. }
  161. GET my_index/_search
  162. {
  163. "script_fields": {
  164. "source": {
  165. "script": {
  166. "lang": "groovy",
  167. "inline": "_source.title + ' ' + _source.first_name + ' ' + _source.last_name" <2>
  168. }
  169. },
  170. "stored_fields": {
  171. "script": {
  172. "lang": "groovy",
  173. "inline": "_fields['first_name'].value + ' ' + _fields['last_name'].value"
  174. }
  175. }
  176. }
  177. }
  178. -------------------------------
  179. // AUTOSENSE
  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. =======================================================