1
0

api-conventions.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. // TESTRESPONSE[s/1.6375021/$body.hits.hits.0._score/]
  187. It also supports the `*` wildcard character to match any field or part
  188. of a field's name:
  189. [source,sh]
  190. --------------------------------------------------
  191. GET /_cluster/state?filter_path=metadata.indices.*.stat*
  192. --------------------------------------------------
  193. // CONSOLE
  194. // TEST[s/^/PUT twitter\n/]
  195. Responds:
  196. [source,sh]
  197. --------------------------------------------------
  198. {
  199. "metadata" : {
  200. "indices" : {
  201. "twitter": {"state": "open"}
  202. }
  203. }
  204. }
  205. --------------------------------------------------
  206. // TESTRESPONSE
  207. And the `**` wildcard can be used to include fields without knowing the
  208. exact path of the field. For example, we can return the Lucene version
  209. of every segment with this request:
  210. [source,js]
  211. --------------------------------------------------
  212. GET /_cluster/state?filter_path=routing_table.indices.**.state
  213. --------------------------------------------------
  214. // CONSOLE
  215. // TEST[s/^/PUT twitter\n/]
  216. Responds:
  217. [source,js]
  218. --------------------------------------------------
  219. {
  220. "routing_table": {
  221. "indices": {
  222. "twitter": {
  223. "shards": {
  224. "0": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
  225. "1": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
  226. "2": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
  227. "3": [{"state": "STARTED"}, {"state": "UNASSIGNED"}],
  228. "4": [{"state": "STARTED"}, {"state": "UNASSIGNED"}]
  229. }
  230. }
  231. }
  232. }
  233. }
  234. --------------------------------------------------
  235. // TESTRESPONSE
  236. It is also possible to exclude one or more fields by prefixing the filter with the char `-`:
  237. [source,js]
  238. --------------------------------------------------
  239. GET /_count?filter_path=-_shards
  240. --------------------------------------------------
  241. // CONSOLE
  242. // TEST[setup:twitter]
  243. Responds:
  244. [source,js]
  245. --------------------------------------------------
  246. {
  247. "count" : 5
  248. }
  249. --------------------------------------------------
  250. // TESTRESPONSE
  251. And for more control, both inclusive and exclusive filters can be combined in the same expression. In
  252. this case, the exclusive filters will be applied first and the result will be filtered again using the
  253. inclusive filters:
  254. [source,js]
  255. --------------------------------------------------
  256. GET /_cluster/state?filter_path=metadata.indices.*.state,-metadata.indices.logstash-*
  257. --------------------------------------------------
  258. // CONSOLE
  259. // TEST[s/^/PUT index-1\nPUT index-2\nPUT index-3\nPUT logstash-2016.01\n/]
  260. Responds:
  261. [source,js]
  262. --------------------------------------------------
  263. {
  264. "metadata" : {
  265. "indices" : {
  266. "index-1" : {"state" : "open"},
  267. "index-2" : {"state" : "open"},
  268. "index-3" : {"state" : "open"}
  269. }
  270. }
  271. }
  272. --------------------------------------------------
  273. // TESTRESPONSE
  274. Note that elasticsearch sometimes returns directly the raw value of a field,
  275. like the `_source` field. If you want to filter `_source` fields, you should
  276. consider combining the already existing `_source` parameter (see
  277. <<get-source-filtering,Get API>> for more details) with the `filter_path`
  278. parameter like this:
  279. [source,js]
  280. --------------------------------------------------
  281. POST /library/book?refresh
  282. {"title": "Book #1", "rating": 200.1}
  283. POST /library/book?refresh
  284. {"title": "Book #2", "rating": 1.7}
  285. POST /library/book?refresh
  286. {"title": "Book #3", "rating": 0.1}
  287. GET /_search?filter_path=hits.hits._source&_source=title&sort=rating:desc
  288. --------------------------------------------------
  289. // CONSOLE
  290. [source,js]
  291. --------------------------------------------------
  292. {
  293. "hits" : {
  294. "hits" : [ {
  295. "_source":{"title":"Book #1"}
  296. }, {
  297. "_source":{"title":"Book #2"}
  298. }, {
  299. "_source":{"title":"Book #3"}
  300. } ]
  301. }
  302. }
  303. --------------------------------------------------
  304. // TESTRESPONSE
  305. [float]
  306. === Flat Settings
  307. The `flat_settings` flag affects rendering of the lists of settings. When
  308. `flat_settings` flag is `true` settings are returned in a flat format:
  309. [source,js]
  310. --------------------------------------------------
  311. GET twitter/_settings?flat_settings=true
  312. --------------------------------------------------
  313. // CONSOLE
  314. // TEST[setup:twitter]
  315. Returns:
  316. [source,js]
  317. --------------------------------------------------
  318. {
  319. "twitter" : {
  320. "settings": {
  321. "index.number_of_replicas": "1",
  322. "index.number_of_shards": "1",
  323. "index.creation_date": "1474389951325",
  324. "index.uuid": "n6gzFZTgS664GUfx0Xrpjw",
  325. "index.version.created": ...
  326. }
  327. }
  328. }
  329. --------------------------------------------------
  330. // TESTRESPONSE[s/1474389951325/$body.twitter.settings.index\\\\.creation_date/]
  331. // TESTRESPONSE[s/n6gzFZTgS664GUfx0Xrpjw/$body.twitter.settings.index\\\\.uuid/]
  332. // TESTRESPONSE[s/"index.version.created": \.\.\./"index.version.created": $body.twitter.settings.index\\\\.version\\\\.created/]
  333. When the `flat_settings` flag is `false` settings are returned in a more
  334. human readable structured format:
  335. [source,js]
  336. --------------------------------------------------
  337. GET twitter/_settings?flat_settings=false
  338. --------------------------------------------------
  339. // CONSOLE
  340. // TEST[setup:twitter]
  341. Returns:
  342. [source,js]
  343. --------------------------------------------------
  344. {
  345. "twitter" : {
  346. "settings" : {
  347. "index" : {
  348. "number_of_replicas": "1",
  349. "number_of_shards": "1",
  350. "creation_date": "1474389951325",
  351. "uuid": "n6gzFZTgS664GUfx0Xrpjw",
  352. "version": {
  353. "created": ...
  354. }
  355. }
  356. }
  357. }
  358. }
  359. --------------------------------------------------
  360. // TESTRESPONSE[s/1474389951325/$body.twitter.settings.index.creation_date/]
  361. // TESTRESPONSE[s/n6gzFZTgS664GUfx0Xrpjw/$body.twitter.settings.index.uuid/]
  362. // TESTRESPONSE[s/"created": \.\.\./"created": $body.twitter.settings.index.version.created/]
  363. By default the `flat_settings` is set to `false`.
  364. [float]
  365. === Parameters
  366. Rest parameters (when using HTTP, map to HTTP URL parameters) follow the
  367. convention of using underscore casing.
  368. [float]
  369. === Boolean Values
  370. All REST APIs parameters (both request parameters and JSON body) support
  371. providing boolean "false" as the values: `false`, `0`, `no` and `off`.
  372. All other values are considered "true". Note, this is not related to
  373. fields within a document indexed treated as boolean fields.
  374. [float]
  375. === Number Values
  376. All REST APIs support providing numbered parameters as `string` on top
  377. of supporting the native JSON number types.
  378. [[time-units]]
  379. [float]
  380. === Time units
  381. Whenever durations need to be specified, e.g. for a `timeout` parameter, the duration must specify
  382. the unit, like `2d` for 2 days. The supported units are:
  383. [horizontal]
  384. `d`:: days
  385. `h`:: hours
  386. `m`:: minutes
  387. `s`:: seconds
  388. `ms`:: milliseconds
  389. `micros`:: microseconds
  390. `nanos`:: nanoseconds
  391. [[byte-units]]
  392. [float]
  393. === Byte size units
  394. Whenever the byte size of data needs to be specified, eg when setting a buffer size
  395. parameter, the value must specify the unit, like `10kb` for 10 kilobytes. The
  396. supported units are:
  397. [horizontal]
  398. `b`:: Bytes
  399. `kb`:: Kilobytes
  400. `mb`:: Megabytes
  401. `gb`:: Gigabytes
  402. `tb`:: Terabytes
  403. `pb`:: Petabytes
  404. [[size-units]]
  405. [float]
  406. === Unit-less quantities
  407. Unit-less quantities means that they don't have a "unit" like "bytes" or "Hertz" or "meter" or "long tonne".
  408. 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
  409. when we mean 87 though. These are the supported multipliers:
  410. [horizontal]
  411. ``:: Single
  412. `k`:: Kilo
  413. `m`:: Mega
  414. `g`:: Giga
  415. `t`:: Tera
  416. `p`:: Peta
  417. [[distance-units]]
  418. [float]
  419. === Distance Units
  420. Wherever distances need to be specified, such as the `distance` parameter in
  421. the <<query-dsl-geo-distance-query>>), the default unit if none is specified is
  422. the meter. Distances can be specified in other units, such as `"1km"` or
  423. `"2mi"` (2 miles).
  424. The full list of units is listed below:
  425. [horizontal]
  426. Mile:: `mi` or `miles`
  427. Yard:: `yd` or `yards`
  428. Feet:: `ft` or `feet`
  429. Inch:: `in` or `inch`
  430. Kilometer:: `km` or `kilometers`
  431. Meter:: `m` or `meters`
  432. Centimeter:: `cm` or `centimeters`
  433. Millimeter:: `mm` or `millimeters`
  434. Nautical mile:: `NM`, `nmi` or `nauticalmiles`
  435. [[fuzziness]]
  436. [float]
  437. === Fuzziness
  438. Some queries and APIs support parameters to allow inexact _fuzzy_ matching,
  439. using the `fuzziness` parameter.
  440. When querying `text` or `keyword` fields, `fuzziness` is interpreted as a
  441. http://en.wikipedia.org/wiki/Levenshtein_distance[Levenshtein Edit Distance]
  442. -- the number of one character changes that need to be made to one string to
  443. make it the same as another string.
  444. The `fuzziness` parameter can be specified as:
  445. `0`, `1`, `2`::
  446. the maximum allowed Levenshtein Edit Distance (or number of edits)
  447. `AUTO`::
  448. +
  449. --
  450. generates an edit distance based on the length of the term. For lengths:
  451. `0..2`:: must match exactly
  452. `3..5`:: one edit allowed
  453. `>5`:: two edits allowed
  454. `AUTO` should generally be the preferred value for `fuzziness`.
  455. --
  456. [float]
  457. === Result Casing
  458. All REST APIs accept the `case` parameter. When set to `camelCase`, all
  459. field names in the result will be returned in camel casing, otherwise,
  460. underscore casing will be used. Note, this does not apply to the source
  461. document indexed.
  462. [float]
  463. === Request body in query string
  464. For libraries that don't accept a request body for non-POST requests,
  465. you can pass the request body as the `source` query string parameter
  466. instead.
  467. [[url-access-control]]
  468. == URL-based access control
  469. Many users use a proxy with URL-based access control to secure access to
  470. Elasticsearch indices. For <<search-multi-search,multi-search>>,
  471. <<docs-multi-get,multi-get>> and <<docs-bulk,bulk>> requests, the user has
  472. the choice of specifying an index in the URL and on each individual request
  473. within the request body. This can make URL-based access control challenging.
  474. To prevent the user from overriding the index which has been specified in the
  475. URL, add this setting to the `config.yml` file:
  476. rest.action.multi.allow_explicit_index: false
  477. The default value is `true`, but when set to `false`, Elasticsearch will
  478. reject requests that have an explicit index specified in the request body.