search-fields.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. The following search API request sets the `_source` request body parameter to
  33. `false`. The document source is not included in the response.
  34. [source,console]
  35. ----
  36. GET /_search
  37. {
  38. "_source": false,
  39. "query": {
  40. "term": {
  41. "user.id": "8a4f500d"
  42. }
  43. }
  44. }
  45. ----
  46. To return only a subset of source fields, specify a wildcard (`*`) pattern in
  47. the `_source` parameter. The following search API request returns the source for
  48. only the `obj` field and its properties.
  49. [source,console]
  50. ----
  51. GET /_search
  52. {
  53. "_source": "obj.*",
  54. "query": {
  55. "term": {
  56. "user.id": "8a4f500d"
  57. }
  58. }
  59. }
  60. ----
  61. You can also specify an array of wildcard patterns in the `_source` field. The
  62. following search API request returns the source for only the `obj1` and
  63. `obj2` fields and their properties.
  64. [source,console]
  65. ----
  66. GET /_search
  67. {
  68. "_source": [ "obj1.*", "obj2.*" ],
  69. "query": {
  70. "term": {
  71. "user.id": "8a4f500d"
  72. }
  73. }
  74. }
  75. ----
  76. For finer control, you can specify an object containing arrays of `includes` and
  77. `excludes` patterns in the `_source` parameter.
  78. If the `includes` property is specified, only source fields that match one of
  79. its patterns are returned. You can exclude fields from this subset using the
  80. `excludes` property.
  81. If the `includes` property is not specified, the entire document source is
  82. returned, excluding any fields that match a pattern in the `excludes` property.
  83. The following search API request returns the source for only the `obj1` and
  84. `obj2` fields and their properties, excluding any child `description` fields.
  85. [source,console]
  86. ----
  87. GET /_search
  88. {
  89. "_source": {
  90. "includes": [ "obj1.*", "obj2.*" ],
  91. "excludes": [ "*.description" ]
  92. },
  93. "query": {
  94. "term": {
  95. "user.id": "8a4f500d"
  96. }
  97. }
  98. }
  99. ----
  100. [discrete]
  101. [[docvalue-fields]]
  102. === Doc value fields
  103. You can use the <<docvalue-fields,`docvalue_fields`>> parameter to return
  104. <<doc-values,doc values>> for one or more fields in the search response.
  105. Doc values store the same values as the `_source` but in an on-disk,
  106. column-based structure that's optimized for sorting and aggregations. Since each
  107. field is stored separately, {es} only reads the field values that were requested
  108. and can avoid loading the whole document `_source`.
  109. Doc values are stored for supported fields by default. However, doc values are
  110. not supported for <<text,`text`>> or
  111. {plugins}/mapper-annotated-text-usage.html[`text_annotated`] fields.
  112. The following search request uses the `docvalue_fields` parameter to
  113. retrieve doc values for the following fields:
  114. * Fields with names starting with `my_ip`
  115. * `my_keyword_field`
  116. * Fields with names ending with `_date_field`
  117. [source,console]
  118. ----
  119. GET /_search
  120. {
  121. "query": {
  122. "match_all": {}
  123. },
  124. "docvalue_fields": [
  125. "my_ip*", <1>
  126. {
  127. "field": "my_keyword_field" <2>
  128. },
  129. {
  130. "field": "*_date_field",
  131. "format": "epoch_millis" <3>
  132. }
  133. ]
  134. }
  135. ----
  136. <1> Wildcard patten used to match field names, specified as a string.
  137. <2> Wildcard patten used to match field names, specified as an object.
  138. <3> With the object notation, you can use the `format` parameter to specify a
  139. format for the field's returned doc values. <<date,Date fields>> support a
  140. <<mapping-date-format,date `format`>>. <<number,Numeric fields>> support a
  141. https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html[DecimalFormat
  142. pattern]. Other field data types do not support the `format` parameter.
  143. TIP: You cannot use the `docvalue_fields` parameter to retrieve doc values for
  144. nested objects. If you specify a nested object, the search returns an empty
  145. array (`[ ]`) for the field. To access nested fields, use the
  146. <<request-body-search-inner-hits, `inner_hits`>> parameter's `docvalue_fields`
  147. property.
  148. [discrete]
  149. [[stored-fields]]
  150. === Stored fields
  151. It's also possible to store an individual field's values by using the
  152. <<mapping-store,`store`>> mapping option. You can use the
  153. <<request-body-search-stored-fields, `stored_fields`>> parameter to include
  154. these stored values in the search response.