| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 | [[api-conventions]]= API Conventions[partintro]--The *elasticsearch* REST APIs are exposed using <<modules-http,JSON over HTTP>>.The conventions listed in this chapter can be applied throughout the RESTAPI, unless otherwise specified.* <<multi-index>>* <<date-math-index-names>>* <<common-options>>--[[multi-index]]== Multiple IndicesMost APIs that refer to an `index` parameter support execution across multiple indices,using simple `test1,test2,test3` notation (or `_all` for all indices). It alsosupport wildcards, for example: `test*`, and the ability to "add" (`+`)and "remove" (`-`), for example: `+test*,-test3`.All multi indices API support the following url query string parameters:`ignore_unavailable`::Controls whether to ignore if any specified indices are unavailable, thisincludes indices that don't exist or closed indices. Either `true` or `false`can be specified.`allow_no_indices`::Controls whether to fail if a wildcard indices expressions results into noconcrete indices. Either `true` or `false` can be specified. For example ifthe wildcard expression `foo*` is specified and no indices are available thatstart with `foo` then depending on this setting the request will fail. Thissetting is also applicable when `_all`, `*` or no index has been specified. Thissettings also applies for aliases, in case an alias points to a closed index.`expand_wildcards`::Controls to what kind of concrete indices wildcard indices expression expandto. If `open` is specified then the wildcard expression is expanded to onlyopen indices and if `closed` is specified then the wildcard expression isexpanded only to closed indices. Also both values (`open,closed`) can bespecified to expand to all indices.If `none` is specified then wildcard expansion will be disabled and if `all`is specified, wildcard expressions will expand to all indices (this is equivalentto specifying `open,closed`).The defaults settings for the above parameters depend on the api being used.NOTE: Single index APIs such as the <<docs>> and the<<indices-aliases,single-index `alias` APIs>> do not support multiple indices.[[date-math-index-names]]== Date math support in index namesDate math index name resolution enables you to search a range of time-series indices, ratherthan searching all of your time-series indices and filtering the results or maintaining aliases.Limiting the number of indices that are searched reduces the load on the cluster and improvesexecution performance. For example, if you are searching for errors in yourdaily logs, you can use a date math name template to restrict the search to the pasttwo days.Almost all APIs that have an `index` parameter, support date math in the `index` parametervalue.A date math index name takes the following form:[source,txt]----------------------------------------------------------------------<static_name{date_math_expr{date_format|time_zone}}>----------------------------------------------------------------------Where:[horizontal]`static_name`:: is the static text part of the name`date_math_expr`:: is a dynamic date math expression that computes the date dynamically`date_format`:: is the optional format in which the computed date should be rendered. Defaults to `YYYY.MM.dd`.`time_zone`:: is the optional time zone . Defaults to `utc`.You must enclose date math index name expressions within angle brackets. For example:[source,js]----------------------------------------------------------------------curl -XGET 'localhost:9200/<logstash-{now%2Fd-2d}>/_search' {  "query" : {    ...  }}----------------------------------------------------------------------NOTE: The `/` used for date rounding must be url encoded as `%2F` in any url.The following example shows different forms of date math index names and the final index namesthey resolve to given the current time is 22rd March 2024 noon utc.[options="header"]|======| Expression                		      |Resolves to| `<logstash-{now/d}>`      		      | `logstash-2024.03.22`| `<logstash-{now/M}>`      		      | `logstash-2024.03.01`| `<logstash-{now/M{YYYY.MM}}>`           | `logstash-2024.03`| `<logstash-{now/M-1M{YYYY.MM}}>`        | `logstash-2024.02`| `<logstash-{now/d{YYYY.MM.dd\|+12:00}}>`  | `logstash-2024.03.23`|======To use the characters `{` and `}` in the static part of an index name template, escape themwith a backslash `\`, for example: * `<elastic\\{ON\\}-{now/M}>` resolves to `elastic{ON}-2024.03.01`The following example shows a search request that searches the Logstash indices for the pastthree days, assuming the indices use the default Logstash index name format,`logstash-YYYY.MM.dd`.[source,js]----------------------------------------------------------------------curl -XGET 'localhost:9200/<logstash-{now%2Fd-2d}>,<logstash-{now%2Fd-1d}>,<logstash-{now%2Fd}>/_search' {  "query" : {    ...  }}----------------------------------------------------------------------[[common-options]]== Common optionsThe following options can be applied to all of the REST APIs.[float]=== Pretty ResultsWhen appending `?pretty=true` to any request made, the JSON returnedwill be pretty formatted (use it for debugging only!). Another option isto set `?format=yaml` which will cause the result to be returned in the(sometimes) more readable yaml format.[float]=== Human readable outputStatistics are returned in a format suitable for humans(eg `"exists_time": "1h"` or `"size": "1kb"`) and for computers(eg `"exists_time_in_millis": 3600000` or `"size_in_bytes": 1024`).The human readable values can be turned off by adding `?human=false`to the query string. This makes sense when the stats results arebeing consumed by a monitoring tool, rather than intended for humanconsumption.  The default for the `human` flag is`false`.[[date-math]][float]=== Date MathMost parameters which accept a formatted date value -- such as `gt` and `lt`in <<query-dsl-range-query,range queries>> `range` queries, or `from` and `to`in <<search-aggregations-bucket-daterange-aggregation,`daterange`aggregations>> -- understand date maths.The expression starts with an anchor date, which can either be `now`, or adate string ending with `||`. This anchor date can optionally be followed byone or more maths expressions:* `+1h` - add one hour* `-1d` - subtract one day* `/d`  - round down to the nearest dayThe supported <<time-units,time units>> are: `y` (year), `M` (month), `w` (week),`d` (day), `h` (hour), `m` (minute), and `s` (second).Some examples are:[horizontal]`now+1h`::              The current time plus one hour, with ms resolution.`now+1h+1m`::           The current time plus one hour plus one minute, with ms resolution.`now+1h/d`::            The current time plus one hour, rounded down to the nearest day.`2015-01-01||+1M/d`::   `2015-01-01` plus one month, rounded down to the nearest day.[float]=== Response FilteringAll REST APIs accept a `filter_path` parameter that can be used to reducethe response returned by elasticsearch. This parameter takes a commaseparated list of filters expressed with the dot notation:[source,sh]--------------------------------------------------curl -XGET 'localhost:9200/_search?pretty&filter_path=took,hits.hits._id,hits.hits._score'{  "took" : 3,  "hits" : {    "hits" : [      {        "_id" : "3640",        "_score" : 1.0      },      {        "_id" : "3642",        "_score" : 1.0      }    ]  }}--------------------------------------------------It also supports the `*` wildcard character to match any field or partof a field's name:[source,sh]--------------------------------------------------curl -XGET 'localhost:9200/_nodes/stats?filter_path=nodes.*.ho*'{  "nodes" : {    "lvJHed8uQQu4brS-SXKsNA" : {      "host" : "portable"    }  }}--------------------------------------------------And the `**` wildcard can be used to include fields without knowing theexact path of the field. For example, we can return the Lucene versionof every segment with this request:[source,sh]--------------------------------------------------curl 'localhost:9200/_segments?pretty&filter_path=indices.**.version'{  "indices" : {    "movies" : {      "shards" : {        "0" : [ {          "segments" : {            "_0" : {              "version" : "5.2.0"            }          }        } ],        "2" : [ {          "segments" : {            "_0" : {              "version" : "5.2.0"            }          }        } ]      }    },    "books" : {      "shards" : {        "0" : [ {          "segments" : {            "_0" : {              "version" : "5.2.0"            }          }        } ]      }    }  }}--------------------------------------------------Note that elasticsearch sometimes returns directly the raw value of a field,like the `_source` field. If you want to filter `_source` fields, you shouldconsider combining the already existing `_source` parameter (see<<get-source-filtering,Get API>> for more details) with the `filter_path`parameter like this:[source,sh]--------------------------------------------------curl -XGET 'localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=title'{  "hits" : {    "hits" : [ {      "_source":{"title":"Book #2"}    }, {      "_source":{"title":"Book #1"}    }, {      "_source":{"title":"Book #3"}    } ]  }}--------------------------------------------------[float]=== Flat SettingsThe `flat_settings` flag affects rendering of the lists of settings. When`flat_settings` flag is `true` settings are returned in a flat format:[source,js]--------------------------------------------------{  "persistent" : { },  "transient" : {    "discovery.zen.minimum_master_nodes" : "1"  }}--------------------------------------------------When the `flat_settings` flag is `false` settings are returned in a morehuman readable structured format:[source,js]--------------------------------------------------{  "persistent" : { },  "transient" : {    "discovery" : {      "zen" : {        "minimum_master_nodes" : "1"      }    }  }}--------------------------------------------------By default the `flat_settings` is set to `false`.[float]=== ParametersRest parameters (when using HTTP, map to HTTP URL parameters) follow theconvention of using underscore casing.[float]=== Boolean ValuesAll REST APIs parameters (both request parameters and JSON body) supportproviding boolean "false" as the values: `false`, `0`, `no` and `off`.All other values are considered "true". Note, this is not related tofields within a document indexed treated as boolean fields.[float]=== Number ValuesAll REST APIs support providing numbered parameters as `string` on topof supporting the native JSON number types.[[time-units]][float]=== Time unitsWhenever durations need to be specified, eg for a `timeout` parameter, theduration must specify the unit, like `2d` for 2 days.  The supported unitsare:[horizontal]`y`::   Year`M`::   Month`w`::   Week`d`::   Day`h`::   Hour`m`::   Minute`s`::   Second`ms`::  Milli-second[[size-units]][float]=== Data size unitsWhenever the size of data needs to be specified, eg when setting a buffer sizeparameter, the value must specify the unit, like `10kb` for 10 kilobytes.  Thesupported units are:[horizontal]`b`::   Bytes`kb`::  Kilobytes`mb`::  Megabytes`gb`::  Gigabytes`tb`::  Terabytes`pb`::  Petabytes[[distance-units]][float]=== Distance UnitsWherever distances need to be specified, such as the `distance` parameter inthe <<query-dsl-geo-distance-query>>), the default unit if none is specified isthe meter. Distances can be specified in other units, such as `"1km"` or`"2mi"` (2 miles).The full list of units is listed below:[horizontal]Mile::          `mi` or `miles`Yard::          `yd` or `yards`Feet::          `ft` or `feet`Inch::          `in` or `inch`Kilometer::     `km` or `kilometers`Meter::         `m` or `meters`Centimeter::    `cm` or `centimeters`Millimeter::    `mm` or `millimeters`Nautical mile:: `NM`, `nmi` or `nauticalmiles`The `precision` parameter in the <<query-dsl-geohash-cell-query>> acceptsdistances with the above units, but if no unit is specified, then theprecision is interpreted as the length of the geohash.[[fuzziness]][float]=== FuzzinessSome queries and APIs support parameters to allow inexact _fuzzy_ matching,using the `fuzziness` parameter. The `fuzziness` parameter is contextsensitive which means that it depends on the type of the field being queried:[float]==== Numeric, date and IPv4 fieldsWhen querying numeric, date and IPv4 fields, `fuzziness` is interpreted as a`+/-` margin. It behaves like a <<query-dsl-range-query>> where:    -fuzziness <= field value <= +fuzzinessThe `fuzziness` parameter should be set to a numeric value, eg `2` or `2.0`. A`date` field interprets a long as milliseconds, but also accepts a stringcontaining a time value -- `"1h"` -- as explained in <<time-units>>. An `ip`field accepts a long or another IPv4 address (which will be converted into along).[float]==== String fieldsWhen querying `string` fields, `fuzziness` is interpreted as ahttp://en.wikipedia.org/wiki/Levenshtein_distance[Levenshtein Edit Distance]-- the number of one character changes that need to be made to one string tomake it the same as another string.The `fuzziness` parameter can be specified as:`0`, `1`, `2`::the maximum allowed Levenshtein Edit Distance (or number of edits)`AUTO`::+--generates an edit distance based on the length of the term. For lengths:`0..2`:: must match exactly`3..5`:: one edit allowed`>5`:: two edits allowed`AUTO` should generally be the preferred value for `fuzziness`.--[float]=== Result CasingAll REST APIs accept the `case` parameter. When set to `camelCase`, allfield names in the result will be returned in camel casing, otherwise,underscore casing will be used. Note, this does not apply to the sourcedocument indexed.[float]=== Request body in query stringFor libraries that don't accept a request body for non-POST requests,you can pass the request body as the `source` query string parameterinstead.[[url-access-control]]== URL-based access controlMany users use a proxy with URL-based access control to secure access toElasticsearch indices. For <<search-multi-search,multi-search>>,<<docs-multi-get,multi-get>> and <<docs-bulk,bulk>> requests, the user hasthe choice of specifying an index in the URL and on each individual requestwithin the request body. This can make URL-based access control challenging.To prevent the user from overriding the index which has been specified in theURL, add this setting to the `config.yml` file:    rest.action.multi.allow_explicit_index: falseThe default value is `true`, but when set to `false`, Elasticsearch willreject requests that have an explicit index specified in the request body.
 |