api-conventions.asciidoc 18 KB

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