api-conventions.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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*` or `*test` or `te*t` or `*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. GET /<logstash-{now%2Fd}>/_search
  67. {
  68. "query" : {
  69. "match": {
  70. "test": "data"
  71. }
  72. }
  73. }
  74. ----------------------------------------------------------------------
  75. // CONSOLE
  76. // TEST[s/^/PUT logstash-2016.09.20\n/]
  77. // TEST[s/\{now/{2016.09.20||/]
  78. NOTE: The `/` used for date rounding must be url encoded as `%2F` in any url.
  79. The following example shows different forms of date math index names and the final index names
  80. they resolve to given the current time is 22rd March 2024 noon utc.
  81. [options="header"]
  82. |======
  83. | Expression |Resolves to
  84. | `<logstash-{now/d}>` | `logstash-2024.03.22`
  85. | `<logstash-{now/M}>` | `logstash-2024.03.01`
  86. | `<logstash-{now/M{YYYY.MM}}>` | `logstash-2024.03`
  87. | `<logstash-{now/M-1M{YYYY.MM}}>` | `logstash-2024.02`
  88. | `<logstash-{now/d{YYYY.MM.dd\|+12:00}}>` | `logstash-2024.03.23`
  89. |======
  90. To use the characters `{` and `}` in the static part of an index name template, escape them
  91. with a backslash `\`, for example:
  92. * `<elastic\\{ON\\}-{now/M}>` resolves to `elastic{ON}-2024.03.01`
  93. The following example shows a search request that searches the Logstash indices for the past
  94. three days, assuming the indices use the default Logstash index name format,
  95. `logstash-YYYY.MM.dd`.
  96. [source,js]
  97. ----------------------------------------------------------------------
  98. GET /<logstash-{now%2Fd-2d}>,<logstash-{now%2Fd-1d}>,<logstash-{now%2Fd}>/_search
  99. {
  100. "query" : {
  101. "match": {
  102. "test": "data"
  103. }
  104. }
  105. }
  106. ----------------------------------------------------------------------
  107. // CONSOLE
  108. // TEST[s/^/PUT logstash-2016.09.20\nPUT logstash-2016.09.19\nPUT logstash-2016.09.18\n/]
  109. // TEST[s/\{now/{2016.09.20||/]
  110. [[common-options]]
  111. == Common options
  112. The following options can be applied to all of the REST APIs.
  113. [float]
  114. === Pretty Results
  115. When appending `?pretty=true` to any request made, the JSON returned
  116. will be pretty formatted (use it for debugging only!). Another option is
  117. to set `?format=yaml` which will cause the result to be returned in the
  118. (sometimes) more readable yaml format.
  119. [float]
  120. === Human readable output
  121. Statistics are returned in a format suitable for humans
  122. (eg `"exists_time": "1h"` or `"size": "1kb"`) and for computers
  123. (eg `"exists_time_in_millis": 3600000` or `"size_in_bytes": 1024`).
  124. The human readable values can be turned off by adding `?human=false`
  125. to the query string. This makes sense when the stats results are
  126. being consumed by a monitoring tool, rather than intended for human
  127. consumption. The default for the `human` flag is
  128. `false`.
  129. [[date-math]]
  130. [float]
  131. === Date Math
  132. Most parameters which accept a formatted date value -- such as `gt` and `lt`
  133. in <<query-dsl-range-query,range queries>> `range` queries, or `from` and `to`
  134. in <<search-aggregations-bucket-daterange-aggregation,`daterange`
  135. aggregations>> -- understand date maths.
  136. The expression starts with an anchor date, which can either be `now`, or a
  137. date string ending with `||`. This anchor date can optionally be followed by
  138. one or more maths expressions:
  139. * `+1h` - add one hour
  140. * `-1d` - subtract one day
  141. * `/d` - round down to the nearest day
  142. The supported time units differ than those supported by <<time-units, time units>> for durations.
  143. The supported units are:
  144. [horizontal]
  145. `y`:: years
  146. `M`:: months
  147. `w`:: weeks
  148. `d`:: days
  149. `h`:: hours
  150. `H`:: hours
  151. `m`:: minutes
  152. `s`:: seconds
  153. Some examples are:
  154. [horizontal]
  155. `now+1h`:: The current time plus one hour, with ms resolution.
  156. `now+1h+1m`:: The current time plus one hour plus one minute, with ms resolution.
  157. `now+1h/d`:: The current time plus one hour, rounded down to the nearest day.
  158. `2015-01-01||+1M/d`:: `2015-01-01` plus one month, rounded down to the nearest day.
  159. [float]
  160. === Response Filtering
  161. All REST APIs accept a `filter_path` parameter that can be used to reduce
  162. the response returned by elasticsearch. This parameter takes a comma
  163. separated list of filters expressed with the dot notation:
  164. [source,js]
  165. --------------------------------------------------
  166. GET /_search?q=elasticsearch&filter_path=took,hits.hits._id,hits.hits._score
  167. --------------------------------------------------
  168. // CONSOLE
  169. // TEST[setup:twitter]
  170. Responds:
  171. [source,js]
  172. --------------------------------------------------
  173. {
  174. "took" : 3,
  175. "hits" : {
  176. "hits" : [
  177. {
  178. "_id" : "0",
  179. "_score" : 1.6375021
  180. }
  181. ]
  182. }
  183. }
  184. --------------------------------------------------
  185. // TESTRESPONSE[s/"took" : 3/"took" : $body.took/]
  186. It also supports the `*` wildcard character to match any field or part
  187. of a field's name:
  188. [source,sh]
  189. --------------------------------------------------
  190. GET /_cluster/state?filter_path=metadata.indices.*.stat*
  191. --------------------------------------------------
  192. // CONSOLE
  193. // TEST[s/^/PUT twitter\n/]
  194. Responds:
  195. [source,sh]
  196. --------------------------------------------------
  197. {
  198. "metadata" : {
  199. "indices" : {
  200. "twitter": {"state": "open"}
  201. }
  202. }
  203. }
  204. --------------------------------------------------
  205. // TESTRESPONSE
  206. And the `**` wildcard can be used to include fields without knowing the
  207. exact path of the field. For example, we can return the Lucene version
  208. of every segment with this request:
  209. [source,js]
  210. --------------------------------------------------
  211. GET /_cluster/state?filter_path=routing_table.indices.**.state
  212. --------------------------------------------------
  213. // CONSOLE
  214. // TEST[s/^/PUT twitter\n/]
  215. Responds:
  216. [source,js]
  217. --------------------------------------------------
  218. {
  219. "routing_table": {
  220. "indices": {
  221. "twitter": {
  222. "shards": {
  223. "0": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
  224. "1": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
  225. "2": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
  226. "3": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
  227. "4": [{"state": "STARTED"}, {"state": "UNASSIGNED"}]
  228. }
  229. }
  230. }
  231. }
  232. }
  233. --------------------------------------------------
  234. // TESTRESPONSE
  235. It is also possible to exclude one or more fields by prefixing the filter with the char `-`:
  236. [source,js]
  237. --------------------------------------------------
  238. GET /_count?filter_path=-_shards
  239. --------------------------------------------------
  240. // CONSOLE
  241. // TEST[setup:twitter]
  242. Responds:
  243. [source,js]
  244. --------------------------------------------------
  245. {
  246. "count" : 5
  247. }
  248. --------------------------------------------------
  249. // TESTRESPONSE
  250. And for more control, both inclusive and exclusive filters can be combined in the same expression. In
  251. this case, the exclusive filters will be applied first and the result will be filtered again using the
  252. inclusive filters:
  253. [source,js]
  254. --------------------------------------------------
  255. GET /_cluster/state?filter_path=metadata.indices.*.state,-metadata.indices.logstash-*
  256. --------------------------------------------------
  257. // CONSOLE
  258. // TEST[s/^/PUT index-1\nPUT index-2\nPUT index-3\nPUT logstash-2016.01\n/]
  259. Responds:
  260. [source,js]
  261. --------------------------------------------------
  262. {
  263. "metadata" : {
  264. "indices" : {
  265. "index-1" : {"state" : "open"},
  266. "index-2" : {"state" : "open"},
  267. "index-3" : {"state" : "open"}
  268. }
  269. }
  270. }
  271. --------------------------------------------------
  272. // TESTRESPONSE
  273. Note that elasticsearch sometimes returns directly the raw value of a field,
  274. like the `_source` field. If you want to filter `_source` fields, you should
  275. consider combining the already existing `_source` parameter (see
  276. <<get-source-filtering,Get API>> for more details) with the `filter_path`
  277. parameter like this:
  278. [source,js]
  279. --------------------------------------------------
  280. POST /library/book?refresh
  281. {"title": "Book #1", "rating": 200.1}
  282. POST /library/book?refresh
  283. {"title": "Book #2", "rating": 1.7}
  284. POST /library/book?refresh
  285. {"title": "Book #3", "rating": 0.1}
  286. GET /_search?filter_path=hits.hits._source&_source=title&sort=rating:desc
  287. --------------------------------------------------
  288. // CONSOLE
  289. [source,js]
  290. --------------------------------------------------
  291. {
  292. "hits" : {
  293. "hits" : [ {
  294. "_source":{"title":"Book #1"}
  295. }, {
  296. "_source":{"title":"Book #2"}
  297. }, {
  298. "_source":{"title":"Book #3"}
  299. } ]
  300. }
  301. }
  302. --------------------------------------------------
  303. // TESTRESPONSE
  304. [float]
  305. === Flat Settings
  306. The `flat_settings` flag affects rendering of the lists of settings. When
  307. `flat_settings` flag is `true` settings are returned in a flat format:
  308. [source,js]
  309. --------------------------------------------------
  310. GET twitter/_settings?flat_settings=true
  311. --------------------------------------------------
  312. // CONSOLE
  313. // TEST[setup:twitter]
  314. Returns:
  315. [source,js]
  316. --------------------------------------------------
  317. {
  318. "twitter" : {
  319. "settings": {
  320. "index.number_of_replicas": "1",
  321. "index.number_of_shards": "1",
  322. "index.creation_date": "1474389951325",
  323. "index.uuid": "n6gzFZTgS664GUfx0Xrpjw",
  324. "index.version.created": "6000001"
  325. }
  326. }
  327. }
  328. --------------------------------------------------
  329. // TESTRESPONSE[s/1474389951325/$body.twitter.settings.index\\\\.creation_date/]
  330. // TESTRESPONSE[s/n6gzFZTgS664GUfx0Xrpjw/$body.twitter.settings.index\\\\.uuid/]
  331. When the `flat_settings` flag is `false` settings are returned in a more
  332. human readable structured format:
  333. [source,js]
  334. --------------------------------------------------
  335. GET twitter/_settings?flat_settings=false
  336. --------------------------------------------------
  337. // CONSOLE
  338. // TEST[setup:twitter]
  339. Returns:
  340. [source,js]
  341. --------------------------------------------------
  342. {
  343. "twitter" : {
  344. "settings" : {
  345. "index" : {
  346. "number_of_replicas": "1",
  347. "number_of_shards": "1",
  348. "creation_date": "1474389951325",
  349. "uuid": "n6gzFZTgS664GUfx0Xrpjw",
  350. "version": {
  351. "created": "6000001"
  352. }
  353. }
  354. }
  355. }
  356. }
  357. --------------------------------------------------
  358. // TESTRESPONSE[s/1474389951325/$body.twitter.settings.index.creation_date/]
  359. // TESTRESPONSE[s/n6gzFZTgS664GUfx0Xrpjw/$body.twitter.settings.index.uuid/]
  360. By default the `flat_settings` is set to `false`.
  361. [float]
  362. === Parameters
  363. Rest parameters (when using HTTP, map to HTTP URL parameters) follow the
  364. convention of using underscore casing.
  365. [float]
  366. === Boolean Values
  367. All REST APIs parameters (both request parameters and JSON body) support
  368. providing boolean "false" as the values: `false`, `0`, `no` and `off`.
  369. All other values are considered "true". Note, this is not related to
  370. fields within a document indexed treated as boolean fields.
  371. [float]
  372. === Number Values
  373. All REST APIs support providing numbered parameters as `string` on top
  374. of supporting the native JSON number types.
  375. [[time-units]]
  376. [float]
  377. === Time units
  378. Whenever durations need to be specified, e.g. for a `timeout` parameter, the duration must specify
  379. the unit, like `2d` for 2 days. The supported units are:
  380. [horizontal]
  381. `d`:: days
  382. `h`:: hours
  383. `m`:: minutes
  384. `s`:: seconds
  385. `ms`:: milliseconds
  386. `micros`:: microseconds
  387. `nanos`:: nanoseconds
  388. [[byte-units]]
  389. [float]
  390. === Byte size units
  391. Whenever the byte size of data needs to be specified, eg when setting a buffer size
  392. parameter, the value must specify the unit, like `10kb` for 10 kilobytes. The
  393. supported units are:
  394. [horizontal]
  395. `b`:: Bytes
  396. `kb`:: Kilobytes
  397. `mb`:: Megabytes
  398. `gb`:: Gigabytes
  399. `tb`:: Terabytes
  400. `pb`:: Petabytes
  401. [[size-units]]
  402. [float]
  403. === Unit-less quantities
  404. Unit-less quantities means that they don't have a "unit" like "bytes" or "Hertz" or "meter" or "long tonne".
  405. 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
  406. when we mean 87 though. These are the supported multipliers:
  407. [horizontal]
  408. ``:: Single
  409. `k`:: Kilo
  410. `m`:: Mega
  411. `g`:: Giga
  412. `t`:: Tera
  413. `p`:: Peta
  414. [[distance-units]]
  415. [float]
  416. === Distance Units
  417. Wherever distances need to be specified, such as the `distance` parameter in
  418. the <<query-dsl-geo-distance-query>>), the default unit if none is specified is
  419. the meter. Distances can be specified in other units, such as `"1km"` or
  420. `"2mi"` (2 miles).
  421. The full list of units is listed below:
  422. [horizontal]
  423. Mile:: `mi` or `miles`
  424. Yard:: `yd` or `yards`
  425. Feet:: `ft` or `feet`
  426. Inch:: `in` or `inch`
  427. Kilometer:: `km` or `kilometers`
  428. Meter:: `m` or `meters`
  429. Centimeter:: `cm` or `centimeters`
  430. Millimeter:: `mm` or `millimeters`
  431. Nautical mile:: `NM`, `nmi` or `nauticalmiles`
  432. [[fuzziness]]
  433. [float]
  434. === Fuzziness
  435. Some queries and APIs support parameters to allow inexact _fuzzy_ matching,
  436. using the `fuzziness` parameter.
  437. When querying `text` or `keyword` fields, `fuzziness` is interpreted as a
  438. http://en.wikipedia.org/wiki/Levenshtein_distance[Levenshtein Edit Distance]
  439. -- the number of one character changes that need to be made to one string to
  440. make it the same as another string.
  441. The `fuzziness` parameter can be specified as:
  442. `0`, `1`, `2`::
  443. the maximum allowed Levenshtein Edit Distance (or number of edits)
  444. `AUTO`::
  445. +
  446. --
  447. generates an edit distance based on the length of the term. For lengths:
  448. `0..2`:: must match exactly
  449. `3..5`:: one edit allowed
  450. `>5`:: two edits allowed
  451. `AUTO` should generally be the preferred value for `fuzziness`.
  452. --
  453. [float]
  454. === Result Casing
  455. All REST APIs accept the `case` parameter. When set to `camelCase`, all
  456. field names in the result will be returned in camel casing, otherwise,
  457. underscore casing will be used. Note, this does not apply to the source
  458. document indexed.
  459. [float]
  460. === Request body in query string
  461. For libraries that don't accept a request body for non-POST requests,
  462. you can pass the request body as the `source` query string parameter
  463. instead.
  464. [[url-access-control]]
  465. == URL-based access control
  466. Many users use a proxy with URL-based access control to secure access to
  467. Elasticsearch indices. For <<search-multi-search,multi-search>>,
  468. <<docs-multi-get,multi-get>> and <<docs-bulk,bulk>> requests, the user has
  469. the choice of specifying an index in the URL and on each individual request
  470. within the request body. This can make URL-based access control challenging.
  471. To prevent the user from overriding the index which has been specified in the
  472. URL, add this setting to the `config.yml` file:
  473. rest.action.multi.allow_explicit_index: false
  474. The default value is `true`, but when set to `false`, Elasticsearch will
  475. reject requests that have an explicit index specified in the request body.