search-fields.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. [[search-fields]]
  2. == Retrieve selected fields from a search
  3. ++++
  4. <titleabbrev>Retrieve selected fields</titleabbrev>
  5. ++++
  6. By default, each hit in the search response includes the document
  7. <<mapping-source-field,`_source`>>, which is the entire JSON object that was
  8. provided when indexing the document. To retrieve specific fields in the search
  9. response, you can use the `fields` parameter:
  10. [source,console]
  11. ----
  12. POST my-index-000001/_search
  13. {
  14. "query": {
  15. "match": {
  16. "message": "foo"
  17. }
  18. },
  19. "fields": ["user.id", "@timestamp"],
  20. "_source": false
  21. }
  22. ----
  23. // TEST[setup:my_index]
  24. The `fields` parameter consults both a document's `_source` and the index
  25. mappings to load and return values. Because it makes use of the mappings,
  26. `fields` has some advantages over referencing the `_source` directly: it
  27. accepts <<multi-fields, multi-fields>> and <<alias, field aliases>>, and
  28. also formats field values like dates in a consistent way.
  29. A document's `_source` is stored as a single field in Lucene. So the whole
  30. `_source` object must be loaded and parsed even if only a small number of
  31. fields are requested. To avoid this limitation, you can try another option for
  32. loading fields:
  33. * Use the <<docvalue-fields, `docvalue_fields`>>
  34. parameter to get values for selected fields. This can be a good
  35. choice when returning a fairly small number of fields that support doc values,
  36. such as keywords and dates.
  37. * Use the <<request-body-search-stored-fields, `stored_fields`>> parameter to
  38. get the values for specific stored fields (fields that use the
  39. <<mapping-store,`store`>> mapping option).
  40. If needed, you can use the <<script-fields,`script_field`>> parameter to
  41. transform field values in the response using a script. However, scripts can’t
  42. make use of {es}'s index structures or related optimizations. This can sometimes
  43. result in slower search speeds.
  44. You can find more detailed information on each of these methods in the
  45. following sections:
  46. * <<search-fields-param>>
  47. * <<docvalue-fields>>
  48. * <<stored-fields>>
  49. * <<source-filtering>>
  50. * <<script-fields>>
  51. [discrete]
  52. [[search-fields-param]]
  53. === Fields
  54. The `fields` parameter allows for retrieving a list of document fields in
  55. the search response. It consults both the document `_source` and the index
  56. mappings to return each value in a standardized way that matches its mapping
  57. type. By default, date fields are formatted according to the
  58. <<mapping-date-format,date format>> parameter in their mappings.
  59. The following search request uses the `fields` parameter to retrieve values
  60. for the `user.id` field, all fields starting with `http.response.`, and the
  61. `@timestamp` field:
  62. [source,console]
  63. ----
  64. POST my-index-000001/_search
  65. {
  66. "query": {
  67. "match": {
  68. "user.id": "kimchy"
  69. }
  70. },
  71. "fields": [
  72. "user.id",
  73. "http.response.*", <1>
  74. {
  75. "field": "@timestamp",
  76. "format": "epoch_millis" <2>
  77. }
  78. ],
  79. "_source": false
  80. }
  81. ----
  82. // TEST[setup:my_index]
  83. <1> Both full field names and wildcard patterns are accepted.
  84. <2> Using object notation, you can pass a `format` parameter to apply a custom
  85. format for the field's values. The date fields
  86. <<date,`date`>> and <<date_nanos, `date_nanos`>> accept a
  87. <<mapping-date-format,date format>>. <<spatial_datatypes, Spatial fields>>
  88. accept either `geojson` for http://www.geojson.org[GeoJSON] (the default)
  89. or `wkt` for
  90. https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry[Well Known Text].
  91. Other field types do not support the `format` parameter.
  92. The values are returned as a flat list in the `fields` section in each hit:
  93. [source,console-result]
  94. ----
  95. {
  96. "took" : 2,
  97. "timed_out" : false,
  98. "_shards" : {
  99. "total" : 1,
  100. "successful" : 1,
  101. "skipped" : 0,
  102. "failed" : 0
  103. },
  104. "hits" : {
  105. "total" : {
  106. "value" : 1,
  107. "relation" : "eq"
  108. },
  109. "max_score" : 1.0,
  110. "hits" : [
  111. {
  112. "_index" : "my-index-000001",
  113. "_id" : "0",
  114. "_score" : 1.0,
  115. "fields" : {
  116. "user.id" : [
  117. "kimchy"
  118. ],
  119. "@timestamp" : [
  120. "4098435132000"
  121. ],
  122. "http.response.bytes": [
  123. 1070000
  124. ],
  125. "http.response.status_code": [
  126. 200
  127. ]
  128. }
  129. }
  130. ]
  131. }
  132. }
  133. ----
  134. // TESTRESPONSE[s/"took" : 2/"took": $body.took/]
  135. // TESTRESPONSE[s/"max_score" : 1.0/"max_score" : $body.hits.max_score/]
  136. // TESTRESPONSE[s/"_score" : 1.0/"_score" : $body.hits.hits.0._score/]
  137. Only leaf fields are returned -- `fields` does not allow for fetching entire
  138. objects.
  139. The `fields` parameter handles field types like <<alias, field aliases>> and
  140. <<constant-keyword, `constant_keyword`>> whose values aren't always present in
  141. the `_source`. Other mapping options are also respected, including
  142. <<ignore-above, `ignore_above`>>, <<ignore-malformed, `ignore_malformed`>> and
  143. <<null-value, `null_value`>>.
  144. NOTE: The `fields` response always returns an array of values for each field,
  145. even when there is a single value in the `_source`. This is because {es} has
  146. no dedicated array type, and any field could contain multiple values. The
  147. `fields` parameter also does not guarantee that array values are returned in
  148. a specific order. See the mapping documentation on <<array, arrays>> for more
  149. background.
  150. [discrete]
  151. [[docvalue-fields]]
  152. === Doc value fields
  153. You can use the <<docvalue-fields,`docvalue_fields`>> parameter to return
  154. <<doc-values,doc values>> for one or more fields in the search response.
  155. Doc values store the same values as the `_source` but in an on-disk,
  156. column-based structure that's optimized for sorting and aggregations. Since each
  157. field is stored separately, {es} only reads the field values that were requested
  158. and can avoid loading the whole document `_source`.
  159. Doc values are stored for supported fields by default. However, doc values are
  160. not supported for <<text,`text`>> or
  161. {plugins}/mapper-annotated-text-usage.html[`text_annotated`] fields.
  162. The following search request uses the `docvalue_fields` parameter to retrieve
  163. doc values for the `user.id` field, all fields starting with `http.response.`, and the
  164. `@timestamp` field:
  165. [source,console]
  166. ----
  167. GET my-index-000001/_search
  168. {
  169. "query": {
  170. "match": {
  171. "user.id": "kimchy"
  172. }
  173. },
  174. "docvalue_fields": [
  175. "user.id",
  176. "http.response.*", <1>
  177. {
  178. "field": "date",
  179. "format": "epoch_millis" <2>
  180. }
  181. ]
  182. }
  183. ----
  184. // TEST[setup:my_index]
  185. <1> Both full field names and wildcard patterns are accepted.
  186. <2> Using object notation, you can pass a `format` parameter to apply a custom
  187. format for the field's doc values. <<date,Date fields>> support a
  188. <<mapping-date-format,date `format`>>. <<number,Numeric fields>> support a
  189. https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html[DecimalFormat
  190. pattern]. Other field datatypes do not support the `format` parameter.
  191. TIP: You cannot use the `docvalue_fields` parameter to retrieve doc values for
  192. nested objects. If you specify a nested object, the search returns an empty
  193. array (`[ ]`) for the field. To access nested fields, use the
  194. <<inner-hits, `inner_hits`>> parameter's `docvalue_fields`
  195. property.
  196. [discrete]
  197. [[stored-fields]]
  198. === Stored fields
  199. It's also possible to store an individual field's values by using the
  200. <<mapping-store,`store`>> mapping option. You can use the
  201. `stored_fields` parameter to include these stored values in the search response.
  202. include::request/stored-fields.asciidoc[]
  203. [discrete]
  204. [[source-filtering]]
  205. === Source filtering
  206. You can use the `_source` parameter to select what fields of the source are
  207. returned. This is called _source filtering_.
  208. The following search API request sets the `_source` request body parameter to
  209. `false`. The document source is not included in the response.
  210. [source,console]
  211. ----
  212. GET /_search
  213. {
  214. "_source": false,
  215. "query": {
  216. "match": {
  217. "user.id": "kimchy"
  218. }
  219. }
  220. }
  221. ----
  222. To return only a subset of source fields, specify a wildcard (`*`) pattern in
  223. the `_source` parameter. The following search API request returns the source for
  224. only the `obj` field and its properties.
  225. [source,console]
  226. ----
  227. GET /_search
  228. {
  229. "_source": "obj.*",
  230. "query": {
  231. "match": {
  232. "user.id": "kimchy"
  233. }
  234. }
  235. }
  236. ----
  237. You can also specify an array of wildcard patterns in the `_source` field. The
  238. following search API request returns the source for only the `obj1` and
  239. `obj2` fields and their properties.
  240. [source,console]
  241. ----
  242. GET /_search
  243. {
  244. "_source": [ "obj1.*", "obj2.*" ],
  245. "query": {
  246. "match": {
  247. "user.id": "kimchy"
  248. }
  249. }
  250. }
  251. ----
  252. For finer control, you can specify an object containing arrays of `includes` and
  253. `excludes` patterns in the `_source` parameter.
  254. If the `includes` property is specified, only source fields that match one of
  255. its patterns are returned. You can exclude fields from this subset using the
  256. `excludes` property.
  257. If the `includes` property is not specified, the entire document source is
  258. returned, excluding any fields that match a pattern in the `excludes` property.
  259. The following search API request returns the source for only the `obj1` and
  260. `obj2` fields and their properties, excluding any child `description` fields.
  261. [source,console]
  262. ----
  263. GET /_search
  264. {
  265. "_source": {
  266. "includes": [ "obj1.*", "obj2.*" ],
  267. "excludes": [ "*.description" ]
  268. },
  269. "query": {
  270. "term": {
  271. "user.id": "kimchy"
  272. }
  273. }
  274. }
  275. ----
  276. include::request/script-fields.asciidoc[]