search-fields.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. [discrete]
  2. [[search-fields]]
  3. === Retrieve selected fields
  4. By default, each hit in the search response includes the document
  5. <<mapping-source-field,`_source`>>, which is the entire JSON object that was
  6. provided when indexing the document. If you only need certain source fields in
  7. the search response, you can use the <<source-filtering,source filtering>> to
  8. restrict what parts of the source are returned.
  9. Returning fields using only the document source has some limitations:
  10. * The `_source` field does not include <<multi-fields, multi-fields>> or
  11. <<alias, field aliases>>. Likewise, a field in the source does not contain
  12. values copied using the <<copy-to,`copy_to`>> mapping parameter.
  13. * Since the `_source` is stored as a single field in Lucene, the whole source
  14. object must be loaded and parsed, even if only a small number of fields are
  15. needed.
  16. To avoid these limitations, you can:
  17. * Use the <<docvalue-fields, `docvalue_fields`>>
  18. parameter to get values for selected fields. This can be a good
  19. choice when returning a fairly small number of fields that support doc values,
  20. such as keywords and dates.
  21. * Use the <<request-body-search-stored-fields, `stored_fields`>> parameter to get the values for specific stored fields. (Fields that use the <<mapping-store,`store`>> mapping option.)
  22. You can find more detailed information on each of these methods in the
  23. following sections:
  24. * <<source-filtering>>
  25. * <<docvalue-fields>>
  26. * <<stored-fields>>
  27. [discrete]
  28. [[source-filtering]]
  29. === Source filtering
  30. You can use the `_source` parameter to select what fields of the source are
  31. returned. This is called _source filtering_.
  32. .*Example*
  33. [%collapsible]
  34. ====
  35. The following search API request sets the `_source` request body parameter to
  36. `false`. The document source is not included in the response.
  37. [source,console]
  38. ----
  39. GET /_search
  40. {
  41. "_source": false,
  42. "query": {
  43. "term": {
  44. "user.id": "8a4f500d"
  45. }
  46. }
  47. }
  48. ----
  49. To return only a subset of source fields, specify a wildcard (`*`) pattern in
  50. the `_source` parameter. The following search API request returns the source for
  51. only the `obj` field and its properties.
  52. [source,console]
  53. ----
  54. GET /_search
  55. {
  56. "_source": "obj.*",
  57. "query": {
  58. "term": {
  59. "user.id": "8a4f500d"
  60. }
  61. }
  62. }
  63. ----
  64. You can also specify an array of wildcard patterns in the `_source` field. The
  65. following search API request returns the source for only the `obj1` and
  66. `obj2` fields and their properties.
  67. [source,console]
  68. ----
  69. GET /_search
  70. {
  71. "_source": [ "obj1.*", "obj2.*" ],
  72. "query": {
  73. "term": {
  74. "user.id": "8a4f500d"
  75. }
  76. }
  77. }
  78. ----
  79. For finer control, you can specify an object containing arrays of `includes` and
  80. `excludes` patterns in the `_source` parameter.
  81. If the `includes` property is specified, only source fields that match one of
  82. its patterns are returned. You can exclude fields from this subset using the
  83. `excludes` property.
  84. If the `includes` property is not specified, the entire document source is
  85. returned, excluding any fields that match a pattern in the `excludes` property.
  86. The following search API request returns the source for only the `obj1` and
  87. `obj2` fields and their properties, excluding any child `description` fields.
  88. [source,console]
  89. ----
  90. GET /_search
  91. {
  92. "_source": {
  93. "includes": [ "obj1.*", "obj2.*" ],
  94. "excludes": [ "*.description" ]
  95. },
  96. "query": {
  97. "term": {
  98. "user.id": "8a4f500d"
  99. }
  100. }
  101. }
  102. ----
  103. ====
  104. [discrete]
  105. [[docvalue-fields]]
  106. === Doc value fields
  107. You can use the <<docvalue-fields,`docvalue_fields`>> parameter to return
  108. <<doc-values,doc values>> for one or more fields in the search response.
  109. Doc values store the same values as the `_source` but in an on-disk,
  110. column-based structure that's optimized for sorting and aggregations. Since each
  111. field is stored separately, {es} only reads the field values that were requested
  112. and can avoid loading the whole document `_source`.
  113. Doc values are stored for supported fields by default. However, doc values are
  114. not supported for <<text,`text`>> or
  115. {plugins}/mapper-annotated-text-usage.html[`text_annotated`] fields.
  116. .*Example*
  117. [%collapsible]
  118. ====
  119. The following search request uses the `docvalue_fields` parameter to
  120. retrieve doc values for the following fields:
  121. * Fields with names starting with `my_ip`
  122. * `my_keyword_field`
  123. * Fields with names ending with `_date_field`
  124. [source,console]
  125. ----
  126. GET /_search
  127. {
  128. "query": {
  129. "match_all": {}
  130. },
  131. "docvalue_fields": [
  132. "my_ip*", <1>
  133. {
  134. "field": "my_keyword_field" <2>
  135. },
  136. {
  137. "field": "*_date_field",
  138. "format": "epoch_millis" <3>
  139. }
  140. ]
  141. }
  142. ----
  143. <1> Wildcard patten used to match field names, specified as a string.
  144. <2> Wildcard patten used to match field names, specified as an object.
  145. <3> With the object notation, you can use the `format` parameter to specify a
  146. format for the field's returned doc values. <<date,Date fields>> support a
  147. <<mapping-date-format,date `format`>>. <<number,Numeric fields>> support a
  148. https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html[DecimalFormat
  149. pattern]. Other field datatypes do not support the `format` parameter.
  150. ====
  151. TIP: You cannot use the `docvalue_fields` parameter to retrieve doc values for
  152. nested objects. If you specify a nested object, the search returns an empty
  153. array (`[ ]`) for the field. To access nested fields, use the
  154. <<request-body-search-inner-hits, `inner_hits`>> parameter's `docvalue_fields`
  155. property.
  156. [discrete]
  157. [[stored-fields]]
  158. === Stored fields
  159. It's also possible to store an individual field's values by using the
  160. <<mapping-store,`store`>> mapping option. You can use the
  161. <<request-body-search-stored-fields, `stored_fields`>> parameter to include
  162. these stored values in the search response.