alias.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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
  180. this index or data 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. If an alias points to multiple indices or data streams and `is_write_index`
  204. isn't set, the alias rejects write requests. If an index alias points to one
  205. index and `is_write_index` isn't set, the index automatically acts as the write
  206. index. Data stream aliases don't automatically set a write data stream, even if
  207. the alias points to one data stream.
  208. TIP: We recommend using data streams to store append-only time series data. If
  209. you frequently update or delete existing time series data, use an index alias
  210. with a write index instead. See
  211. <<manage-time-series-data-without-data-streams>>.
  212. [discrete]
  213. [[filter-alias]]
  214. === Filter an alias
  215. The `filter` option uses <<query-dsl,Query DSL>> to limit the documents an alias
  216. can access. Data stream aliases do not support `filter`.
  217. [source,console]
  218. ----
  219. POST _aliases
  220. {
  221. "actions": [
  222. {
  223. "add": {
  224. "index": "my-index-2099.05.06-000001",
  225. "alias": "my-alias",
  226. "filter": {
  227. "bool": {
  228. "filter": [
  229. {
  230. "range": {
  231. "@timestamp": {
  232. "gte": "now-1d/d",
  233. "lt": "now/d"
  234. }
  235. }
  236. },
  237. {
  238. "term": {
  239. "user.id": "kimchy"
  240. }
  241. }
  242. ]
  243. }
  244. }
  245. }
  246. }
  247. ]
  248. }
  249. ----
  250. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]
  251. [discrete]
  252. [[alias-routing]]
  253. === Routing
  254. Use the `routing` option to <<mapping-routing-field,route>> requests for an
  255. alias to a specific shard. This lets you take advantage of
  256. <<shard-request-cache,shard caches>> to speed up searches. Data stream aliases
  257. do not support routing options.
  258. [source,console]
  259. ----
  260. POST _aliases
  261. {
  262. "actions": [
  263. {
  264. "add": {
  265. "index": "my-index-2099.05.06-000001",
  266. "alias": "my-alias",
  267. "routing": "1"
  268. }
  269. }
  270. ]
  271. }
  272. ----
  273. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]
  274. Use `index_routing` and `search_routing` to specify different routing values for
  275. indexing and search. If specified, these options overwrite the `routing` value
  276. for their respective operations.
  277. [source,console]
  278. ----
  279. POST _aliases
  280. {
  281. "actions": [
  282. {
  283. "add": {
  284. "index": "my-index-2099.05.06-000001",
  285. "alias": "my-alias",
  286. "search_routing": "1",
  287. "index_routing": "2"
  288. }
  289. }
  290. ]
  291. }
  292. ----
  293. // TEST[s/^/PUT my-index-2099.05.06-000001\n/]