alias.asciidoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. 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 or data stream aliases
  108. when they are created.
  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. You can use `is_write_index` to specify a write index or data stream for an
  179. alias. {es} routes any write requests for the alias to this index or data
  180. stream.
  181. [source,console]
  182. ----
  183. POST _aliases
  184. {
  185. "actions": [
  186. {
  187. "add": {
  188. "index": "logs-nginx.access-prod",
  189. "alias": "logs"
  190. }
  191. },
  192. {
  193. "add": {
  194. "index": "logs-my_app-default",
  195. "alias": "logs",
  196. "is_write_index": true
  197. }
  198. }
  199. ]
  200. }
  201. ----
  202. // TEST[s/^/PUT _data_stream\/logs-nginx.access-prod\nPUT _data_stream\/logs-my_app-default\n/]
  203. include::{es-repo-dir}/indices/aliases.asciidoc[tag=write-index-defaults]
  204. TIP: We recommend using data streams to store append-only time series data. If
  205. you frequently update or delete existing time series data, use an index alias
  206. with a write index instead. See
  207. <<manage-time-series-data-without-data-streams>>.
  208. [discrete]
  209. [[filter-alias]]
  210. === Filter an alias
  211. The `filter` option uses <<query-dsl,Query DSL>> to limit the documents an alias
  212. can access.
  213. [source,console]
  214. ----
  215. POST _aliases
  216. {
  217. "actions": [
  218. {
  219. "add": {
  220. "index": "my-index-2099.05.06-000001",
  221. "alias": "my-alias",
  222. "filter": {
  223. "bool": {
  224. "filter": [
  225. {
  226. "range": {
  227. "@timestamp": {
  228. "gte": "now-1d/d",
  229. "lt": "now/d"
  230. }
  231. }
  232. },
  233. {
  234. "term": {
  235. "user.id": "kimchy"
  236. }
  237. }
  238. ]
  239. }
  240. }
  241. }
  242. }
  243. ]
  244. }
  245. ----
  246. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]
  247. [discrete]
  248. [[alias-routing]]
  249. === Routing
  250. Use the `routing` option to <<mapping-routing-field,route>> requests for an
  251. alias to a specific shard. This lets you take advantage of
  252. <<shard-request-cache,shard caches>> to speed up searches. Data stream aliases
  253. do not support routing options.
  254. [source,console]
  255. ----
  256. POST _aliases
  257. {
  258. "actions": [
  259. {
  260. "add": {
  261. "index": "my-index-2099.05.06-000001",
  262. "alias": "my-alias",
  263. "routing": "1"
  264. }
  265. }
  266. ]
  267. }
  268. ----
  269. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]
  270. Use `index_routing` and `search_routing` to specify different routing values for
  271. indexing and search. If specified, these options overwrite the `routing` value
  272. for their respective operations.
  273. [source,console]
  274. ----
  275. POST _aliases
  276. {
  277. "actions": [
  278. {
  279. "add": {
  280. "index": "my-index-2099.05.06-000001",
  281. "alias": "my-alias",
  282. "search_routing": "1",
  283. "index_routing": "2"
  284. }
  285. }
  286. ]
  287. }
  288. ----
  289. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]