retrieve-selected-fields.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. {wikipedia}/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-field-type, `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. WARNING: The `stored_fields` parameter is for fields that are explicitly marked as
  203. stored in the mapping, which is off by default and generally not recommended.
  204. Use <<source-filtering,source filtering>> instead to select
  205. subsets of the original source document to be returned.
  206. Allows to selectively load specific stored fields for each document represented
  207. by a search hit.
  208. [source,console]
  209. --------------------------------------------------
  210. GET /_search
  211. {
  212. "stored_fields" : ["user", "postDate"],
  213. "query" : {
  214. "term" : { "user" : "kimchy" }
  215. }
  216. }
  217. --------------------------------------------------
  218. `*` can be used to load all stored fields from the document.
  219. An empty array will cause only the `_id` and `_type` for each hit to be
  220. returned, for example:
  221. [source,console]
  222. --------------------------------------------------
  223. GET /_search
  224. {
  225. "stored_fields" : [],
  226. "query" : {
  227. "term" : { "user" : "kimchy" }
  228. }
  229. }
  230. --------------------------------------------------
  231. If the requested fields are not stored (`store` mapping set to `false`), they will be ignored.
  232. Stored field values fetched from the document itself are always returned as an array. On the contrary, metadata fields like `_routing` are never returned as an array.
  233. Also only leaf fields can be returned via the `stored_fields` option. If an object field is specified, it will be ignored.
  234. NOTE: On its own, `stored_fields` cannot be used to load fields in nested
  235. objects -- if a field contains a nested object in its path, then no data will
  236. be returned for that stored field. To access nested fields, `stored_fields`
  237. must be used within an <<inner-hits, `inner_hits`>> block.
  238. [discrete]
  239. [[disable-stored-fields]]
  240. ==== Disable stored fields
  241. To disable the stored fields (and metadata fields) entirely use: `_none_`:
  242. [source,console]
  243. --------------------------------------------------
  244. GET /_search
  245. {
  246. "stored_fields": "_none_",
  247. "query" : {
  248. "term" : { "user" : "kimchy" }
  249. }
  250. }
  251. --------------------------------------------------
  252. NOTE: <<source-filtering,`_source`>> and <<request-body-search-version, `version`>> parameters cannot be activated if `_none_` is used.
  253. [discrete]
  254. [[source-filtering]]
  255. === Source filtering
  256. You can use the `_source` parameter to select what fields of the source are
  257. returned. This is called _source filtering_.
  258. The following search API request sets the `_source` request body parameter to
  259. `false`. The document source is not included in the response.
  260. [source,console]
  261. ----
  262. GET /_search
  263. {
  264. "_source": false,
  265. "query": {
  266. "match": {
  267. "user.id": "kimchy"
  268. }
  269. }
  270. }
  271. ----
  272. To return only a subset of source fields, specify a wildcard (`*`) pattern in
  273. the `_source` parameter. The following search API request returns the source for
  274. only the `obj` field and its properties.
  275. [source,console]
  276. ----
  277. GET /_search
  278. {
  279. "_source": "obj.*",
  280. "query": {
  281. "match": {
  282. "user.id": "kimchy"
  283. }
  284. }
  285. }
  286. ----
  287. You can also specify an array of wildcard patterns in the `_source` field. The
  288. following search API request returns the source for only the `obj1` and
  289. `obj2` fields and their properties.
  290. [source,console]
  291. ----
  292. GET /_search
  293. {
  294. "_source": [ "obj1.*", "obj2.*" ],
  295. "query": {
  296. "match": {
  297. "user.id": "kimchy"
  298. }
  299. }
  300. }
  301. ----
  302. For finer control, you can specify an object containing arrays of `includes` and
  303. `excludes` patterns in the `_source` parameter.
  304. If the `includes` property is specified, only source fields that match one of
  305. its patterns are returned. You can exclude fields from this subset using the
  306. `excludes` property.
  307. If the `includes` property is not specified, the entire document source is
  308. returned, excluding any fields that match a pattern in the `excludes` property.
  309. The following search API request returns the source for only the `obj1` and
  310. `obj2` fields and their properties, excluding any child `description` fields.
  311. [source,console]
  312. ----
  313. GET /_search
  314. {
  315. "_source": {
  316. "includes": [ "obj1.*", "obj2.*" ],
  317. "excludes": [ "*.description" ]
  318. },
  319. "query": {
  320. "term": {
  321. "user.id": "kimchy"
  322. }
  323. }
  324. }
  325. ----
  326. [discrete]
  327. [[script-fields]]
  328. === Script fields
  329. You can use the `script_fields` parameter to retrieve a <<modules-scripting,script
  330. evaluation>> (based on different fields) for each hit. For example:
  331. [source,console]
  332. --------------------------------------------------
  333. GET /_search
  334. {
  335. "query": {
  336. "match_all": {}
  337. },
  338. "script_fields": {
  339. "test1": {
  340. "script": {
  341. "lang": "painless",
  342. "source": "doc['price'].value * 2"
  343. }
  344. },
  345. "test2": {
  346. "script": {
  347. "lang": "painless",
  348. "source": "doc['price'].value * params.factor",
  349. "params": {
  350. "factor": 2.0
  351. }
  352. }
  353. }
  354. }
  355. }
  356. --------------------------------------------------
  357. // TEST[setup:sales]
  358. Script fields can work on fields that are not stored (`price` in
  359. the above case), and allow to return custom values to be returned (the
  360. evaluated value of the script).
  361. Script fields can also access the actual `_source` document and
  362. extract specific elements to be returned from it by using `params['_source']`.
  363. Here is an example:
  364. [source,console]
  365. --------------------------------------------------
  366. GET /_search
  367. {
  368. "query" : {
  369. "match_all": {}
  370. },
  371. "script_fields" : {
  372. "test1" : {
  373. "script" : "params['_source']['message']"
  374. }
  375. }
  376. }
  377. --------------------------------------------------
  378. // TEST[setup:my_index]
  379. Note the `_source` keyword here to navigate the json-like model.
  380. It's important to understand the difference between
  381. `doc['my_field'].value` and `params['_source']['my_field']`. The first,
  382. using the doc keyword, will cause the terms for that field to be loaded to
  383. memory (cached), which will result in faster execution, but more memory
  384. consumption. Also, the `doc[...]` notation only allows for simple valued
  385. fields (you can't return a json object from it) and makes sense only for
  386. non-analyzed or single term based fields. However, using `doc` is
  387. still the recommended way to access values from the document, if at all
  388. possible, because `_source` must be loaded and parsed every time it's used.
  389. Using `_source` is very slow.