api-conventions.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. 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 differ than those supported by <<time-units, time units>> for durations.
  131. The supported units are:
  132. [horizontal]
  133. `y`:: years
  134. `M`:: months
  135. `w`:: weeks
  136. `d`:: days
  137. `h`:: hours
  138. `H`:: hours
  139. `m`:: minutes
  140. `s`:: seconds
  141. Some examples are:
  142. [horizontal]
  143. `now+1h`:: The current time plus one hour, with ms resolution.
  144. `now+1h+1m`:: The current time plus one hour plus one minute, with ms resolution.
  145. `now+1h/d`:: The current time plus one hour, rounded down to the nearest day.
  146. `2015-01-01||+1M/d`:: `2015-01-01` plus one month, rounded down to the nearest day.
  147. [float]
  148. === Response Filtering
  149. All REST APIs accept a `filter_path` parameter that can be used to reduce
  150. the response returned by elasticsearch. This parameter takes a comma
  151. separated list of filters expressed with the dot notation:
  152. [source,sh]
  153. --------------------------------------------------
  154. curl -XGET 'localhost:9200/_search?pretty&filter_path=took,hits.hits._id,hits.hits._score'
  155. {
  156. "took" : 3,
  157. "hits" : {
  158. "hits" : [
  159. {
  160. "_id" : "3640",
  161. "_score" : 1.0
  162. },
  163. {
  164. "_id" : "3642",
  165. "_score" : 1.0
  166. }
  167. ]
  168. }
  169. }
  170. --------------------------------------------------
  171. It also supports the `*` wildcard character to match any field or part
  172. of a field's name:
  173. [source,sh]
  174. --------------------------------------------------
  175. curl -XGET 'localhost:9200/_nodes/stats?filter_path=nodes.*.ho*'
  176. {
  177. "nodes" : {
  178. "lvJHed8uQQu4brS-SXKsNA" : {
  179. "host" : "portable"
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. And the `**` wildcard can be used to include fields without knowing the
  185. exact path of the field. For example, we can return the Lucene version
  186. of every segment with this request:
  187. [source,sh]
  188. --------------------------------------------------
  189. curl 'localhost:9200/_segments?pretty&filter_path=indices.**.version'
  190. {
  191. "indices" : {
  192. "movies" : {
  193. "shards" : {
  194. "0" : [ {
  195. "segments" : {
  196. "_0" : {
  197. "version" : "5.2.0"
  198. }
  199. }
  200. } ],
  201. "2" : [ {
  202. "segments" : {
  203. "_0" : {
  204. "version" : "5.2.0"
  205. }
  206. }
  207. } ]
  208. }
  209. },
  210. "books" : {
  211. "shards" : {
  212. "0" : [ {
  213. "segments" : {
  214. "_0" : {
  215. "version" : "5.2.0"
  216. }
  217. }
  218. } ]
  219. }
  220. }
  221. }
  222. }
  223. --------------------------------------------------
  224. It is also possible to exclude one or more fields by prefixing the filter with the char `-`:
  225. [source,sh]
  226. --------------------------------------------------
  227. curl -XGET 'localhost:9200/_count?filter_path=-_shards'
  228. {
  229. "count" : 1
  230. }
  231. %
  232. --------------------------------------------------
  233. And for more control, both inclusive and exclusive filters can be combined in the same expression. In
  234. this case, the exclusive filters will be applied first and the result will be filtered again using the
  235. inclusive filters:
  236. [source,sh]
  237. --------------------------------------------------
  238. curl -XGET 'localhost:9200/_cluster/state?filter_path=metadata.indices.*.state,-metadata.indices.logs-*'
  239. {
  240. "metadata" : {
  241. "indices" : {
  242. "index-1" : {
  243. "state" : "open"
  244. },
  245. "index-3" : {
  246. "state" : "open"
  247. },
  248. "index-2" : {
  249. "state" : "open"
  250. }
  251. }
  252. }
  253. }%
  254. --------------------------------------------------
  255. Note that elasticsearch sometimes returns directly the raw value of a field,
  256. like the `_source` field. If you want to filter `_source` fields, you should
  257. consider combining the already existing `_source` parameter (see
  258. <<get-source-filtering,Get API>> for more details) with the `filter_path`
  259. parameter like this:
  260. [source,sh]
  261. --------------------------------------------------
  262. curl -XGET 'localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=title'
  263. {
  264. "hits" : {
  265. "hits" : [ {
  266. "_source":{"title":"Book #2"}
  267. }, {
  268. "_source":{"title":"Book #1"}
  269. }, {
  270. "_source":{"title":"Book #3"}
  271. } ]
  272. }
  273. }
  274. --------------------------------------------------
  275. [float]
  276. === Flat Settings
  277. The `flat_settings` flag affects rendering of the lists of settings. When
  278. `flat_settings` flag is `true` settings are returned in a flat format:
  279. [source,js]
  280. --------------------------------------------------
  281. {
  282. "persistent" : { },
  283. "transient" : {
  284. "discovery.zen.minimum_master_nodes" : "1"
  285. }
  286. }
  287. --------------------------------------------------
  288. When the `flat_settings` flag is `false` settings are returned in a more
  289. human readable structured format:
  290. [source,js]
  291. --------------------------------------------------
  292. {
  293. "persistent" : { },
  294. "transient" : {
  295. "discovery" : {
  296. "zen" : {
  297. "minimum_master_nodes" : "1"
  298. }
  299. }
  300. }
  301. }
  302. --------------------------------------------------
  303. By default the `flat_settings` is set to `false`.
  304. [float]
  305. === Parameters
  306. Rest parameters (when using HTTP, map to HTTP URL parameters) follow the
  307. convention of using underscore casing.
  308. [float]
  309. === Boolean Values
  310. All REST APIs parameters (both request parameters and JSON body) support
  311. providing boolean "false" as the values: `false`, `0`, `no` and `off`.
  312. All other values are considered "true". Note, this is not related to
  313. fields within a document indexed treated as boolean fields.
  314. [float]
  315. === Number Values
  316. All REST APIs support providing numbered parameters as `string` on top
  317. of supporting the native JSON number types.
  318. [[time-units]]
  319. [float]
  320. === Time units
  321. Whenever durations need to be specified, e.g. for a `timeout` parameter, the duration must specify
  322. the unit, like `2d` for 2 days. The supported units are:
  323. [horizontal]
  324. `d`:: days
  325. `h`:: hours
  326. `m`:: minutes
  327. `s`:: seconds
  328. `ms`:: milliseconds
  329. `micros`:: microseconds
  330. `nanos`:: nanoseconds
  331. [[byte-units]]
  332. [float]
  333. === Byte size units
  334. Whenever the byte size of data needs to be specified, eg when setting a buffer size
  335. parameter, the value must specify the unit, like `10kb` for 10 kilobytes. The
  336. supported units are:
  337. [horizontal]
  338. `b`:: Bytes
  339. `kb`:: Kilobytes
  340. `mb`:: Megabytes
  341. `gb`:: Gigabytes
  342. `tb`:: Terabytes
  343. `pb`:: Petabytes
  344. [[size-units]]
  345. [float]
  346. === Unit-less quantities
  347. Unit-less quantities means that they don't have a "unit" like "bytes" or "Hertz" or "meter" or "long tonne".
  348. 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
  349. when we mean 87 though. These are the supported multipliers:
  350. [horizontal]
  351. ``:: Single
  352. `k`:: Kilo
  353. `m`:: Mega
  354. `g`:: Giga
  355. `t`:: Tera
  356. `p`:: Peta
  357. [[distance-units]]
  358. [float]
  359. === Distance Units
  360. Wherever distances need to be specified, such as the `distance` parameter in
  361. the <<query-dsl-geo-distance-query>>), the default unit if none is specified is
  362. the meter. Distances can be specified in other units, such as `"1km"` or
  363. `"2mi"` (2 miles).
  364. The full list of units is listed below:
  365. [horizontal]
  366. Mile:: `mi` or `miles`
  367. Yard:: `yd` or `yards`
  368. Feet:: `ft` or `feet`
  369. Inch:: `in` or `inch`
  370. Kilometer:: `km` or `kilometers`
  371. Meter:: `m` or `meters`
  372. Centimeter:: `cm` or `centimeters`
  373. Millimeter:: `mm` or `millimeters`
  374. Nautical mile:: `NM`, `nmi` or `nauticalmiles`
  375. [[fuzziness]]
  376. [float]
  377. === Fuzziness
  378. Some queries and APIs support parameters to allow inexact _fuzzy_ matching,
  379. using the `fuzziness` parameter.
  380. When querying `text` or `keyword` fields, `fuzziness` is interpreted as a
  381. http://en.wikipedia.org/wiki/Levenshtein_distance[Levenshtein Edit Distance]
  382. -- the number of one character changes that need to be made to one string to
  383. make it the same as another string.
  384. The `fuzziness` parameter can be specified as:
  385. `0`, `1`, `2`::
  386. the maximum allowed Levenshtein Edit Distance (or number of edits)
  387. `AUTO`::
  388. +
  389. --
  390. generates an edit distance based on the length of the term. For lengths:
  391. `0..2`:: must match exactly
  392. `3..5`:: one edit allowed
  393. `>5`:: two edits allowed
  394. `AUTO` should generally be the preferred value for `fuzziness`.
  395. --
  396. [float]
  397. === Result Casing
  398. All REST APIs accept the `case` parameter. When set to `camelCase`, all
  399. field names in the result will be returned in camel casing, otherwise,
  400. underscore casing will be used. Note, this does not apply to the source
  401. document indexed.
  402. [float]
  403. === Request body in query string
  404. For libraries that don't accept a request body for non-POST requests,
  405. you can pass the request body as the `source` query string parameter
  406. instead.
  407. [[url-access-control]]
  408. == URL-based access control
  409. Many users use a proxy with URL-based access control to secure access to
  410. Elasticsearch indices. For <<search-multi-search,multi-search>>,
  411. <<docs-multi-get,multi-get>> and <<docs-bulk,bulk>> requests, the user has
  412. the choice of specifying an index in the URL and on each individual request
  413. within the request body. This can make URL-based access control challenging.
  414. To prevent the user from overriding the index which has been specified in the
  415. URL, add this setting to the `config.yml` file:
  416. rest.action.multi.allow_explicit_index: false
  417. The default value is `true`, but when set to `false`, Elasticsearch will
  418. reject requests that have an explicit index specified in the request body.