1
0

common-options.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. [[common-options]]
  2. == Common options
  3. All {es} REST APIs support the following options.
  4. [discrete]
  5. === Pretty Results
  6. When appending `?pretty=true` to any request made, the JSON returned
  7. will be pretty formatted (use it for debugging only!). Another option is
  8. to set `?format=yaml` which will cause the result to be returned in the
  9. (sometimes) more readable yaml format.
  10. [discrete]
  11. === Human readable output
  12. Statistics are returned in a format suitable for humans
  13. (e.g. `"exists_time": "1h"` or `"size": "1kb"`) and for computers
  14. (e.g. `"exists_time_in_millis": 3600000` or `"size_in_bytes": 1024`).
  15. The human readable values can be turned off by adding `?human=false`
  16. to the query string. This makes sense when the stats results are
  17. being consumed by a monitoring tool, rather than intended for human
  18. consumption. The default for the `human` flag is
  19. `false`.
  20. [[date-math]]
  21. [discrete]
  22. === Date Math
  23. Most parameters which accept a formatted date value -- such as `gt` and `lt`
  24. in <<query-dsl-range-query,`range` queries>>, or `from` and `to`
  25. in <<search-aggregations-bucket-daterange-aggregation,`daterange`
  26. aggregations>> -- understand date maths.
  27. The expression starts with an anchor date, which can either be `now`, or a
  28. date string ending with `||`. This anchor date can optionally be followed by
  29. one or more maths expressions:
  30. * `+1h`: Add one hour
  31. * `-1d`: Subtract one day
  32. * `/d`: Round down to the nearest day
  33. The supported time units differ from those supported by <<time-units, time units>> for durations.
  34. The supported units are:
  35. [horizontal]
  36. `y`:: Years
  37. `M`:: Months
  38. `w`:: Weeks
  39. `d`:: Days
  40. `h`:: Hours
  41. `H`:: Hours
  42. `m`:: Minutes
  43. `s`:: Seconds
  44. Assuming `now` is `2001-01-01 12:00:00`, some examples are:
  45. [horizontal]
  46. `now+1h`:: `now` in milliseconds plus one hour. Resolves to: `2001-01-01 13:00:00`
  47. `now-1h`:: `now` in milliseconds minus one hour. Resolves to: `2001-01-01 11:00:00`
  48. `now-1h/d`:: `now` in milliseconds minus one hour, rounded down to UTC 00:00. Resolves to: `2001-01-01 00:00:00`
  49. `2001.02.01\|\|+1M/d`:: `2001-02-01` in milliseconds plus one month. Resolves to: `2001-03-01 00:00:00`
  50. [discrete]
  51. [[common-options-response-filtering]]
  52. === Response Filtering
  53. All REST APIs accept a `filter_path` parameter that can be used to reduce
  54. the response returned by Elasticsearch. This parameter takes a comma
  55. separated list of filters expressed with the dot notation:
  56. [source,console]
  57. --------------------------------------------------
  58. GET /_search?q=kimchy&filter_path=took,hits.hits._id,hits.hits._score
  59. --------------------------------------------------
  60. // TEST[setup:my_index]
  61. Responds:
  62. [source,console-result]
  63. --------------------------------------------------
  64. {
  65. "took" : 3,
  66. "hits" : {
  67. "hits" : [
  68. {
  69. "_id" : "0",
  70. "_score" : 1.6375021
  71. }
  72. ]
  73. }
  74. }
  75. --------------------------------------------------
  76. // TESTRESPONSE[s/"took" : 3/"took" : $body.took/]
  77. // TESTRESPONSE[s/1.6375021/$body.hits.hits.0._score/]
  78. It also supports the `*` wildcard character to match any field or part
  79. of a field's name:
  80. [source,console]
  81. --------------------------------------------------
  82. GET /_cluster/state?filter_path=metadata.indices.*.stat*
  83. --------------------------------------------------
  84. // TEST[s/^/PUT my-index-000001\n/]
  85. Responds:
  86. [source,console-result]
  87. --------------------------------------------------
  88. {
  89. "metadata" : {
  90. "indices" : {
  91. "my-index-000001": {"state": "open"}
  92. }
  93. }
  94. }
  95. --------------------------------------------------
  96. And the `**` wildcard can be used to include fields without knowing the
  97. exact path of the field. For example, we can return the state
  98. of every shard with this request:
  99. [source,console]
  100. --------------------------------------------------
  101. GET /_cluster/state?filter_path=routing_table.indices.**.state
  102. --------------------------------------------------
  103. // TEST[s/^/PUT my-index-000001\n/]
  104. Responds:
  105. [source,console-result]
  106. --------------------------------------------------
  107. {
  108. "routing_table": {
  109. "indices": {
  110. "my-index-000001": {
  111. "shards": {
  112. "0": [{"state": "STARTED"}, {"state": "UNASSIGNED"}]
  113. }
  114. }
  115. }
  116. }
  117. }
  118. --------------------------------------------------
  119. It is also possible to exclude one or more fields by prefixing the filter with the char `-`:
  120. [source,console]
  121. --------------------------------------------------
  122. GET /_count?filter_path=-_shards
  123. --------------------------------------------------
  124. // TEST[setup:my_index]
  125. Responds:
  126. [source,console-result]
  127. --------------------------------------------------
  128. {
  129. "count" : 5
  130. }
  131. --------------------------------------------------
  132. And for more control, both inclusive and exclusive filters can be combined in the same expression. In
  133. this case, the exclusive filters will be applied first and the result will be filtered again using the
  134. inclusive filters:
  135. [source,console]
  136. --------------------------------------------------
  137. GET /_cluster/state?filter_path=metadata.indices.*.state,-metadata.indices.logstash-*
  138. --------------------------------------------------
  139. // TEST[s/^/PUT my-index-000001\nPUT my-index-000002\nPUT my-index-000003\nPUT logstash-2016.01\n/]
  140. Responds:
  141. [source,console-result]
  142. --------------------------------------------------
  143. {
  144. "metadata" : {
  145. "indices" : {
  146. "my-index-000001" : {"state" : "open"},
  147. "my-index-000002" : {"state" : "open"},
  148. "my-index-000003" : {"state" : "open"}
  149. }
  150. }
  151. }
  152. --------------------------------------------------
  153. Note that Elasticsearch sometimes returns directly the raw value of a field,
  154. like the `_source` field. If you want to filter `_source` fields, you should
  155. consider combining the already existing `_source` parameter (see
  156. <<get-source-filtering,Get API>> for more details) with the `filter_path`
  157. parameter like this:
  158. [source,console]
  159. --------------------------------------------------
  160. POST /library/_doc?refresh
  161. {"title": "Book #1", "rating": 200.1}
  162. POST /library/_doc?refresh
  163. {"title": "Book #2", "rating": 1.7}
  164. POST /library/_doc?refresh
  165. {"title": "Book #3", "rating": 0.1}
  166. GET /_search?filter_path=hits.hits._source&_source=title&sort=rating:desc
  167. --------------------------------------------------
  168. [source,console-result]
  169. --------------------------------------------------
  170. {
  171. "hits" : {
  172. "hits" : [ {
  173. "_source":{"title":"Book #1"}
  174. }, {
  175. "_source":{"title":"Book #2"}
  176. }, {
  177. "_source":{"title":"Book #3"}
  178. } ]
  179. }
  180. }
  181. --------------------------------------------------
  182. [discrete]
  183. === Flat Settings
  184. The `flat_settings` flag affects rendering of the lists of settings. When the
  185. `flat_settings` flag is `true`, settings are returned in a flat format:
  186. [source,console]
  187. --------------------------------------------------
  188. GET my-index-000001/_settings?flat_settings=true
  189. --------------------------------------------------
  190. // TEST[setup:my_index]
  191. Returns:
  192. [source,console-result]
  193. --------------------------------------------------
  194. {
  195. "my-index-000001" : {
  196. "settings": {
  197. "index.number_of_replicas": "1",
  198. "index.number_of_shards": "1",
  199. "index.creation_date": "1474389951325",
  200. "index.uuid": "n6gzFZTgS664GUfx0Xrpjw",
  201. "index.version.created": ...,
  202. "index.routing.allocation.include._tier_preference" : "data_content",
  203. "index.provided_name" : "my-index-000001"
  204. }
  205. }
  206. }
  207. --------------------------------------------------
  208. // TESTRESPONSE[s/1474389951325/$body.my-index-000001.settings.index\\\\.creation_date/]
  209. // TESTRESPONSE[s/n6gzFZTgS664GUfx0Xrpjw/$body.my-index-000001.settings.index\\\\.uuid/]
  210. // TESTRESPONSE[s/"index.version.created": \.\.\./"index.version.created": $body.my-index-000001.settings.index\\\\.version\\\\.created/]
  211. When the `flat_settings` flag is `false`, settings are returned in a more
  212. human readable structured format:
  213. [source,console]
  214. --------------------------------------------------
  215. GET my-index-000001/_settings?flat_settings=false
  216. --------------------------------------------------
  217. // TEST[setup:my_index]
  218. Returns:
  219. [source,console-result]
  220. --------------------------------------------------
  221. {
  222. "my-index-000001" : {
  223. "settings" : {
  224. "index" : {
  225. "number_of_replicas": "1",
  226. "number_of_shards": "1",
  227. "creation_date": "1474389951325",
  228. "uuid": "n6gzFZTgS664GUfx0Xrpjw",
  229. "version": {
  230. "created": ...
  231. },
  232. "routing": {
  233. "allocation": {
  234. "include": {
  235. "_tier_preference": "data_content"
  236. }
  237. }
  238. },
  239. "provided_name" : "my-index-000001"
  240. }
  241. }
  242. }
  243. }
  244. --------------------------------------------------
  245. // TESTRESPONSE[s/1474389951325/$body.my-index-000001.settings.index.creation_date/]
  246. // TESTRESPONSE[s/n6gzFZTgS664GUfx0Xrpjw/$body.my-index-000001.settings.index.uuid/]
  247. // TESTRESPONSE[s/"created": \.\.\./"created": $body.my-index-000001.settings.index.version.created/]
  248. By default `flat_settings` is set to `false`.
  249. [[fuzziness]]
  250. [discrete]
  251. === Fuzziness
  252. Some queries and APIs support parameters to allow inexact _fuzzy_ matching,
  253. using the `fuzziness` parameter.
  254. When querying `text` or `keyword` fields, `fuzziness` is interpreted as a
  255. {wikipedia}/Levenshtein_distance[Levenshtein Edit Distance]
  256. -- the number of one character changes that need to be made to one string to
  257. make it the same as another string.
  258. The `fuzziness` parameter can be specified as:
  259. [horizontal]
  260. `0`, `1`, `2`::
  261. The maximum allowed Levenshtein Edit Distance (or number of edits)
  262. `AUTO`::
  263. +
  264. --
  265. Generates an edit distance based on the length of the term.
  266. Low and high distance arguments may be optionally provided `AUTO:[low],[high]`. If not specified,
  267. the default values are 3 and 6, equivalent to `AUTO:3,6` that make for lengths:
  268. `0..2`:: Must match exactly
  269. `3..5`:: One edit allowed
  270. `>5`:: Two edits allowed
  271. `AUTO` should generally be the preferred value for `fuzziness`.
  272. --
  273. [discrete]
  274. [[common-options-error-options]]
  275. === Enabling stack traces
  276. By default when a request returns an error Elasticsearch doesn't include the
  277. stack trace of the error. You can enable that behavior by setting the
  278. `error_trace` url parameter to `true`. For example, by default when you send an
  279. invalid `size` parameter to the `_search` API:
  280. [source,console]
  281. ----------------------------------------------------------------------
  282. POST /my-index-000001/_search?size=surprise_me
  283. ----------------------------------------------------------------------
  284. // TEST[s/surprise_me/surprise_me&error_trace=false/ catch:bad_request]
  285. // Since the test system sends error_trace=true by default we have to override
  286. The response looks like:
  287. [source,console-result]
  288. ----------------------------------------------------------------------
  289. {
  290. "error" : {
  291. "root_cause" : [
  292. {
  293. "type" : "illegal_argument_exception",
  294. "reason" : "Failed to parse int parameter [size] with value [surprise_me]"
  295. }
  296. ],
  297. "type" : "illegal_argument_exception",
  298. "reason" : "Failed to parse int parameter [size] with value [surprise_me]",
  299. "caused_by" : {
  300. "type" : "number_format_exception",
  301. "reason" : "For input string: \"surprise_me\""
  302. }
  303. },
  304. "status" : 400
  305. }
  306. ----------------------------------------------------------------------
  307. But if you set `error_trace=true`:
  308. [source,console]
  309. ----------------------------------------------------------------------
  310. POST /my-index-000001/_search?size=surprise_me&error_trace=true
  311. ----------------------------------------------------------------------
  312. // TEST[catch:bad_request]
  313. The response looks like:
  314. [source,console-result]
  315. ----------------------------------------------------------------------
  316. {
  317. "error": {
  318. "root_cause": [
  319. {
  320. "type": "illegal_argument_exception",
  321. "reason": "Failed to parse int parameter [size] with value [surprise_me]",
  322. "stack_trace": "Failed to parse int parameter [size] with value [surprise_me]]; nested: IllegalArgumentException..."
  323. }
  324. ],
  325. "type": "illegal_argument_exception",
  326. "reason": "Failed to parse int parameter [size] with value [surprise_me]",
  327. "stack_trace": "java.lang.IllegalArgumentException: Failed to parse int parameter [size] with value [surprise_me]\n at org.elasticsearch.rest.RestRequest.paramAsInt(RestRequest.java:175)...",
  328. "caused_by": {
  329. "type": "number_format_exception",
  330. "reason": "For input string: \"surprise_me\"",
  331. "stack_trace": "java.lang.NumberFormatException: For input string: \"surprise_me\"\n at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)..."
  332. }
  333. },
  334. "status": 400
  335. }
  336. ----------------------------------------------------------------------
  337. // TESTRESPONSE[s/"stack_trace": "Failed to parse int parameter.+\.\.\."/"stack_trace": $body.error.root_cause.0.stack_trace/]
  338. // TESTRESPONSE[s/"stack_trace": "java.lang.IllegalArgum.+\.\.\."/"stack_trace": $body.error.stack_trace/]
  339. // TESTRESPONSE[s/"stack_trace": "java.lang.Number.+\.\.\."/"stack_trace": $body.error.caused_by.stack_trace/]