aliases.asciidoc 12 KB

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