1
0

api-conventions.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. [[api-conventions]]
  2. == API conventions
  3. The {es} REST APIs are exposed over HTTP.
  4. Except where noted, the following conventions apply across all APIs.
  5. [discrete]
  6. === Content-type requirements
  7. The type of the content sent in a request body must be specified using
  8. the `Content-Type` header. The value of this header must map to one of
  9. the supported formats that the API supports. Most APIs support JSON,
  10. YAML, CBOR, and SMILE. The bulk and multi-search APIs support NDJSON,
  11. JSON, and SMILE; other types will result in an error response.
  12. When using the `source` query string parameter, the content type must be
  13. specified using the `source_content_type` query string parameter.
  14. {es} only supports UTF-8-encoded JSON. {es} ignores any other encoding headings
  15. sent with a request. Responses are also UTF-8 encoded.
  16. [discrete]
  17. [[x-opaque-id]]
  18. === `X-Opaque-Id` HTTP header
  19. You can pass an `X-Opaque-Id` HTTP header to track the origin of a request in
  20. {es} logs and tasks. If provided, {es} surfaces the `X-Opaque-Id` value in the:
  21. * Response of any request that includes the header
  22. * <<_identifying_running_tasks,Task management API>> response
  23. * <<search-slow-log,Slow logs>>
  24. * <<deprecation-logging,Deprecation logs>>
  25. For the deprecation logs, {es} also uses the `X-Opaque-Id` value to throttle
  26. and deduplicate deprecation warnings. See <<_deprecation_logs_throttling>>.
  27. The `X-Opaque-Id` header accepts any arbitrary value. However, we recommend you
  28. limit these values to a finite set, such as an ID per client. Don't generate a
  29. unique `X-Opaque-Id` header for every request. Too many unique `X-Opaque-Id`
  30. values can prevent {es} from deduplicating warnings in the deprecation logs.
  31. [discrete]
  32. [[traceparent]]
  33. === `traceparent` HTTP header
  34. {es} also supports a `traceparent` HTTP header using the
  35. https://www.w3.org/TR/trace-context/#traceparent-header[official W3C trace
  36. context spec]. You can use the `traceparent` header to trace requests across
  37. Elastic products and other services. Because it's only used for traces, you can
  38. safely generate a unique `traceparent` header for each request.
  39. If provided, {es} surfaces the header's `trace-id` value as `trace.id` in the:
  40. * <<logging,JSON {es} server logs>>
  41. * <<search-slow-log,Slow logs>>
  42. * <<deprecation-logging,Deprecation logs>>
  43. For example, the following `traceparent` value would produce the following
  44. `trace.id` value in the above logs.
  45. [source,txt]
  46. ----
  47. `traceparent`: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01
  48. `trace.id`: 0af7651916cd43dd8448eb211c80319c
  49. ----
  50. [discrete]
  51. [[get-requests]]
  52. === GET and POST requests
  53. A number of {es} GET APIs--most notably the search API--support a request body.
  54. While the GET action makes sense in the context of retrieving information,
  55. GET requests with a body are not supported by all HTTP libraries.
  56. All {es} GET APIs that require a body can also be submitted as POST requests.
  57. Alternatively, you can pass the request body as the
  58. <<api-request-body-query-string, `source` query string parameter>>
  59. when using GET.
  60. include::rest-api/cron-expressions.asciidoc[]
  61. [discrete]
  62. [[api-date-math-index-names]]
  63. === Date math support in index and index alias names
  64. Date math name resolution lets you to search a range of time series indices or
  65. index aliases rather than searching all of your indices and filtering the
  66. results. Limiting the number of searched indices reduces cluster load and
  67. improves search performance. For example, if you are searching for errors in
  68. your daily logs, you can use a date math name template to restrict the search to
  69. the past two days.
  70. Most APIs that accept an index or index alias argument support date math. A date
  71. math name takes the following form:
  72. [source,txt]
  73. ----------------------------------------------------------------------
  74. <static_name{date_math_expr{date_format|time_zone}}>
  75. ----------------------------------------------------------------------
  76. Where:
  77. [horizontal]
  78. `static_name`:: Static text
  79. `date_math_expr`:: Dynamic date math expression that computes the date dynamically
  80. `date_format`:: Optional format in which the computed date should be rendered. Defaults to `yyyy.MM.dd`. Format should be compatible with java-time https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
  81. `time_zone`:: Optional time zone. Defaults to `UTC`.
  82. NOTE: Pay attention to the usage of small vs capital letters used in the `date_format`. For example:
  83. `mm` denotes minute of hour, while `MM` denotes month of year. Similarly `hh` denotes the hour in the
  84. `1-12` range in combination with `AM/PM`, while `HH` denotes the hour in the `0-23` 24-hour range.
  85. Date math expressions are resolved locale-independent. Consequently, it is not possible to use any other
  86. calendars than the Gregorian calendar.
  87. You must enclose date math names in angle brackets. If you use the name in a
  88. request path, special characters must be URI encoded. For example:
  89. [source,console]
  90. ----
  91. # PUT /<my-index-{now/d}>
  92. PUT /%3Cmy-index-%7Bnow%2Fd%7D%3E
  93. ----
  94. [NOTE]
  95. .Percent encoding of date math characters
  96. ======================================================
  97. The special characters used for date rounding must be URI encoded as follows:
  98. [horizontal]
  99. `<`:: `%3C`
  100. `>`:: `%3E`
  101. `/`:: `%2F`
  102. `{`:: `%7B`
  103. `}`:: `%7D`
  104. `|`:: `%7C`
  105. `+`:: `%2B`
  106. `:`:: `%3A`
  107. `,`:: `%2C`
  108. ======================================================
  109. The following example shows different forms of date math names and the final names
  110. they resolve to given the current time is 22nd March 2024 noon UTC.
  111. [options="header"]
  112. |======
  113. | Expression |Resolves to
  114. | `<logstash-{now/d}>` | `logstash-2024.03.22`
  115. | `<logstash-{now/M}>` | `logstash-2024.03.01`
  116. | `<logstash-{now/M{yyyy.MM}}>` | `logstash-2024.03`
  117. | `<logstash-{now/M-1M{yyyy.MM}}>` | `logstash-2024.02`
  118. | `<logstash-{now/d{yyyy.MM.dd\|+12:00}}>` | `logstash-2024.03.23`
  119. |======
  120. To use the characters `{` and `}` in the static part of a name template, escape them
  121. with a backslash `\`, for example:
  122. * `<elastic\\{ON\\}-{now/M}>` resolves to `elastic{ON}-2024.03.01`
  123. The following example shows a search request that searches the Logstash indices for the past
  124. three days, assuming the indices use the default Logstash index name format,
  125. `logstash-YYYY.MM.dd`.
  126. [source,console]
  127. ----------------------------------------------------------------------
  128. # GET /<logstash-{now/d-2d}>,<logstash-{now/d-1d}>,<logstash-{now/d}>/_search
  129. GET /%3Clogstash-%7Bnow%2Fd-2d%7D%3E%2C%3Clogstash-%7Bnow%2Fd-1d%7D%3E%2C%3Clogstash-%7Bnow%2Fd%7D%3E/_search
  130. {
  131. "query" : {
  132. "match": {
  133. "test": "data"
  134. }
  135. }
  136. }
  137. ----------------------------------------------------------------------
  138. // TEST[s/^/PUT logstash-2016.09.20\nPUT logstash-2016.09.19\nPUT logstash-2016.09.18\n/]
  139. // TEST[s/now/2016.09.20%7C%7C/]
  140. [discrete]
  141. [[api-multi-index]]
  142. === Multi-target syntax
  143. Most APIs that accept a `<data-stream>`, `<index>`, or `<target>` request path
  144. parameter also support _multi-target syntax_.
  145. In multi-target syntax, you can use a comma-separated list to run a request on
  146. multiple resources, such as data streams, indices, or aliases:
  147. `test1,test2,test3`. You can also use {wikipedia}/Glob_(programming)[glob-like]
  148. wildcard (`*`) expressions to target resources that match a pattern: `test*` or
  149. `*test` or `te*t` or `*test*`.
  150. You can exclude targets using the `-` character: `test*,-test3`.
  151. IMPORTANT: Aliases are resolved after wildcard expressions. This can result in a
  152. request that targets an excluded alias. For example, if `test3` is an index
  153. alias, the pattern `test*,-test3` still targets the indices for `test3`. To
  154. avoid this, exclude the concrete indices for the alias instead.
  155. You can also exclude clusters from a list of clusters to search using the `-` character:
  156. `remote*:*,-remote1:*,-remote4:*` will search all clusters with an alias that starts
  157. with "remote" except for "remote1" and "remote4". Note that to exclude a cluster
  158. with this notation you must exclude all of its indexes. Excluding a subset of indexes
  159. on a remote cluster is currently not supported. For example, this will throw an exception:
  160. `remote*:*,-remote1:logs*`.
  161. Multi-target APIs that can target indices support the following query
  162. string parameters:
  163. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  164. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  165. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  166. The defaults settings for the above parameters depend on the API being used.
  167. Some multi-target APIs that can target indices also support the following query
  168. string parameter:
  169. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=ignore_throttled]
  170. NOTE: APIs with a single target, such as the <<docs-get,get document API>>, do
  171. not support multi-target syntax.
  172. [discrete]
  173. [[multi-hidden]]
  174. ==== Hidden data streams and indices
  175. For most APIs, wildcard expressions do not match hidden data streams and indices
  176. by default. To match hidden data streams and indices using a wildcard
  177. expression, you must specify the `expand_wildcards` query parameter.
  178. Alternatively, querying an index pattern starting with a dot, such as
  179. `.watcher_hist*`, will match hidden indices by default. This is intended to
  180. mirror Unix file-globbing behavior and provide a smoother transition path to
  181. hidden indices.
  182. You can create hidden data streams by setting `data_stream.hidden` to `true` in
  183. the stream's matching <<indices-put-template,index template>>. You can hide
  184. indices using the <<index-hidden,`index.hidden`>> index setting.
  185. The backing indices for data streams are hidden automatically. Some features,
  186. such as {ml}, store information in hidden indices.
  187. Global index templates that match all indices are not applied to hidden indices.
  188. [discrete]
  189. [[system-indices]]
  190. ==== System indices
  191. {es} modules and plugins can store configuration and state information in internal _system indices_.
  192. You should not directly access or modify system indices
  193. as they contain data essential to the operation of the system.
  194. IMPORTANT: Direct access to system indices is deprecated and
  195. will no longer be allowed in a future major version.
  196. To view system indices within cluster:
  197. [source,console]
  198. --------------------------------------------------
  199. GET _cluster/state/metadata?filter_path=metadata.indices.*.system
  200. --------------------------------------------------
  201. WARNING: When overwriting current cluster state, system indices should be restored
  202. as part of their {ref}/snapshot-restore.html#feature-state[feature state].
  203. [discrete]
  204. [[node-spec-convention]]
  205. ==== Node specification
  206. Some cluster-level APIs may operate on a subset of the nodes which can be specified with node filters. For example, task management, node stats, and node info APIs can all report results from a filtered set of nodes rather than from all nodes. <<cluster-nodes,Learn more>>.
  207. [discrete]
  208. [[component-selectors]]
  209. ==== Component selectors
  210. A data stream component is a logical grouping of indices that help organize data inside a data stream. All data streams contain a `data` component by default. The `data` component comprises the data stream's backing indices. When searching, managing, or indexing into a data stream, the data component is what you are interacting with by default.
  211. Some data stream features are exposed as additional components alongside its `data` component. These other components are comprised of separate sets of backing indices. These additional components store supplemental data independent of the data stream's regular backing indices. An example of another component is the `failures` component exposed by the data stream <<failure-store, failure store>> feature, which captures documents that fail to be ingested in a separate set of backing indices on the data stream.
  212. Some APIs that accept a `<data-stream>`, `<index>`, or `<target>` request path parameter also support selector syntax which defines which component on a data stream the API should operate on. To use a selector, it is appended to the index or data stream name. Selectors can be combined with other index pattern syntax like <<api-date-math-index-names,date math>> and wildcards.
  213. There are two selector suffixes supported by Elasticsearch APIs:
  214. `::data`::
  215. Refers to a data stream's backing indices containing regular data. Data streams always contain a data component.
  216. `::failures`::
  217. This component refers to the internal indices used for a data stream's <<failure-store, failure store>>.
  218. As an example, <<search>>, <<search-field-caps,field capabilities>>, and <<indices-stats,index stats>> APIs can all report results from a different component rather than from the default data.
  219. [source,console]
  220. ----
  221. # Search a data stream normally
  222. GET my-data-stream/_search
  223. # Search a data stream's failure data if present
  224. GET my-data-stream::failures/_search
  225. # Syntax can be combined with other index pattern syntax (wildcards, multi-target, date math, cross cluster search, etc)
  226. GET logs-*::failures/_search
  227. GET logs-*::data,logs-*::failures/_count
  228. GET remote-cluster:logs-*-*::failures/_search
  229. GET *::data,*::failures,-logs-rdbms-*::failures/_stats
  230. GET <logs-{now/d}>::failures/_search
  231. ----
  232. // TEST[skip:backport]
  233. [discrete]
  234. [[api-conventions-parameters]]
  235. === Parameters
  236. Rest parameters (when using HTTP, map to HTTP URL parameters) follow the
  237. convention of using underscore casing.
  238. [discrete]
  239. [[api-request-body-query-string]]
  240. === Request body in query string
  241. For libraries that don't accept a request body for non-POST requests,
  242. you can pass the request body as the `source` query string parameter
  243. instead. When using this method, the `source_content_type` parameter
  244. should also be passed with a media type value that indicates the format
  245. of the source, such as `application/json`.
  246. [discrete]
  247. [[api-compatibility]]
  248. === REST API version compatibility
  249. Major version upgrades often include a number of breaking changes
  250. that impact how you interact with {es}.
  251. While we recommend that you monitor the deprecation logs and
  252. update applications before upgrading {es},
  253. having to coordinate the necessary changes can be an impediment to upgrading.
  254. You can enable an existing application to function without modification after
  255. an upgrade by including API compatibility headers, which tell {es} you are still
  256. using the previous version of the REST API. Using these headers allows the
  257. structure of requests and responses to remain the same; it does not guarantee
  258. the same behavior.
  259. You set version compatibility on a per-request basis in the `Content-Type` and `Accept` headers.
  260. Setting `compatible-with` to the same major version as
  261. the version you're running has no impact,
  262. but ensures that the request will still work after {es} is upgraded.
  263. To tell {es} 8.0 you are using the 7.x request and response format,
  264. set `compatible-with=7`:
  265. [source,sh]
  266. ----------------------------------------------------------------------
  267. Content-Type: application/vnd.elasticsearch+json; compatible-with=7
  268. Accept: application/vnd.elasticsearch+json; compatible-with=7
  269. ----------------------------------------------------------------------
  270. [discrete]
  271. [[api-push-back]]
  272. === HTTP `429 Too Many Requests` status code push back
  273. {es} APIs may respond with the HTTP `429 Too Many Requests` status code, indicating that the cluster is too busy
  274. to handle the request. When this happens, consider retrying after a short delay. If the retry also receives
  275. a `429 Too Many Requests` response, extend the delay by backing off exponentially before each subsequent retry.
  276. [discrete]
  277. [[api-url-access-control]]
  278. === URL-based access control
  279. Many users use a proxy with URL-based access control to secure access to
  280. {es} data streams and indices. For <<search-multi-search,multi-search>>,
  281. <<docs-multi-get,multi-get>>, and <<docs-bulk,bulk>> requests, the user has
  282. the choice of specifying a data stream or index in the URL and on each individual request
  283. within the request body. This can make URL-based access control challenging.
  284. To prevent the user from overriding the data stream or index specified in the
  285. URL, set `rest.action.multi.allow_explicit_index` to `false` in `elasticsearch.yml`.
  286. This causes {es} to
  287. reject requests that explicitly specify a data stream or index in the request body.
  288. [discrete]
  289. === Boolean Values
  290. All REST API parameters (both request parameters and JSON body) support
  291. providing boolean "false" as the value `false` and boolean "true" as the
  292. value `true`. All other values will raise an error.
  293. [[api-conventions-number-values]]
  294. [discrete]
  295. === Number Values
  296. When passing a numeric parameter in a request body, you may use a `string`
  297. containing the number instead of the native numeric type. For example:
  298. [source,console]
  299. --------------------------------------------------
  300. POST /_search
  301. {
  302. "size": "1000"
  303. }
  304. --------------------------------------------------
  305. Integer-valued fields in a response body are described as `integer` (or
  306. occasionally `long`) in this manual, but there are generally no explicit bounds
  307. on such values. JSON, SMILE, CBOR and YAML all permit arbitrarily large integer
  308. values. Do not assume that `integer` fields in a response body will always fit
  309. into a 32-bit signed integer.
  310. [[byte-units]]
  311. [discrete]
  312. === Byte size units
  313. Whenever the byte size of data needs to be specified, e.g. when setting a buffer size
  314. parameter, the value must specify the unit, like `10kb` for 10 kilobytes. Note that
  315. these units use powers of 1024, so `1kb` means 1024 bytes. The supported units are:
  316. [horizontal]
  317. `b`:: Bytes
  318. `kb`:: Kilobytes
  319. `mb`:: Megabytes
  320. `gb`:: Gigabytes
  321. `tb`:: Terabytes
  322. `pb`:: Petabytes
  323. [[distance-units]]
  324. [discrete]
  325. === Distance Units
  326. Wherever distances need to be specified, such as the `distance` parameter in
  327. the <<query-dsl-geo-distance-query>>), the default unit is meters if none is specified.
  328. Distances can be specified in other units, such as `"1km"` or
  329. `"2mi"` (2 miles).
  330. The full list of units is listed below:
  331. [horizontal]
  332. Mile:: `mi` or `miles`
  333. Yard:: `yd` or `yards`
  334. Feet:: `ft` or `feet`
  335. Inch:: `in` or `inch`
  336. Kilometer:: `km` or `kilometers`
  337. Meter:: `m` or `meters`
  338. Centimeter:: `cm` or `centimeters`
  339. Millimeter:: `mm` or `millimeters`
  340. Nautical mile:: `NM`, `nmi`, or `nauticalmiles`
  341. [discrete]
  342. [[time-units]]
  343. === Time units
  344. Whenever durations need to be specified, e.g. for a `timeout` parameter, the duration must specify
  345. the unit, like `2d` for 2 days. The supported units are:
  346. [horizontal]
  347. `d`:: Days
  348. `h`:: Hours
  349. `m`:: Minutes
  350. `s`:: Seconds
  351. `ms`:: Milliseconds
  352. `micros`:: Microseconds
  353. `nanos`:: Nanoseconds
  354. [[size-units]]
  355. [discrete]
  356. === Unit-less quantities
  357. Unit-less quantities means that they don't have a "unit" like "bytes" or "Hertz" or "meter" or "long tonne".
  358. If one of these quantities is large we'll print it out like 10m for 10,000,000 or 7k for 7,000. We'll still print 87
  359. when we mean 87 though. These are the supported multipliers:
  360. [horizontal]
  361. `k`:: Kilo
  362. `m`:: Mega
  363. `g`:: Giga
  364. `t`:: Tera
  365. `p`:: Peta