aliases.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 add, remove,
  82. or delete. Supports <<date-math-index-names,date math>>.
  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 add, remove, or delete. Supports
  89. <<date-math-index-names,date math>>.
  90. +
  91. If the `alias` parameter is not specified, this parameter is required for the
  92. `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 overridden 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. Index alias names support <<date-math-index-names,date math>>.
  155. [source,console]
  156. ----
  157. POST /_aliases
  158. {
  159. "actions" : [
  160. { "add" : { "index" : "logs", "alias" : "<logs_{now/M}>" } }
  161. ]
  162. }
  163. ----
  164. // TEST[s/^/PUT logs\n/]
  165. // TEST[continued]
  166. [[indices-aliases-api-remove-alias-ex]]
  167. ===== Remove an alias
  168. The following request removes the `alias1` alias.
  169. [source,console]
  170. --------------------------------------------------
  171. POST /_aliases
  172. {
  173. "actions" : [
  174. { "remove" : { "index" : "test1", "alias" : "alias1" } }
  175. ]
  176. }
  177. --------------------------------------------------
  178. // TEST[continued]
  179. [[indices-aliases-api-rename-alias-ex]]
  180. ===== Rename an alias
  181. Renaming an alias is a simple `remove` then `add` operation within the
  182. same API. This operation is atomic, no need to worry about a short
  183. period of time where the alias does not point to an index:
  184. [source,console]
  185. --------------------------------------------------
  186. POST /_aliases
  187. {
  188. "actions" : [
  189. { "remove" : { "index" : "test1", "alias" : "alias1" } },
  190. { "add" : { "index" : "test1", "alias" : "alias2" } }
  191. ]
  192. }
  193. --------------------------------------------------
  194. // TEST[continued]
  195. [[indices-aliases-api-add-multi-alias-ex]]
  196. ===== Add an alias to multiple indices
  197. Associating an alias with more than one index is simply several `add`
  198. actions:
  199. [source,console]
  200. --------------------------------------------------
  201. POST /_aliases
  202. {
  203. "actions" : [
  204. { "add" : { "index" : "test1", "alias" : "alias1" } },
  205. { "add" : { "index" : "test2", "alias" : "alias1" } }
  206. ]
  207. }
  208. --------------------------------------------------
  209. // TEST[s/^/PUT test1\nPUT test2\n/]
  210. Multiple indices can be specified for an action with the `indices` array syntax:
  211. [source,console]
  212. --------------------------------------------------
  213. POST /_aliases
  214. {
  215. "actions" : [
  216. { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
  217. ]
  218. }
  219. --------------------------------------------------
  220. // TEST[s/^/PUT test1\nPUT test2\n/]
  221. To specify multiple aliases in one action, the corresponding `aliases` array
  222. syntax exists as well.
  223. For the example above, a glob pattern can also be used to associate an alias to
  224. more than one index that share a common name:
  225. [source,console]
  226. --------------------------------------------------
  227. POST /_aliases
  228. {
  229. "actions" : [
  230. { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
  231. ]
  232. }
  233. --------------------------------------------------
  234. // TEST[s/^/PUT test1\nPUT test2\n/]
  235. In this case, the alias is a point-in-time alias that will group all
  236. current indices that match, it will not automatically update as new
  237. indices that match this pattern are added/removed.
  238. It is an error to index to an alias which points to more than one index.
  239. It is also possible to swap an index with an alias in one, atomic operation.
  240. This means there will be no point in time where the alias points to no
  241. index in the cluster state. However, as indexing and searches involve multiple
  242. steps, it is possible for the in-flight or queued requests to fail
  243. due to a temporarily non-existent index.
  244. [source,console]
  245. --------------------------------------------------
  246. PUT test <1>
  247. PUT test_2 <2>
  248. POST /_aliases
  249. {
  250. "actions" : [
  251. { "add": { "index": "test_2", "alias": "test" } },
  252. { "remove_index": { "index": "test" } } <3>
  253. ]
  254. }
  255. --------------------------------------------------
  256. <1> An index we've added by mistake
  257. <2> The index we should have added
  258. <3> `remove_index` is just like <<indices-delete-index>> and will only remove a concrete index.
  259. [[filtered]]
  260. ===== Filtered aliases
  261. Aliases with filters provide an easy way to create different "views" of
  262. the same index. The filter can be defined using Query DSL and is applied
  263. to all Search, Count, Delete By Query and More Like This operations with
  264. this alias.
  265. To create a filtered alias, first we need to ensure that the fields already
  266. exist in the mapping:
  267. [source,console]
  268. --------------------------------------------------
  269. PUT /my-index-000001
  270. {
  271. "mappings": {
  272. "properties": {
  273. "user": {
  274. "properties": {
  275. "id": {
  276. "type": "keyword"
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. --------------------------------------------------
  284. Now we can create an alias that uses a filter on field `user.id`:
  285. [source,console]
  286. --------------------------------------------------
  287. POST /_aliases
  288. {
  289. "actions": [
  290. {
  291. "add": {
  292. "index": "my-index-000001",
  293. "alias": "alias2",
  294. "filter": { "term": { "user.id": "kimchy" } }
  295. }
  296. }
  297. ]
  298. }
  299. --------------------------------------------------
  300. // TEST[continued]
  301. [[aliases-routing]]
  302. ===== Routing
  303. It is possible to associate routing values with aliases. This feature
  304. can be used together with filtering aliases in order to avoid
  305. unnecessary shard operations.
  306. The following command creates a new alias `alias1` that points to index
  307. `test`. After `alias1` is created, all operations with this alias are
  308. automatically modified to use value `1` for routing:
  309. [source,console]
  310. --------------------------------------------------
  311. POST /_aliases
  312. {
  313. "actions": [
  314. {
  315. "add": {
  316. "index": "test",
  317. "alias": "alias1",
  318. "routing": "1"
  319. }
  320. }
  321. ]
  322. }
  323. --------------------------------------------------
  324. // TEST[s/^/PUT test\n/]
  325. It's also possible to specify different routing values for searching
  326. and indexing operations:
  327. [source,console]
  328. --------------------------------------------------
  329. POST /_aliases
  330. {
  331. "actions": [
  332. {
  333. "add": {
  334. "index": "test",
  335. "alias": "alias2",
  336. "search_routing": "1,2",
  337. "index_routing": "2"
  338. }
  339. }
  340. ]
  341. }
  342. --------------------------------------------------
  343. // TEST[s/^/PUT test\n/]
  344. As shown in the example above, search routing may contain several values
  345. separated by comma. Index routing can contain only a single value.
  346. If a search operation that uses routing alias also has a routing parameter, an
  347. intersection of both search alias routing and routing specified in the
  348. parameter is used. For example the following command will use "2" as a
  349. routing value:
  350. [source,console]
  351. --------------------------------------------------
  352. GET /alias2/_search?q=user.id:kimchy&routing=2,3
  353. --------------------------------------------------
  354. // TEST[continued]
  355. [[aliases-write-index]]
  356. ===== Write index
  357. It is possible to associate the index pointed to by an alias as the write index.
  358. When specified, all index and update requests against an alias that point to multiple
  359. indices will attempt to resolve to the one index that is the write index.
  360. Only one index per alias can be assigned to be the write index at a time. If no write index is specified
  361. and there are multiple indices referenced by an alias, then writes will not be allowed.
  362. It is possible to specify an index associated with an alias as a write index using both the aliases API
  363. and index creation API.
  364. Setting an index to be the write index with an alias also affects how the alias is manipulated during
  365. Rollover (see <<indices-rollover-index, Rollover With Write Index>>).
  366. [source,console]
  367. --------------------------------------------------
  368. POST /_aliases
  369. {
  370. "actions": [
  371. {
  372. "add": {
  373. "index": "test",
  374. "alias": "alias1",
  375. "is_write_index": true
  376. }
  377. },
  378. {
  379. "add": {
  380. "index": "test2",
  381. "alias": "alias1"
  382. }
  383. }
  384. ]
  385. }
  386. --------------------------------------------------
  387. // TEST[s/^/PUT test\nPUT test2\n/]
  388. In this example, we associate the alias `alias1` to both `test` and `test2`, where
  389. `test` will be the index chosen for writing to.
  390. [source,console]
  391. --------------------------------------------------
  392. PUT /alias1/_doc/1
  393. {
  394. "foo": "bar"
  395. }
  396. --------------------------------------------------
  397. // TEST[continued]
  398. The new document that was indexed to `/alias1/_doc/1` will be indexed as if it were
  399. `/test/_doc/1`.
  400. [source,console]
  401. --------------------------------------------------
  402. GET /test/_doc/1
  403. --------------------------------------------------
  404. // TEST[continued]
  405. To swap which index is the write index for an alias, the Aliases API can be leveraged to
  406. do an atomic swap. The swap is not dependent on the ordering of the actions.
  407. [source,console]
  408. --------------------------------------------------
  409. POST /_aliases
  410. {
  411. "actions": [
  412. {
  413. "add": {
  414. "index": "test",
  415. "alias": "alias1",
  416. "is_write_index": false
  417. }
  418. }, {
  419. "add": {
  420. "index": "test2",
  421. "alias": "alias1",
  422. "is_write_index": true
  423. }
  424. }
  425. ]
  426. }
  427. --------------------------------------------------
  428. // TEST[s/^/PUT test\nPUT test2\n/]