aliases.asciidoc 13 KB

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