aliases.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. [[indices-aliases]]
  2. === Update index alias API
  3. ++++
  4. <titleabbrev>Update index alias</titleabbrev>
  5. ++++
  6. Adds or removes index aliases.
  7. include::{es-repo-dir}/glossary.asciidoc[tag=index-alias-desc]
  8. [source,console]
  9. ----
  10. POST /_aliases
  11. {
  12. "actions" : [
  13. { "add" : { "index" : "my-index-000001", "alias" : "alias1" } }
  14. ]
  15. }
  16. ----
  17. // TEST[setup:my_index]
  18. [[indices-aliases-api-request]]
  19. ==== {api-request-title}
  20. `POST /_aliases`
  21. [[indices-aliases-api-desc]]
  22. ==== {api-description-title}
  23. APIs in Elasticsearch accept an index name when working against a
  24. specific index, and several indices when applicable. The index aliases
  25. API allows aliasing an index with a name, with all APIs automatically
  26. converting the alias name to the actual index name. An alias can also be
  27. mapped to more than one index, and when specifying it, the alias will
  28. automatically expand to the aliased indices. An alias can also be
  29. associated with a filter that will automatically be applied when
  30. searching, and routing values. An alias cannot have the same name as an index.
  31. [[indices-aliases-api-query-params]]
  32. ==== {api-query-parms-title}
  33. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  34. [[indices-aliases-api-request-body]]
  35. ==== {api-request-body-title}
  36. `actions`::
  37. +
  38. --
  39. (Required, array of actions)
  40. Set of actions to perform.
  41. Valid actions include:
  42. `add`::
  43. Adds an alias to an index.
  44. `remove`::
  45. Removes an alias from an index.
  46. `remove_index`::
  47. Deletes an index or index alias,
  48. like the <<indices-delete-index,delete index API>>.
  49. You can perform these actions on alias objects.
  50. Valid parameters for alias objects include:
  51. `index`::
  52. (String)
  53. Wildcard expression of index names
  54. used to perform the action.
  55. +
  56. If the `indices` parameter is not specified,
  57. this parameter is required.
  58. +
  59. NOTE: You cannot add <<data-streams,data streams>> to an index alias.
  60. `indices`::
  61. (Array)
  62. Array of index names
  63. used to perform the action.
  64. +
  65. If the `index` parameter is not specified,
  66. this parameter is required.
  67. +
  68. NOTE: You cannot add <<data-streams,data streams>> to an index alias.
  69. `alias`::
  70. (String)
  71. Comma-separated list or wildcard expression of index alias names to
  72. add, remove, or delete.
  73. +
  74. If the `aliases` parameter is not specified,
  75. this parameter is required for the `add` or `remove` action.
  76. `aliases`::
  77. (Array of strings)
  78. Array of index alias names to
  79. add, remove, or delete.
  80. +
  81. If the `alias` parameter is not specified,
  82. this parameter is required for the `add` or `remove` action.
  83. `filter`::
  84. (Optional, query object)
  85. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-alias-filter]
  86. +
  87. See <<filtered>> for an example.
  88. `is_hidden`::
  89. (Optional, boolean)
  90. If `true`, the alias will be excluded from wildcard expressions by default,
  91. unless overriden in the request using the `expand_wildcards` parameter,
  92. similar to <<index-hidden,hidden indices>>. This property must be set to the
  93. same value on all indices that share an alias. Defaults to `false`.
  94. `must_exist`::
  95. (Optional, boolean)
  96. If `true`, the alias to remove must exist. Defaults to `false`.
  97. `is_write_index`::
  98. (Optional, boolean)
  99. If `true`, assigns the index as an alias's write index.
  100. Defaults to `false`.
  101. +
  102. An alias can have one write index at a time.
  103. +
  104. See <<aliases-write-index>> for an example.
  105. +
  106. [IMPORTANT]
  107. ====
  108. Aliases that do not explicitly set `is_write_index: true` for an index, and
  109. only reference one index, will have that referenced index behave as if it is the write index
  110. until an additional index is referenced. At that point, there will be no write index and
  111. writes will be rejected.
  112. ====
  113. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-routing]
  114. +
  115. See <<aliases-routing>> for an example.
  116. `index_routing`::
  117. (Optional, string)
  118. Custom <<mapping-routing-field, routing value>> used
  119. for the alias's indexing operations.
  120. +
  121. See <<aliases-routing>> for an example.
  122. `search_routing`::
  123. (Optional, string)
  124. Custom <<mapping-routing-field, routing value>> used
  125. for the alias's search operations.
  126. +
  127. See <<aliases-routing>> for an example.
  128. --
  129. [[indices-aliases-api-example]]
  130. ==== {api-examples-title}
  131. [[indices-aliases-api-add-alias-ex]]
  132. ===== Add an alias
  133. The following request adds the `alias1` alias to the `test1` index.
  134. [source,console]
  135. --------------------------------------------------
  136. POST /_aliases
  137. {
  138. "actions" : [
  139. { "add" : { "index" : "test1", "alias" : "alias1" } }
  140. ]
  141. }
  142. --------------------------------------------------
  143. // TEST[s/^/PUT test1\nPUT test2\n/]
  144. [[indices-aliases-api-remove-alias-ex]]
  145. ===== Remove an alias
  146. The following request removes the `alias1` alias.
  147. [source,console]
  148. --------------------------------------------------
  149. POST /_aliases
  150. {
  151. "actions" : [
  152. { "remove" : { "index" : "test1", "alias" : "alias1" } }
  153. ]
  154. }
  155. --------------------------------------------------
  156. // TEST[continued]
  157. [[indices-aliases-api-rename-alias-ex]]
  158. ===== Rename an alias
  159. Renaming an alias is a simple `remove` then `add` operation within the
  160. same API. This operation is atomic, no need to worry about a short
  161. period of time where the alias does not point to an index:
  162. [source,console]
  163. --------------------------------------------------
  164. POST /_aliases
  165. {
  166. "actions" : [
  167. { "remove" : { "index" : "test1", "alias" : "alias1" } },
  168. { "add" : { "index" : "test1", "alias" : "alias2" } }
  169. ]
  170. }
  171. --------------------------------------------------
  172. // TEST[continued]
  173. [[indices-aliases-api-add-multi-alias-ex]]
  174. ===== Add an alias to multiple indices
  175. Associating an alias with more than one index is simply several `add`
  176. actions:
  177. [source,console]
  178. --------------------------------------------------
  179. POST /_aliases
  180. {
  181. "actions" : [
  182. { "add" : { "index" : "test1", "alias" : "alias1" } },
  183. { "add" : { "index" : "test2", "alias" : "alias1" } }
  184. ]
  185. }
  186. --------------------------------------------------
  187. // TEST[s/^/PUT test1\nPUT test2\n/]
  188. Multiple indices can be specified for an action with the `indices` array syntax:
  189. [source,console]
  190. --------------------------------------------------
  191. POST /_aliases
  192. {
  193. "actions" : [
  194. { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
  195. ]
  196. }
  197. --------------------------------------------------
  198. // TEST[s/^/PUT test1\nPUT test2\n/]
  199. To specify multiple aliases in one action, the corresponding `aliases` array
  200. syntax exists as well.
  201. For the example above, a glob pattern can also be used to associate an alias to
  202. more than one index that share a common name:
  203. [source,console]
  204. --------------------------------------------------
  205. POST /_aliases
  206. {
  207. "actions" : [
  208. { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
  209. ]
  210. }
  211. --------------------------------------------------
  212. // TEST[s/^/PUT test1\nPUT test2\n/]
  213. In this case, the alias is a point-in-time alias that will group all
  214. current indices that match, it will not automatically update as new
  215. indices that match this pattern are added/removed.
  216. It is an error to index to an alias which points to more than one index.
  217. It is also possible to swap an index with an alias in one, atomic operation.
  218. This means there will be no point in time where the alias points to no
  219. index in the cluster state. However, as indexing and searches involve multiple
  220. steps, it is possible for the in-flight or queued requests to fail
  221. due to a temporarily non-existent index.
  222. [source,console]
  223. --------------------------------------------------
  224. PUT test <1>
  225. PUT test_2 <2>
  226. POST /_aliases
  227. {
  228. "actions" : [
  229. { "add": { "index": "test_2", "alias": "test" } },
  230. { "remove_index": { "index": "test" } } <3>
  231. ]
  232. }
  233. --------------------------------------------------
  234. <1> An index we've added by mistake
  235. <2> The index we should have added
  236. <3> `remove_index` is just like <<indices-delete-index>>
  237. [[filtered]]
  238. ===== Filtered aliases
  239. Aliases with filters provide an easy way to create different "views" of
  240. the same index. The filter can be defined using Query DSL and is applied
  241. to all Search, Count, Delete By Query and More Like This operations with
  242. this alias.
  243. To create a filtered alias, first we need to ensure that the fields already
  244. exist in the mapping:
  245. [source,console]
  246. --------------------------------------------------
  247. PUT /my-index-000001
  248. {
  249. "mappings": {
  250. "properties": {
  251. "user": {
  252. "properties": {
  253. "id": {
  254. "type": "keyword"
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. --------------------------------------------------
  262. Now we can create an alias that uses a filter on field `user.id`:
  263. [source,console]
  264. --------------------------------------------------
  265. POST /_aliases
  266. {
  267. "actions": [
  268. {
  269. "add": {
  270. "index": "my-index-000001",
  271. "alias": "alias2",
  272. "filter": { "term": { "user.id": "kimchy" } }
  273. }
  274. }
  275. ]
  276. }
  277. --------------------------------------------------
  278. // TEST[continued]
  279. [[aliases-routing]]
  280. ===== Routing
  281. It is possible to associate routing values with aliases. This feature
  282. can be used together with filtering aliases in order to avoid
  283. unnecessary shard operations.
  284. The following command creates a new alias `alias1` that points to index
  285. `test`. After `alias1` is created, all operations with this alias are
  286. automatically modified to use value `1` for routing:
  287. [source,console]
  288. --------------------------------------------------
  289. POST /_aliases
  290. {
  291. "actions": [
  292. {
  293. "add": {
  294. "index": "test",
  295. "alias": "alias1",
  296. "routing": "1"
  297. }
  298. }
  299. ]
  300. }
  301. --------------------------------------------------
  302. // TEST[s/^/PUT test\n/]
  303. It's also possible to specify different routing values for searching
  304. and indexing operations:
  305. [source,console]
  306. --------------------------------------------------
  307. POST /_aliases
  308. {
  309. "actions": [
  310. {
  311. "add": {
  312. "index": "test",
  313. "alias": "alias2",
  314. "search_routing": "1,2",
  315. "index_routing": "2"
  316. }
  317. }
  318. ]
  319. }
  320. --------------------------------------------------
  321. // TEST[s/^/PUT test\n/]
  322. As shown in the example above, search routing may contain several values
  323. separated by comma. Index routing can contain only a single value.
  324. If a search operation that uses routing alias also has a routing parameter, an
  325. intersection of both search alias routing and routing specified in the
  326. parameter is used. For example the following command will use "2" as a
  327. routing value:
  328. [source,console]
  329. --------------------------------------------------
  330. GET /alias2/_search?q=user.id:kimchy&routing=2,3
  331. --------------------------------------------------
  332. // TEST[continued]
  333. [[aliases-write-index]]
  334. ===== Write index
  335. It is possible to associate the index pointed to by an alias as the write index.
  336. When specified, all index and update requests against an alias that point to multiple
  337. indices will attempt to resolve to the one index that is the write index.
  338. Only one index per alias can be assigned to be the write index at a time. If no write index is specified
  339. and there are multiple indices referenced by an alias, then writes will not be allowed.
  340. It is possible to specify an index associated with an alias as a write index using both the aliases API
  341. and index creation API.
  342. Setting an index to be the write index with an alias also affects how the alias is manipulated during
  343. Rollover (see <<indices-rollover-index, Rollover With Write Index>>).
  344. [source,console]
  345. --------------------------------------------------
  346. POST /_aliases
  347. {
  348. "actions": [
  349. {
  350. "add": {
  351. "index": "test",
  352. "alias": "alias1",
  353. "is_write_index": true
  354. }
  355. },
  356. {
  357. "add": {
  358. "index": "test2",
  359. "alias": "alias1"
  360. }
  361. }
  362. ]
  363. }
  364. --------------------------------------------------
  365. // TEST[s/^/PUT test\nPUT test2\n/]
  366. In this example, we associate the alias `alias1` to both `test` and `test2`, where
  367. `test` will be the index chosen for writing to.
  368. [source,console]
  369. --------------------------------------------------
  370. PUT /alias1/_doc/1
  371. {
  372. "foo": "bar"
  373. }
  374. --------------------------------------------------
  375. // TEST[continued]
  376. The new document that was indexed to `/alias1/_doc/1` will be indexed as if it were
  377. `/test/_doc/1`.
  378. [source,console]
  379. --------------------------------------------------
  380. GET /test/_doc/1
  381. --------------------------------------------------
  382. // TEST[continued]
  383. To swap which index is the write index for an alias, the Aliases API can be leveraged to
  384. do an atomic swap. The swap is not dependent on the ordering of the actions.
  385. [source,console]
  386. --------------------------------------------------
  387. POST /_aliases
  388. {
  389. "actions": [
  390. {
  391. "add": {
  392. "index": "test",
  393. "alias": "alias1",
  394. "is_write_index": false
  395. }
  396. }, {
  397. "add": {
  398. "index": "test2",
  399. "alias": "alias1",
  400. "is_write_index": true
  401. }
  402. }
  403. ]
  404. }
  405. --------------------------------------------------
  406. // TEST[s/^/PUT test\nPUT test2\n/]