alias.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. [chapter]
  2. [[alias]]
  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. For example, the `logs` alias points to a single data stream. The following
  80. request swaps the stream for the alias. During this swap, the `logs` alias has
  81. no downtime and never points to both streams at the same time.
  82. [source,console]
  83. ----
  84. POST _aliases
  85. {
  86. "actions": [
  87. {
  88. "remove": {
  89. "index": "logs-nginx.access-prod",
  90. "alias": "logs"
  91. }
  92. },
  93. {
  94. "add": {
  95. "index": "logs-my_app-default",
  96. "alias": "logs"
  97. }
  98. }
  99. ]
  100. }
  101. ----
  102. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT _data_stream\/logs-my_app-default\n/]
  103. [discrete]
  104. [[add-alias-at-creation]]
  105. === Add an alias at index creation
  106. You can also use a <<indices-component-template,component>> or
  107. <<indices-put-template,index template>> to add index aliases at index creation.
  108. You cannot use a component or index template to add a data stream alias.
  109. [source,console]
  110. ----
  111. # Component template with index aliases
  112. PUT _component_template/my-aliases
  113. {
  114. "template": {
  115. "aliases": {
  116. "my-alias": {}
  117. }
  118. }
  119. }
  120. # Index template with index aliases
  121. PUT _index_template/my-index-template
  122. {
  123. "index_patterns": [
  124. "my-index-*"
  125. ],
  126. "composed_of": [
  127. "my-aliases",
  128. "my-mappings",
  129. "my-settings"
  130. ],
  131. "template": {
  132. "aliases": {
  133. "yet-another-alias": {}
  134. }
  135. }
  136. }
  137. ----
  138. // TEST[s/,\n "my-mappings",\n "my-settings"//]
  139. // TEST[teardown:data_stream_cleanup]
  140. You can also specify index aliases in <<indices-create-index,create index API>>
  141. requests.
  142. [source,console]
  143. ----
  144. # PUT <my-index-{now/d}-000001>
  145. PUT %3Cmy-index-%7Bnow%2Fd%7D-000001%3E
  146. {
  147. "aliases": {
  148. "my-alias": {}
  149. }
  150. }
  151. ----
  152. [discrete]
  153. [[view-aliases]]
  154. === View aliases
  155. To get a list of your cluster's aliases, use the <<indices-get-alias,get alias
  156. API>> with no argument.
  157. [source,console]
  158. ----
  159. GET _alias
  160. ----
  161. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT logs-nginx.access-prod\/_alias\/logs\n/]
  162. Specify a data stream or index before `_alias` to view its aliases.
  163. [source,console]
  164. ----
  165. GET my-data-stream/_alias
  166. ----
  167. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT logs-nginx.access-prod\/_alias\/logs\n/]
  168. // TEST[s/my-data-stream/logs-nginx.access-prod/]
  169. Specify an alias after `_alias` to view its data streams or indices.
  170. [source,console]
  171. ----
  172. GET _alias/logs
  173. ----
  174. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT logs-nginx.access-prod\/_alias\/logs\n/]
  175. [discrete]
  176. [[write-index]]
  177. === Write index
  178. If an alias points to multiple indices, you can use `is_write_index` to specify
  179. a write index or data stream. {es} routes any write requests for the alias to this
  180. index or data stream.
  181. [source,console]
  182. ----
  183. POST _aliases
  184. {
  185. "actions": [
  186. {
  187. "add": {
  188. "index": "my-index-2099.05.06-000001",
  189. "alias": "my-alias"
  190. }
  191. },
  192. {
  193. "add": {
  194. "index": "my-index-2099.05.07-000002",
  195. "alias": "my-alias",
  196. "is_write_index": true
  197. }
  198. }
  199. ]
  200. }
  201. ----
  202. // TEST[s/^/PUT my-index-2099.05.06-000001\nPUT my-index-2099.05.07-000002\n/]
  203. TIP: We recommend using data streams to store append-only time series data. If
  204. you frequently update or delete existing time series data, use an index alias
  205. with a write index instead. See
  206. <<manage-time-series-data-without-data-streams>>.
  207. If an alias points to multiple indices with no write index, the alias rejects
  208. write requests. If an alias points to one index and `is_write_index` is not set,
  209. the index automatically acts as the write index.
  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. Data stream aliases do not support `filter`.
  215. [source,console]
  216. ----
  217. POST _aliases
  218. {
  219. "actions": [
  220. {
  221. "add": {
  222. "index": "my-index-2099.05.07-000002",
  223. "alias": "my-alias",
  224. "is_write_index": true,
  225. "filter": {
  226. "range": {
  227. "@timestamp": {
  228. "gte": "now-1d/d",
  229. "lt": "now/d"
  230. }
  231. }
  232. }
  233. }
  234. }
  235. ]
  236. }
  237. ----
  238. // TEST[s/^/PUT my-index-2099.05.07-000002\n/]
  239. [discrete]
  240. [[alias-routing]]
  241. === Routing
  242. Use the `routing` option to <<mapping-routing-field,route>> requests for an
  243. alias to a specific shard. This lets you take advantage of
  244. <<shard-request-cache,shard caches>> to speed up searches. Data stream aliases
  245. do not support routing options.
  246. [source,console]
  247. ----
  248. POST _aliases
  249. {
  250. "actions": [
  251. {
  252. "add": {
  253. "index": "my-index-2099.05.06-000001",
  254. "alias": "my-alias",
  255. "routing": "1"
  256. }
  257. }
  258. ]
  259. }
  260. ----
  261. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]
  262. Use `index_routing` and `search_routing` to specify different routing values for
  263. indexing and search. If specified, these options overwrite the `routing` value
  264. for their respective operations.
  265. [source,console]
  266. ----
  267. POST _aliases
  268. {
  269. "actions": [
  270. {
  271. "add": {
  272. "index": "my-index-2099.05.06-000001",
  273. "alias": "my-alias",
  274. "search_routing": "1",
  275. "index_routing": "2"
  276. }
  277. }
  278. ]
  279. }
  280. ----
  281. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]