|
4 | 4 |
|
5 | 5 | By default, each hit in the search response includes the document
|
6 | 6 | <<mapping-source-field,`_source`>>, which is the entire JSON object that was
|
7 |
| -provided when indexing the document. If you only need certain source fields in |
8 |
| -the search response, you can use the <<source-filtering,source filtering>> to |
9 |
| -restrict what parts of the source are returned. |
| 7 | +provided when indexing the document. To retrieve specific fields in the search |
| 8 | +response, you can use the `fields` parameter: |
10 | 9 |
|
11 |
| -Returning fields using only the document source has some limitations: |
| 10 | +[source,console] |
| 11 | +---- |
| 12 | +POST twitter/_search |
| 13 | +{ |
| 14 | + "query": { |
| 15 | + "match": { |
| 16 | + "message": "elasticsearch" |
| 17 | + } |
| 18 | + }, |
| 19 | + "fields": ["user", "date"], |
| 20 | + "_source": false |
| 21 | +} |
| 22 | +---- |
| 23 | +// TEST[setup:twitter] |
12 | 24 |
|
13 |
| -* The `_source` field does not include <<multi-fields, multi-fields>> or |
14 |
| -<<alias, field aliases>>. Likewise, a field in the source does not contain |
15 |
| -values copied using the <<copy-to,`copy_to`>> mapping parameter. |
16 |
| -* Since the `_source` is stored as a single field in Lucene, the whole source |
17 |
| -object must be loaded and parsed, even if only a small number of fields are |
18 |
| -needed. |
| 25 | +The `fields` parameter consults both a document's `_source` and the index |
| 26 | +mappings to load and return values. Because it makes use of the mappings, |
| 27 | +`fields` has some advantages over referencing the `_source` directly: it |
| 28 | +accepts <<multi-fields, multi-fields>> and <<alias, field aliases>>, and |
| 29 | +also formats field values like dates in a consistent way. |
19 | 30 |
|
20 |
| -To avoid these limitations, you can: |
| 31 | +A document's `_source` is stored as a single field in Lucene. So the whole |
| 32 | +`_source` object must be loaded and parsed even if only a small number of |
| 33 | +fields are requested. To avoid this limitation, you can try another option for |
| 34 | +loading fields: |
21 | 35 |
|
22 | 36 | * Use the <<docvalue-fields, `docvalue_fields`>>
|
23 | 37 | parameter to get values for selected fields. This can be a good
|
24 | 38 | choice when returning a fairly small number of fields that support doc values,
|
25 | 39 | such as keywords and dates.
|
26 |
| -* 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.) |
| 40 | +* Use the <<request-body-search-stored-fields, `stored_fields`>> parameter to |
| 41 | +get the values for specific stored fields (fields that use the |
| 42 | +<<mapping-store,`store`>> mapping option). |
27 | 43 |
|
28 |
| -You can find more detailed information on each of these methods in the |
| 44 | +You can find more detailed information on each of these methods in the |
29 | 45 | following sections:
|
30 | 46 |
|
31 |
| -* <<source-filtering>> |
| 47 | +* <<search-fields-param>> |
32 | 48 | * <<docvalue-fields>>
|
33 | 49 | * <<stored-fields>>
|
| 50 | +* <<source-filtering>> |
| 51 | + |
| 52 | +[discrete] |
| 53 | +[[search-fields-param]] |
| 54 | +=== Fields |
| 55 | + |
| 56 | +The `fields` parameter allows for retrieving a list of document fields in |
| 57 | +the search response. It consults both the document `_source` and the index |
| 58 | +mappings to return each value in a standardized way that matches its mapping |
| 59 | +type. By default, date fields are formatted according to the |
| 60 | +<<mapping-date-format,date format>> parameter in their mappings. |
| 61 | + |
| 62 | +.*Example* |
| 63 | +[%collapsible] |
| 64 | +==== |
| 65 | +The following search request uses the `fields` parameter to retrieve values |
| 66 | +for the `user` field, all fields starting with `location.`, and the |
| 67 | +`date` field: |
| 68 | +
|
| 69 | +[source,console] |
| 70 | +---- |
| 71 | +POST twitter/_search |
| 72 | +{ |
| 73 | + "query": { |
| 74 | + "match": { |
| 75 | + "message": "elasticsearch" |
| 76 | + } |
| 77 | + }, |
| 78 | + "fields": [ |
| 79 | + "user", |
| 80 | + "location.*", <1> |
| 81 | + { |
| 82 | + "field": "date", |
| 83 | + "format": "epoch_millis" <2> |
| 84 | + } |
| 85 | + ], |
| 86 | + "_source": false |
| 87 | +} |
| 88 | +---- |
| 89 | +// TEST[continued] |
| 90 | +
|
| 91 | +<1> Both full field names and wildcard patterns are accepted. |
| 92 | +<2> Using object notation, you can pass a `format` parameter to apply a custom |
| 93 | + format for the field's values. This is currently supported for |
| 94 | + <<date,`date` fields>> and <<date_nanos, `date_nanos` fields>>, which |
| 95 | + accept a <<mapping-date-format,date format>>. |
| 96 | +
|
| 97 | +The values are returned as a flat list in the `fields` section in each hit: |
| 98 | +
|
| 99 | +[source,console-result] |
| 100 | +---- |
| 101 | +{ |
| 102 | + "took" : 2, |
| 103 | + "timed_out" : false, |
| 104 | + "_shards" : { |
| 105 | + "total" : 1, |
| 106 | + "successful" : 1, |
| 107 | + "skipped" : 0, |
| 108 | + "failed" : 0 |
| 109 | + }, |
| 110 | + "hits" : { |
| 111 | + "total" : { |
| 112 | + "value" : 1, |
| 113 | + "relation" : "eq" |
| 114 | + }, |
| 115 | + "max_score" : 1.0, |
| 116 | + "hits" : [ |
| 117 | + { |
| 118 | + "_index" : "twitter", |
| 119 | + "_id" : "0", |
| 120 | + "_score" : 1.0, |
| 121 | + "fields" : { |
| 122 | + "user" : [ |
| 123 | + "kimchy" |
| 124 | + ], |
| 125 | + "date" : [ |
| 126 | + "1258294332000" |
| 127 | + ], |
| 128 | + "location.city": [ |
| 129 | + "Amsterdam" |
| 130 | + ], |
| 131 | + "location.country": [ |
| 132 | + "Netherlands" |
| 133 | + ] |
| 134 | + } |
| 135 | + } |
| 136 | + ] |
| 137 | + } |
| 138 | +} |
| 139 | +---- |
| 140 | +// TESTRESPONSE[s/"took" : 2/"took": $body.took/] |
| 141 | +// TESTRESPONSE[s/"max_score" : 1.0/"max_score" : $body.hits.max_score/] |
| 142 | +// TESTRESPONSE[s/"_score" : 1.0/"_score" : $body.hits.hits.0._score/] |
| 143 | +
|
| 144 | +Only leaf fields are returned -- `fields` does not allow for fetching entire |
| 145 | +objects. |
| 146 | +
|
| 147 | +==== |
| 148 | + |
| 149 | +The `fields` parameter handles field types like <<alias, field aliases>> and |
| 150 | +<<constant-keyword, `constant_keyword`>> whose values aren't always present in |
| 151 | +the `_source`. Other mapping options are also respected, including |
| 152 | +<<ignore-above, `ignore_above`>>, <<ignore-malformed, `ignore_malformed`>> and |
| 153 | +<<null-value, `null_value`>>. |
| 154 | + |
| 155 | +[discrete] |
| 156 | +[[docvalue-fields]] |
| 157 | +=== Doc value fields |
| 158 | + |
| 159 | +You can use the <<docvalue-fields,`docvalue_fields`>> parameter to return |
| 160 | +<<doc-values,doc values>> for one or more fields in the search response. |
| 161 | + |
| 162 | +Doc values store the same values as the `_source` but in an on-disk, |
| 163 | +column-based structure that's optimized for sorting and aggregations. Since each |
| 164 | +field is stored separately, {es} only reads the field values that were requested |
| 165 | +and can avoid loading the whole document `_source`. |
| 166 | + |
| 167 | +Doc values are stored for supported fields by default. However, doc values are |
| 168 | +not supported for <<text,`text`>> or |
| 169 | +{plugins}/mapper-annotated-text-usage.html[`text_annotated`] fields. |
| 170 | + |
| 171 | +.*Example* |
| 172 | +[%collapsible] |
| 173 | +==== |
| 174 | +The following search request uses the `docvalue_fields` parameter to retrieve |
| 175 | +doc values for the `user` field, all fields starting with `location.`, and the |
| 176 | +`date` field: |
| 177 | +
|
| 178 | +
|
| 179 | +[source,console] |
| 180 | +---- |
| 181 | +GET twitter/_search |
| 182 | +{ |
| 183 | + "query": { |
| 184 | + "match": { |
| 185 | + "message": "elasticsearch" |
| 186 | + } |
| 187 | + }, |
| 188 | + "docvalue_fields": [ |
| 189 | + "user", |
| 190 | + "location.*", <1> |
| 191 | + { |
| 192 | + "field": "date", |
| 193 | + "format": "epoch_millis" <2> |
| 194 | + } |
| 195 | + ] |
| 196 | +} |
| 197 | +---- |
| 198 | +// TEST[continued] |
| 199 | +
|
| 200 | +<1> Both full field names and wildcard patterns are accepted. |
| 201 | +<2> Using object notation, you can pass a `format` parameter to apply a custom |
| 202 | + format for the field's doc values. <<date,Date fields>> support a |
| 203 | + <<mapping-date-format,date `format`>>. <<number,Numeric fields>> support a |
| 204 | + https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html[DecimalFormat |
| 205 | + pattern]. Other field datatypes do not support the `format` parameter. |
| 206 | +==== |
| 207 | + |
| 208 | +TIP: You cannot use the `docvalue_fields` parameter to retrieve doc values for |
| 209 | +nested objects. If you specify a nested object, the search returns an empty |
| 210 | +array (`[ ]`) for the field. To access nested fields, use the |
| 211 | +<<request-body-search-inner-hits, `inner_hits`>> parameter's `docvalue_fields` |
| 212 | +property. |
34 | 213 |
|
35 | 214 | [discrete]
|
36 | 215 | [[source-filtering]]
|
@@ -122,7 +301,6 @@ GET /_search
|
122 | 301 | ----
|
123 | 302 | ====
|
124 | 303 |
|
125 |
| - |
126 | 304 | [discrete]
|
127 | 305 | [[docvalue-fields]]
|
128 | 306 | === Doc value fields
|
@@ -184,7 +362,6 @@ array (`[ ]`) for the field. To access nested fields, use the
|
184 | 362 | <<request-body-search-inner-hits, `inner_hits`>> parameter's `docvalue_fields`
|
185 | 363 | property.
|
186 | 364 |
|
187 |
| - |
188 | 365 | [discrete]
|
189 | 366 | [[stored-fields]]
|
190 | 367 | === Stored fields
|
|
0 commit comments