aliases.asciidoc 13 KB

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