api-conventions.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. [[get-requests]]
  18. === GET and POST requests
  19. A number of {es} GET APIs--most notably the search API--support a request body.
  20. While the GET action makes sense in the context of retrieving information,
  21. GET requests with a body are not supported by all HTTP libraries.
  22. All {es} GET APIs that require a body can also be submitted as POST requests.
  23. Alternatively, you can pass the request body as the
  24. <<api-request-body-query-string, `source` query string parameter>>
  25. when using GET.
  26. include::rest-api/cron-expressions.asciidoc[]
  27. [discrete]
  28. [[api-date-math-index-names]]
  29. === Date math support in index and index alias names
  30. Date math name resolution lets you to search a range of time series indices or
  31. index aliases rather than searching all of your indices and filtering the
  32. results. Limiting the number of searched indices reduces cluster load and
  33. improves search performance. For example, if you are searching for errors in
  34. your daily logs, you can use a date math name template to restrict the search to
  35. the past two days.
  36. Most APIs that accept an index or index alias argument support date math. A date
  37. math name takes the following form:
  38. [source,txt]
  39. ----------------------------------------------------------------------
  40. <static_name{date_math_expr{date_format|time_zone}}>
  41. ----------------------------------------------------------------------
  42. Where:
  43. [horizontal]
  44. `static_name`:: Static text
  45. `date_math_expr`:: Dynamic date math expression that computes the date dynamically
  46. `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
  47. `time_zone`:: Optional time zone. Defaults to `UTC`.
  48. NOTE: Pay attention to the usage of small vs capital letters used in the `date_format`. For example:
  49. `mm` denotes minute of hour, while `MM` denotes month of year. Similarly `hh` denotes the hour in the
  50. `1-12` range in combination with `AM/PM`, while `HH` denotes the hour in the `0-23` 24-hour range.
  51. Date math expressions are resolved locale-independent. Consequently, it is not possible to use any other
  52. calendars than the Gregorian calendar.
  53. You must enclose date math names in angle brackets. If you use the name in a
  54. request path, special characters must be URI encoded. For example:
  55. [source,console]
  56. ----
  57. # PUT /<my-index-{now/d}>
  58. PUT /%3Cmy-index-%7Bnow%2Fd%7D%3E
  59. ----
  60. [NOTE]
  61. .Percent encoding of date math characters
  62. ======================================================
  63. The special characters used for date rounding must be URI encoded as follows:
  64. [horizontal]
  65. `<`:: `%3C`
  66. `>`:: `%3E`
  67. `/`:: `%2F`
  68. `{`:: `%7B`
  69. `}`:: `%7D`
  70. `|`:: `%7C`
  71. `+`:: `%2B`
  72. `:`:: `%3A`
  73. `,`:: `%2C`
  74. ======================================================
  75. The following example shows different forms of date math names and the final names
  76. they resolve to given the current time is 22nd March 2024 noon UTC.
  77. [options="header"]
  78. |======
  79. | Expression |Resolves to
  80. | `<logstash-{now/d}>` | `logstash-2024.03.22`
  81. | `<logstash-{now/M}>` | `logstash-2024.03.01`
  82. | `<logstash-{now/M{yyyy.MM}}>` | `logstash-2024.03`
  83. | `<logstash-{now/M-1M{yyyy.MM}}>` | `logstash-2024.02`
  84. | `<logstash-{now/d{yyyy.MM.dd\|+12:00}}>` | `logstash-2024.03.23`
  85. |======
  86. To use the characters `{` and `}` in the static part of a name template, escape them
  87. with a backslash `\`, for example:
  88. * `<elastic\\{ON\\}-{now/M}>` resolves to `elastic{ON}-2024.03.01`
  89. The following example shows a search request that searches the Logstash indices for the past
  90. three days, assuming the indices use the default Logstash index name format,
  91. `logstash-YYYY.MM.dd`.
  92. [source,console]
  93. ----------------------------------------------------------------------
  94. # GET /<logstash-{now/d-2d}>,<logstash-{now/d-1d}>,<logstash-{now/d}>/_search
  95. GET /%3Clogstash-%7Bnow%2Fd-2d%7D%3E%2C%3Clogstash-%7Bnow%2Fd-1d%7D%3E%2C%3Clogstash-%7Bnow%2Fd%7D%3E/_search
  96. {
  97. "query" : {
  98. "match": {
  99. "test": "data"
  100. }
  101. }
  102. }
  103. ----------------------------------------------------------------------
  104. // TEST[s/^/PUT logstash-2016.09.20\nPUT logstash-2016.09.19\nPUT logstash-2016.09.18\n/]
  105. // TEST[s/now/2016.09.20%7C%7C/]
  106. [discrete]
  107. [[api-multi-index]]
  108. === Multi-target syntax
  109. Most APIs that accept a `<data-stream>`, `<index>`, or `<target>` request path
  110. parameter also support _multi-target syntax_.
  111. In multi-target syntax, you can use a comma-separated list to run a request on
  112. multiple resources, such as data streams, indices, or aliases:
  113. `test1,test2,test3`. You can also use {wikipedia}/Glob_(programming)[glob-like]
  114. wildcard (`*`) expressions to target resources that match a pattern: `test*` or
  115. `*test` or `te*t` or `*test*`.
  116. You can exclude targets using the `-` character: `test*,-test3`.
  117. IMPORTANT: Aliases are resolved after wildcard expressions. This can result in a
  118. request that targets an excluded alias. For example, if `test3` is an index
  119. alias, the pattern `test*,-test3` still targets the indices for `test3`. To
  120. avoid this, exclude the concrete indices for the alias instead.
  121. Multi-target APIs that can target indices support the following query
  122. string parameters:
  123. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  124. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  125. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  126. The defaults settings for the above parameters depend on the API being used.
  127. Some multi-target APIs that can target indices also support the following query
  128. string parameter:
  129. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=ignore_throttled]
  130. NOTE: APIs with a single target, such as the <<docs-get,get document API>>, do
  131. not support multi-target syntax.
  132. [discrete]
  133. [[multi-hidden]]
  134. ==== Hidden data streams and indices
  135. For most APIs, wildcard expressions do not match hidden data streams and indices
  136. by default. To match hidden data streams and indices using a wildcard
  137. expression, you must specify the `expand_wildcards` query parameter.
  138. You can create hidden data streams by setting `data_stream.hidden` to `true` in
  139. the stream's matching <<indices-put-template,index template>>. You can hide
  140. indices using the <<index-hidden,`index.hidden`>> index setting.
  141. The backing indices for data streams are hidden automatically. Some features,
  142. such as {ml}, store information in hidden indices.
  143. Global index templates that match all indices are not applied to hidden indices.
  144. [discrete]
  145. [[system-indices]]
  146. ==== System indices
  147. {es} modules and plugins can store configuration and state information in internal _system indices_.
  148. You should not directly access or modify system indices
  149. as they contain data essential to the operation of the system.
  150. IMPORTANT: Direct access to system indices is deprecated and
  151. will no longer be allowed in the next major version.
  152. [discrete]
  153. [[api-conventions-parameters]]
  154. === Parameters
  155. Rest parameters (when using HTTP, map to HTTP URL parameters) follow the
  156. convention of using underscore casing.
  157. [discrete]
  158. [[api-request-body-query-string]]
  159. === Request body in query string
  160. For libraries that don't accept a request body for non-POST requests,
  161. you can pass the request body as the `source` query string parameter
  162. instead. When using this method, the `source_content_type` parameter
  163. should also be passed with a media type value that indicates the format
  164. of the source, such as `application/json`.
  165. [discrete]
  166. [[api-url-access-control]]
  167. === URL-based access control
  168. Many users use a proxy with URL-based access control to secure access to
  169. {es} data streams and indices. For <<search-multi-search,multi-search>>,
  170. <<docs-multi-get,multi-get>>, and <<docs-bulk,bulk>> requests, the user has
  171. the choice of specifying a data stream or index in the URL and on each individual request
  172. within the request body. This can make URL-based access control challenging.
  173. To prevent the user from overriding the data stream or index specified in the
  174. URL, set `rest.action.multi.allow_explicit_index` to `false` in `elasticsearch.yml`.
  175. This causes {es} to
  176. reject requests that explicitly specify a data stream or index in the request body.
  177. [discrete]
  178. === Boolean Values
  179. All REST API parameters (both request parameters and JSON body) support
  180. providing boolean "false" as the value `false` and boolean "true" as the
  181. value `true`. All other values will raise an error.
  182. [discrete]
  183. === Number Values
  184. All REST APIs support providing numbered parameters as `string` on top
  185. of supporting the native JSON number types.
  186. [[byte-units]]
  187. [discrete]
  188. === Byte size units
  189. Whenever the byte size of data needs to be specified, e.g. when setting a buffer size
  190. parameter, the value must specify the unit, like `10kb` for 10 kilobytes. Note that
  191. these units use powers of 1024, so `1kb` means 1024 bytes. The supported units are:
  192. [horizontal]
  193. `b`:: Bytes
  194. `kb`:: Kilobytes
  195. `mb`:: Megabytes
  196. `gb`:: Gigabytes
  197. `tb`:: Terabytes
  198. `pb`:: Petabytes
  199. [[distance-units]]
  200. [discrete]
  201. === Distance Units
  202. Wherever distances need to be specified, such as the `distance` parameter in
  203. the <<query-dsl-geo-distance-query>>), the default unit is meters if none is specified.
  204. Distances can be specified in other units, such as `"1km"` or
  205. `"2mi"` (2 miles).
  206. The full list of units is listed below:
  207. [horizontal]
  208. Mile:: `mi` or `miles`
  209. Yard:: `yd` or `yards`
  210. Feet:: `ft` or `feet`
  211. Inch:: `in` or `inch`
  212. Kilometer:: `km` or `kilometers`
  213. Meter:: `m` or `meters`
  214. Centimeter:: `cm` or `centimeters`
  215. Millimeter:: `mm` or `millimeters`
  216. Nautical mile:: `NM`, `nmi`, or `nauticalmiles`
  217. [discrete]
  218. [[time-units]]
  219. === Time units
  220. Whenever durations need to be specified, e.g. for a `timeout` parameter, the duration must specify
  221. the unit, like `2d` for 2 days. The supported units are:
  222. [horizontal]
  223. `d`:: Days
  224. `h`:: Hours
  225. `m`:: Minutes
  226. `s`:: Seconds
  227. `ms`:: Milliseconds
  228. `micros`:: Microseconds
  229. `nanos`:: Nanoseconds
  230. [[size-units]]
  231. [discrete]
  232. === Unit-less quantities
  233. Unit-less quantities means that they don't have a "unit" like "bytes" or "Hertz" or "meter" or "long tonne".
  234. 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
  235. when we mean 87 though. These are the supported multipliers:
  236. [horizontal]
  237. `k`:: Kilo
  238. `m`:: Mega
  239. `g`:: Giga
  240. `t`:: Tera
  241. `p`:: Peta