api-conventions.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. [[api-conventions]]
  2. = API Conventions
  3. [partintro]
  4. --
  5. The *elasticsearch* REST APIs are exposed using <<modules-http,JSON over HTTP>>.
  6. The conventions listed in this chapter can be applied throughout the REST
  7. API, unless otherwise specified.
  8. * <<multi-index>>
  9. * <<date-math-index-names>>
  10. * <<common-options>>
  11. --
  12. [[multi-index]]
  13. == Multiple Indices
  14. Most APIs that refer to an `index` parameter support execution across multiple indices,
  15. using simple `test1,test2,test3` notation (or `_all` for all indices). It also
  16. support wildcards, for example: `test*`, and the ability to "add" (`+`)
  17. and "remove" (`-`), for example: `+test*,-test3`.
  18. All multi indices API support the following url query string parameters:
  19. `ignore_unavailable`::
  20. Controls whether to ignore if any specified indices are unavailable, this
  21. includes indices that don't exist or closed indices. Either `true` or `false`
  22. can be specified.
  23. `allow_no_indices`::
  24. Controls whether to fail if a wildcard indices expressions results into no
  25. concrete indices. Either `true` or `false` can be specified. For example if
  26. the wildcard expression `foo*` is specified and no indices are available that
  27. start with `foo` then depending on this setting the request will fail. This
  28. setting is also applicable when `_all`, `*` or no index has been specified. This
  29. settings also applies for aliases, in case an alias points to a closed index.
  30. `expand_wildcards`::
  31. Controls to what kind of concrete indices wildcard indices expression expand
  32. to. If `open` is specified then the wildcard expression is expanded to only
  33. open indices and if `closed` is specified then the wildcard expression is
  34. expanded only to closed indices. Also both values (`open,closed`) can be
  35. specified to expand to all indices.
  36. If `none` is specified then wildcard expansion will be disabled and if `all`
  37. is specified, wildcard expressions will expand to all indices (this is equivalent
  38. to specifying `open,closed`).
  39. The defaults settings for the above parameters depend on the api being used.
  40. NOTE: Single index APIs such as the <<docs>> and the
  41. <<indices-aliases,single-index `alias` APIs>> do not support multiple indices.
  42. [[date-math-index-names]]
  43. == Date math support in index names
  44. Date math index name resolution enables you to search a range of time-series indices, rather
  45. than searching all of your time-series indices and filtering the results or maintaining aliases.
  46. Limiting the number of indices that are searched reduces the load on the cluster and improves
  47. execution performance. For example, if you are searching for errors in your
  48. daily logs, you can use a date math name template to restrict the search to the past
  49. two days.
  50. Almost all APIs that have an `index` parameter, support date math in the `index` parameter
  51. value.
  52. A date math index name takes the following form:
  53. [source,txt]
  54. ----------------------------------------------------------------------
  55. <static_name{date_math_expr{date_format|time_zone}}>
  56. ----------------------------------------------------------------------
  57. Where:
  58. [horizontal]
  59. `static_name`:: is the static text part of the name
  60. `date_math_expr`:: is a dynamic date math expression that computes the date dynamically
  61. `date_format`:: is the optional format in which the computed date should be rendered. Defaults to `YYYY.MM.dd`.
  62. `time_zone`:: is the optional time zone . Defaults to `utc`.
  63. You must enclose date math index name expressions within angle brackets. For example:
  64. [source,js]
  65. ----------------------------------------------------------------------
  66. curl -XGET 'localhost:9200/<logstash-{now%2Fd-2d}>/_search' {
  67. "query" : {
  68. ...
  69. }
  70. }
  71. ----------------------------------------------------------------------
  72. NOTE: The `/` used for date rounding must be url encoded as `%2F` in any url.
  73. The following example shows different forms of date math index names and the final index names
  74. they resolve to given the current time is 22rd March 2024 noon utc.
  75. [options="header"]
  76. |======
  77. | Expression |Resolves to
  78. | `<logstash-{now/d}>` | `logstash-2024.03.22`
  79. | `<logstash-{now/M}>` | `logstash-2024.03.01`
  80. | `<logstash-{now/M{YYYY.MM}}>` | `logstash-2024.03`
  81. | `<logstash-{now/M-1M{YYYY.MM}}>` | `logstash-2024.02`
  82. | `<logstash-{now/d{YYYY.MM.dd\|+12:00}}>` | `logstash-2024.03.23`
  83. |======
  84. To use the characters `{` and `}` in the static part of an index name template, escape them
  85. with a backslash `\`, for example:
  86. * `<elastic\\{ON\\}-{now/M}>` resolves to `elastic{ON}-2024.03.01`
  87. The following example shows a search request that searches the Logstash indices for the past
  88. three days, assuming the indices use the default Logstash index name format,
  89. `logstash-YYYY.MM.dd`.
  90. [source,js]
  91. ----------------------------------------------------------------------
  92. curl -XGET 'localhost:9200/<logstash-{now%2Fd-2d}>,<logstash-{now%2Fd-1d}>,<logstash-{now%2Fd}>/_search' {
  93. "query" : {
  94. ...
  95. }
  96. }
  97. ----------------------------------------------------------------------
  98. [[common-options]]
  99. == Common options
  100. The following options can be applied to all of the REST APIs.
  101. [float]
  102. === Pretty Results
  103. When appending `?pretty=true` to any request made, the JSON returned
  104. will be pretty formatted (use it for debugging only!). Another option is
  105. to set `?format=yaml` which will cause the result to be returned in the
  106. (sometimes) more readable yaml format.
  107. [float]
  108. === Human readable output
  109. Statistics are returned in a format suitable for humans
  110. (eg `"exists_time": "1h"` or `"size": "1kb"`) and for computers
  111. (eg `"exists_time_in_millis": 3600000` or `"size_in_bytes": 1024`).
  112. The human readable values can be turned off by adding `?human=false`
  113. to the query string. This makes sense when the stats results are
  114. being consumed by a monitoring tool, rather than intended for human
  115. consumption. The default for the `human` flag is
  116. `false`.
  117. [[date-math]]
  118. [float]
  119. === Date Math
  120. Most parameters which accept a formatted date value -- such as `gt` and `lt`
  121. in <<query-dsl-range-query,range queries>> `range` queries, or `from` and `to`
  122. in <<search-aggregations-bucket-daterange-aggregation,`daterange`
  123. aggregations>> -- understand date maths.
  124. The expression starts with an anchor date, which can either be `now`, or a
  125. date string ending with `||`. This anchor date can optionally be followed by
  126. one or more maths expressions:
  127. * `+1h` - add one hour
  128. * `-1d` - subtract one day
  129. * `/d` - round down to the nearest day
  130. The supported <<time-units,time units>> are: `y` (year), `M` (month), `w` (week),
  131. `d` (day), `h` (hour), `m` (minute), and `s` (second).
  132. Some examples are:
  133. [horizontal]
  134. `now+1h`:: The current time plus one hour, with ms resolution.
  135. `now+1h+1m`:: The current time plus one hour plus one minute, with ms resolution.
  136. `now+1h/d`:: The current time plus one hour, rounded down to the nearest day.
  137. `2015-01-01||+1M/d`:: `2015-01-01` plus one month, rounded down to the nearest day.
  138. [float]
  139. === Response Filtering
  140. All REST APIs accept a `filter_path` parameter that can be used to reduce
  141. the response returned by elasticsearch. This parameter takes a comma
  142. separated list of filters expressed with the dot notation:
  143. [source,sh]
  144. --------------------------------------------------
  145. curl -XGET 'localhost:9200/_search?pretty&filter_path=took,hits.hits._id,hits.hits._score'
  146. {
  147. "took" : 3,
  148. "hits" : {
  149. "hits" : [
  150. {
  151. "_id" : "3640",
  152. "_score" : 1.0
  153. },
  154. {
  155. "_id" : "3642",
  156. "_score" : 1.0
  157. }
  158. ]
  159. }
  160. }
  161. --------------------------------------------------
  162. It also supports the `*` wildcard character to match any field or part
  163. of a field's name:
  164. [source,sh]
  165. --------------------------------------------------
  166. curl -XGET 'localhost:9200/_nodes/stats?filter_path=nodes.*.ho*'
  167. {
  168. "nodes" : {
  169. "lvJHed8uQQu4brS-SXKsNA" : {
  170. "host" : "portable"
  171. }
  172. }
  173. }
  174. --------------------------------------------------
  175. And the `**` wildcard can be used to include fields without knowing the
  176. exact path of the field. For example, we can return the Lucene version
  177. of every segment with this request:
  178. [source,sh]
  179. --------------------------------------------------
  180. curl 'localhost:9200/_segments?pretty&filter_path=indices.**.version'
  181. {
  182. "indices" : {
  183. "movies" : {
  184. "shards" : {
  185. "0" : [ {
  186. "segments" : {
  187. "_0" : {
  188. "version" : "5.2.0"
  189. }
  190. }
  191. } ],
  192. "2" : [ {
  193. "segments" : {
  194. "_0" : {
  195. "version" : "5.2.0"
  196. }
  197. }
  198. } ]
  199. }
  200. },
  201. "books" : {
  202. "shards" : {
  203. "0" : [ {
  204. "segments" : {
  205. "_0" : {
  206. "version" : "5.2.0"
  207. }
  208. }
  209. } ]
  210. }
  211. }
  212. }
  213. }
  214. --------------------------------------------------
  215. Note that elasticsearch sometimes returns directly the raw value of a field,
  216. like the `_source` field. If you want to filter `_source` fields, you should
  217. consider combining the already existing `_source` parameter (see
  218. <<get-source-filtering,Get API>> for more details) with the `filter_path`
  219. parameter like this:
  220. [source,sh]
  221. --------------------------------------------------
  222. curl -XGET 'localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=title'
  223. {
  224. "hits" : {
  225. "hits" : [ {
  226. "_source":{"title":"Book #2"}
  227. }, {
  228. "_source":{"title":"Book #1"}
  229. }, {
  230. "_source":{"title":"Book #3"}
  231. } ]
  232. }
  233. }
  234. --------------------------------------------------
  235. [float]
  236. === Flat Settings
  237. The `flat_settings` flag affects rendering of the lists of settings. When
  238. `flat_settings` flag is `true` settings are returned in a flat format:
  239. [source,js]
  240. --------------------------------------------------
  241. {
  242. "persistent" : { },
  243. "transient" : {
  244. "discovery.zen.minimum_master_nodes" : "1"
  245. }
  246. }
  247. --------------------------------------------------
  248. When the `flat_settings` flag is `false` settings are returned in a more
  249. human readable structured format:
  250. [source,js]
  251. --------------------------------------------------
  252. {
  253. "persistent" : { },
  254. "transient" : {
  255. "discovery" : {
  256. "zen" : {
  257. "minimum_master_nodes" : "1"
  258. }
  259. }
  260. }
  261. }
  262. --------------------------------------------------
  263. By default the `flat_settings` is set to `false`.
  264. [float]
  265. === Parameters
  266. Rest parameters (when using HTTP, map to HTTP URL parameters) follow the
  267. convention of using underscore casing.
  268. [float]
  269. === Boolean Values
  270. All REST APIs parameters (both request parameters and JSON body) support
  271. providing boolean "false" as the values: `false`, `0`, `no` and `off`.
  272. All other values are considered "true". Note, this is not related to
  273. fields within a document indexed treated as boolean fields.
  274. [float]
  275. === Number Values
  276. All REST APIs support providing numbered parameters as `string` on top
  277. of supporting the native JSON number types.
  278. [[time-units]]
  279. [float]
  280. === Time units
  281. Whenever durations need to be specified, eg for a `timeout` parameter, the
  282. duration must specify the unit, like `2d` for 2 days. The supported units
  283. are:
  284. [horizontal]
  285. `y`:: Year
  286. `M`:: Month
  287. `w`:: Week
  288. `d`:: Day
  289. `h`:: Hour
  290. `m`:: Minute
  291. `s`:: Second
  292. `ms`:: Milli-second
  293. `micros`:: Micro-second
  294. `nanos`:: Nano-second
  295. [[byte-units]]
  296. [float]
  297. === Byte size units
  298. Whenever the byte size of data needs to be specified, eg when setting a buffer size
  299. parameter, the value must specify the unit, like `10kb` for 10 kilobytes. The
  300. supported units are:
  301. [horizontal]
  302. `b`:: Bytes
  303. `kb`:: Kilobytes
  304. `mb`:: Megabytes
  305. `gb`:: Gigabytes
  306. `tb`:: Terabytes
  307. `pb`:: Petabytes
  308. [[size-units]]
  309. [float]
  310. === Unit-less quantities
  311. Unit-less quantities means that they don't have a "unit" like "bytes" or "Hertz" or "meter" or "long tonne".
  312. 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
  313. when we mean 87 though. These are the supported multipliers:
  314. [horizontal]
  315. ``:: Single
  316. `k`:: Kilo
  317. `m`:: Mega
  318. `g`:: Giga
  319. `t`:: Tera
  320. `p`:: Peta
  321. [[distance-units]]
  322. [float]
  323. === Distance Units
  324. Wherever distances need to be specified, such as the `distance` parameter in
  325. the <<query-dsl-geo-distance-query>>), the default unit if none is specified is
  326. the meter. Distances can be specified in other units, such as `"1km"` or
  327. `"2mi"` (2 miles).
  328. The full list of units is listed below:
  329. [horizontal]
  330. Mile:: `mi` or `miles`
  331. Yard:: `yd` or `yards`
  332. Feet:: `ft` or `feet`
  333. Inch:: `in` or `inch`
  334. Kilometer:: `km` or `kilometers`
  335. Meter:: `m` or `meters`
  336. Centimeter:: `cm` or `centimeters`
  337. Millimeter:: `mm` or `millimeters`
  338. Nautical mile:: `NM`, `nmi` or `nauticalmiles`
  339. The `precision` parameter in the <<query-dsl-geohash-cell-query>> accepts
  340. distances with the above units, but if no unit is specified, then the
  341. precision is interpreted as the length of the geohash.
  342. [[fuzziness]]
  343. [float]
  344. === Fuzziness
  345. Some queries and APIs support parameters to allow inexact _fuzzy_ matching,
  346. using the `fuzziness` parameter. The `fuzziness` parameter is context
  347. sensitive which means that it depends on the type of the field being queried:
  348. [float]
  349. ==== Numeric, date and IPv4 fields
  350. When querying numeric, date and IPv4 fields, `fuzziness` is interpreted as a
  351. `+/-` margin. It behaves like a <<query-dsl-range-query>> where:
  352. -fuzziness <= field value <= +fuzziness
  353. The `fuzziness` parameter should be set to a numeric value, eg `2` or `2.0`. A
  354. `date` field interprets a long as milliseconds, but also accepts a string
  355. containing a time value -- `"1h"` -- as explained in <<time-units>>. An `ip`
  356. field accepts a long or another IPv4 address (which will be converted into a
  357. long).
  358. [float]
  359. ==== String fields
  360. When querying `string` fields, `fuzziness` is interpreted as a
  361. http://en.wikipedia.org/wiki/Levenshtein_distance[Levenshtein Edit Distance]
  362. -- the number of one character changes that need to be made to one string to
  363. make it the same as another string.
  364. The `fuzziness` parameter can be specified as:
  365. `0`, `1`, `2`::
  366. the maximum allowed Levenshtein Edit Distance (or number of edits)
  367. `AUTO`::
  368. +
  369. --
  370. generates an edit distance based on the length of the term. For lengths:
  371. `0..2`:: must match exactly
  372. `3..5`:: one edit allowed
  373. `>5`:: two edits allowed
  374. `AUTO` should generally be the preferred value for `fuzziness`.
  375. --
  376. [float]
  377. === Result Casing
  378. All REST APIs accept the `case` parameter. When set to `camelCase`, all
  379. field names in the result will be returned in camel casing, otherwise,
  380. underscore casing will be used. Note, this does not apply to the source
  381. document indexed.
  382. [float]
  383. === Request body in query string
  384. For libraries that don't accept a request body for non-POST requests,
  385. you can pass the request body as the `source` query string parameter
  386. instead.
  387. [[url-access-control]]
  388. == URL-based access control
  389. Many users use a proxy with URL-based access control to secure access to
  390. Elasticsearch indices. For <<search-multi-search,multi-search>>,
  391. <<docs-multi-get,multi-get>> and <<docs-bulk,bulk>> requests, the user has
  392. the choice of specifying an index in the URL and on each individual request
  393. within the request body. This can make URL-based access control challenging.
  394. To prevent the user from overriding the index which has been specified in the
  395. URL, add this setting to the `config.yml` file:
  396. rest.action.multi.allow_explicit_index: false
  397. The default value is `true`, but when set to `false`, Elasticsearch will
  398. reject requests that have an explicit index specified in the request body.