alias.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. [chapter]
  2. [[aliases]]
  3. = Aliases
  4. An alias is a secondary name for a group of data streams or indices. Most {es}
  5. APIs accept an alias in place of a data stream or index name.
  6. You can change the data streams or indices of an alias at any time. If you use
  7. aliases in your application's {es} requests, you can reindex data with no
  8. downtime or changes to your app's code.
  9. [discrete]
  10. [[alias-types]]
  11. === Alias types
  12. There are two types of aliases:
  13. * A **data stream alias** points to one or more data streams.
  14. * An **index alias** points to one or more indices.
  15. An alias cannot point to both data streams and indices. You also cannot add a
  16. data stream's backing index to an index alias.
  17. [discrete]
  18. [[add-alias]]
  19. === Add an alias
  20. To add an existing data stream or index to an alias, use the
  21. <<indices-aliases,aliases API>>'s `add` action. If the alias doesn't exist, the
  22. request creates it.
  23. [source,console]
  24. ----
  25. POST _aliases
  26. {
  27. "actions": [
  28. {
  29. "add": {
  30. "index": "logs-nginx.access-prod",
  31. "alias": "logs"
  32. }
  33. }
  34. ]
  35. }
  36. ----
  37. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\n/]
  38. The API's `index` and `indices` parameters support wildcards (`*`). Wildcard
  39. patterns that match both data streams and indices return an error.
  40. [source,console]
  41. ----
  42. POST _aliases
  43. {
  44. "actions": [
  45. {
  46. "add": {
  47. "index": "logs-*",
  48. "alias": "logs"
  49. }
  50. }
  51. ]
  52. }
  53. ----
  54. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\n/]
  55. [discrete]
  56. [[remove-alias]]
  57. === Remove an alias
  58. To remove an alias, use the aliases API's `remove` action.
  59. [source,console]
  60. ----
  61. POST _aliases
  62. {
  63. "actions": [
  64. {
  65. "remove": {
  66. "index": "logs-nginx.access-prod",
  67. "alias": "logs"
  68. }
  69. }
  70. ]
  71. }
  72. ----
  73. // TEST[continued]
  74. [discrete]
  75. [[multiple-actions]]
  76. === Multiple actions
  77. You can use the aliases API to perform multiple actions in a single atomic
  78. operation.
  79. // tag::alias-multiple-actions-example[]
  80. For example, the `logs` alias points to a single data stream. The following
  81. request swaps the stream for the alias. During this swap, the `logs` alias has
  82. no downtime and never points to both streams at the same time.
  83. [source,console]
  84. ----
  85. POST _aliases
  86. {
  87. "actions": [
  88. {
  89. "remove": {
  90. "index": "logs-nginx.access-prod",
  91. "alias": "logs"
  92. }
  93. },
  94. {
  95. "add": {
  96. "index": "logs-my_app-default",
  97. "alias": "logs"
  98. }
  99. }
  100. ]
  101. }
  102. ----
  103. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT _data_stream\/logs-my_app-default\n/]
  104. // end::alias-multiple-actions-example[]
  105. [discrete]
  106. [[add-alias-at-creation]]
  107. === Add an alias at index creation
  108. You can also use a <<indices-component-template,component>> or
  109. <<indices-put-template,index template>> to add index or data stream aliases
  110. when they are created.
  111. [source,console]
  112. ----
  113. # Component template with index aliases
  114. PUT _component_template/my-aliases
  115. {
  116. "template": {
  117. "aliases": {
  118. "my-alias": {}
  119. }
  120. }
  121. }
  122. # Index template with index aliases
  123. PUT _index_template/my-index-template
  124. {
  125. "index_patterns": [
  126. "my-index-*"
  127. ],
  128. "composed_of": [
  129. "my-aliases",
  130. "my-mappings",
  131. "my-settings"
  132. ],
  133. "template": {
  134. "aliases": {
  135. "yet-another-alias": {}
  136. }
  137. }
  138. }
  139. ----
  140. // TEST[s/,\n "my-mappings",\n "my-settings"//]
  141. // TEST[teardown:data_stream_cleanup]
  142. You can also specify index aliases in <<indices-create-index,create index API>>
  143. requests.
  144. [source,console]
  145. ----
  146. # PUT <my-index-{now/d}-000001>
  147. PUT %3Cmy-index-%7Bnow%2Fd%7D-000001%3E
  148. {
  149. "aliases": {
  150. "my-alias": {}
  151. }
  152. }
  153. ----
  154. [discrete]
  155. [[view-aliases]]
  156. === View aliases
  157. To get a list of your cluster's aliases, use the <<indices-get-alias,get alias
  158. API>> with no argument.
  159. [source,console]
  160. ----
  161. GET _alias
  162. ----
  163. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT logs-nginx.access-prod\/_alias\/logs\n/]
  164. Specify a data stream or index before `_alias` to view its aliases.
  165. [source,console]
  166. ----
  167. GET my-data-stream/_alias
  168. ----
  169. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT logs-nginx.access-prod\/_alias\/logs\n/]
  170. // TEST[s/my-data-stream/logs-nginx.access-prod/]
  171. Specify an alias after `_alias` to view its data streams or indices.
  172. [source,console]
  173. ----
  174. GET _alias/logs
  175. ----
  176. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT logs-nginx.access-prod\/_alias\/logs\n/]
  177. [discrete]
  178. [[write-index]]
  179. === Write index
  180. You can use `is_write_index` to specify a write index or data stream for an
  181. alias. {es} routes any write requests for the alias to this index or data
  182. stream.
  183. [source,console]
  184. ----
  185. POST _aliases
  186. {
  187. "actions": [
  188. {
  189. "add": {
  190. "index": "logs-nginx.access-prod",
  191. "alias": "logs"
  192. }
  193. },
  194. {
  195. "add": {
  196. "index": "logs-my_app-default",
  197. "alias": "logs",
  198. "is_write_index": true
  199. }
  200. }
  201. ]
  202. }
  203. ----
  204. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT _data_stream\/logs-my_app-default\n/]
  205. include::{es-repo-dir}/indices/aliases.asciidoc[tag=write-index-defaults]
  206. TIP: We recommend using data streams to store append-only time series data. If
  207. you frequently update or delete existing time series data, use an index alias
  208. with a write index instead. See
  209. <<manage-time-series-data-without-data-streams>>.
  210. [discrete]
  211. [[filter-alias]]
  212. === Filter an alias
  213. The `filter` option uses <<query-dsl,Query DSL>> to limit the documents an alias
  214. can access.
  215. [source,console]
  216. ----
  217. POST _aliases
  218. {
  219. "actions": [
  220. {
  221. "add": {
  222. "index": "my-index-2099.05.06-000001",
  223. "alias": "my-alias",
  224. "filter": {
  225. "bool": {
  226. "filter": [
  227. {
  228. "range": {
  229. "@timestamp": {
  230. "gte": "now-1d/d",
  231. "lt": "now/d"
  232. }
  233. }
  234. },
  235. {
  236. "term": {
  237. "user.id": "kimchy"
  238. }
  239. }
  240. ]
  241. }
  242. }
  243. }
  244. }
  245. ]
  246. }
  247. ----
  248. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]
  249. [discrete]
  250. [[alias-routing]]
  251. === Routing
  252. Use the `routing` option to <<mapping-routing-field,route>> requests for an
  253. alias to a specific shard. This lets you take advantage of
  254. <<shard-request-cache,shard caches>> to speed up searches. Data stream aliases
  255. do not support routing options.
  256. [source,console]
  257. ----
  258. POST _aliases
  259. {
  260. "actions": [
  261. {
  262. "add": {
  263. "index": "my-index-2099.05.06-000001",
  264. "alias": "my-alias",
  265. "routing": "1"
  266. }
  267. }
  268. ]
  269. }
  270. ----
  271. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]
  272. Use `index_routing` and `search_routing` to specify different routing values for
  273. indexing and search. If specified, these options overwrite the `routing` value
  274. for their respective operations.
  275. [source,console]
  276. ----
  277. POST _aliases
  278. {
  279. "actions": [
  280. {
  281. "add": {
  282. "index": "my-index-2099.05.06-000001",
  283. "alias": "my-alias",
  284. "search_routing": "1",
  285. "index_routing": "2"
  286. }
  287. }
  288. ]
  289. }
  290. ----
  291. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]