aliases.asciidoc 12 KB

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