api-conventions.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 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. Note that elasticsearch sometimes returns directly the raw value of a field,
  225. like the `_source` field. If you want to filter `_source` fields, you should
  226. consider combining the already existing `_source` parameter (see
  227. <<get-source-filtering,Get API>> for more details) with the `filter_path`
  228. parameter like this:
  229. [source,sh]
  230. --------------------------------------------------
  231. curl -XGET 'localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=title'
  232. {
  233. "hits" : {
  234. "hits" : [ {
  235. "_source":{"title":"Book #2"}
  236. }, {
  237. "_source":{"title":"Book #1"}
  238. }, {
  239. "_source":{"title":"Book #3"}
  240. } ]
  241. }
  242. }
  243. --------------------------------------------------
  244. [float]
  245. === Flat Settings
  246. The `flat_settings` flag affects rendering of the lists of settings. When
  247. `flat_settings` flag is `true` settings are returned in a flat format:
  248. [source,js]
  249. --------------------------------------------------
  250. {
  251. "persistent" : { },
  252. "transient" : {
  253. "discovery.zen.minimum_master_nodes" : "1"
  254. }
  255. }
  256. --------------------------------------------------
  257. When the `flat_settings` flag is `false` settings are returned in a more
  258. human readable structured format:
  259. [source,js]
  260. --------------------------------------------------
  261. {
  262. "persistent" : { },
  263. "transient" : {
  264. "discovery" : {
  265. "zen" : {
  266. "minimum_master_nodes" : "1"
  267. }
  268. }
  269. }
  270. }
  271. --------------------------------------------------
  272. By default the `flat_settings` is set to `false`.
  273. [float]
  274. === Parameters
  275. Rest parameters (when using HTTP, map to HTTP URL parameters) follow the
  276. convention of using underscore casing.
  277. [float]
  278. === Boolean Values
  279. All REST APIs parameters (both request parameters and JSON body) support
  280. providing boolean "false" as the values: `false`, `0`, `no` and `off`.
  281. All other values are considered "true". Note, this is not related to
  282. fields within a document indexed treated as boolean fields.
  283. [float]
  284. === Number Values
  285. All REST APIs support providing numbered parameters as `string` on top
  286. of supporting the native JSON number types.
  287. [[time-units]]
  288. [float]
  289. === Time units
  290. Whenever durations need to be specified, e.g. for a `timeout` parameter, the duration must specify
  291. the unit, like `2d` for 2 days. The supported units are:
  292. [horizontal]
  293. `d`:: days
  294. `h`:: hours
  295. `m`:: minutes
  296. `s`:: seconds
  297. `ms`:: milliseconds
  298. `micros`:: microseconds
  299. `nanos`:: nanoseconds
  300. [[byte-units]]
  301. [float]
  302. === Byte size units
  303. Whenever the byte size of data needs to be specified, eg when setting a buffer size
  304. parameter, the value must specify the unit, like `10kb` for 10 kilobytes. The
  305. supported units are:
  306. [horizontal]
  307. `b`:: Bytes
  308. `kb`:: Kilobytes
  309. `mb`:: Megabytes
  310. `gb`:: Gigabytes
  311. `tb`:: Terabytes
  312. `pb`:: Petabytes
  313. [[size-units]]
  314. [float]
  315. === Unit-less quantities
  316. Unit-less quantities means that they don't have a "unit" like "bytes" or "Hertz" or "meter" or "long tonne".
  317. 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
  318. when we mean 87 though. These are the supported multipliers:
  319. [horizontal]
  320. ``:: Single
  321. `k`:: Kilo
  322. `m`:: Mega
  323. `g`:: Giga
  324. `t`:: Tera
  325. `p`:: Peta
  326. [[distance-units]]
  327. [float]
  328. === Distance Units
  329. Wherever distances need to be specified, such as the `distance` parameter in
  330. the <<query-dsl-geo-distance-query>>), the default unit if none is specified is
  331. the meter. Distances can be specified in other units, such as `"1km"` or
  332. `"2mi"` (2 miles).
  333. The full list of units is listed below:
  334. [horizontal]
  335. Mile:: `mi` or `miles`
  336. Yard:: `yd` or `yards`
  337. Feet:: `ft` or `feet`
  338. Inch:: `in` or `inch`
  339. Kilometer:: `km` or `kilometers`
  340. Meter:: `m` or `meters`
  341. Centimeter:: `cm` or `centimeters`
  342. Millimeter:: `mm` or `millimeters`
  343. Nautical mile:: `NM`, `nmi` or `nauticalmiles`
  344. The `precision` parameter in the <<query-dsl-geohash-cell-query>> accepts
  345. distances with the above units, but if no unit is specified, then the
  346. precision is interpreted as the length of the geohash.
  347. [[fuzziness]]
  348. [float]
  349. === Fuzziness
  350. Some queries and APIs support parameters to allow inexact _fuzzy_ matching,
  351. using the `fuzziness` parameter.
  352. When querying `text` or `keyword` fields, `fuzziness` is interpreted as a
  353. http://en.wikipedia.org/wiki/Levenshtein_distance[Levenshtein Edit Distance]
  354. -- the number of one character changes that need to be made to one string to
  355. make it the same as another string.
  356. The `fuzziness` parameter can be specified as:
  357. `0`, `1`, `2`::
  358. the maximum allowed Levenshtein Edit Distance (or number of edits)
  359. `AUTO`::
  360. +
  361. --
  362. generates an edit distance based on the length of the term. For lengths:
  363. `0..2`:: must match exactly
  364. `3..5`:: one edit allowed
  365. `>5`:: two edits allowed
  366. `AUTO` should generally be the preferred value for `fuzziness`.
  367. --
  368. [float]
  369. === Result Casing
  370. All REST APIs accept the `case` parameter. When set to `camelCase`, all
  371. field names in the result will be returned in camel casing, otherwise,
  372. underscore casing will be used. Note, this does not apply to the source
  373. document indexed.
  374. [float]
  375. === Request body in query string
  376. For libraries that don't accept a request body for non-POST requests,
  377. you can pass the request body as the `source` query string parameter
  378. instead.
  379. [[url-access-control]]
  380. == URL-based access control
  381. Many users use a proxy with URL-based access control to secure access to
  382. Elasticsearch indices. For <<search-multi-search,multi-search>>,
  383. <<docs-multi-get,multi-get>> and <<docs-bulk,bulk>> requests, the user has
  384. the choice of specifying an index in the URL and on each individual request
  385. within the request body. This can make URL-based access control challenging.
  386. To prevent the user from overriding the index which has been specified in the
  387. URL, add this setting to the `config.yml` file:
  388. rest.action.multi.allow_explicit_index: false
  389. The default value is `true`, but when set to `false`, Elasticsearch will
  390. reject requests that have an explicit index specified in the request body.