aliases.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. [[indices-aliases]]
  2. == Index Aliases
  3. APIs in Elasticsearch accept an index name when working against a
  4. specific index, and several indices when applicable. The index aliases
  5. API allows aliasing an index with a name, with all APIs automatically
  6. converting the alias name to the actual index name. An alias can also be
  7. mapped to more than one index, and when specifying it, the alias will
  8. automatically expand to the aliased indices. An alias can also be
  9. associated with a filter that will automatically be applied when
  10. searching, and routing values. An alias cannot have the same name as an index.
  11. Here is a sample of associating the alias `alias1` with index `test1`:
  12. [source,js]
  13. --------------------------------------------------
  14. POST /_aliases
  15. {
  16. "actions" : [
  17. { "add" : { "index" : "test1", "alias" : "alias1" } }
  18. ]
  19. }
  20. --------------------------------------------------
  21. // CONSOLE
  22. // TEST[s/^/PUT test1\nPUT test2\n/]
  23. And here is removing that same alias:
  24. [source,js]
  25. --------------------------------------------------
  26. POST /_aliases
  27. {
  28. "actions" : [
  29. { "remove" : { "index" : "test1", "alias" : "alias1" } }
  30. ]
  31. }
  32. --------------------------------------------------
  33. // CONSOLE
  34. // TEST[continued]
  35. Renaming an alias is a simple `remove` then `add` operation within the
  36. same API. This operation is atomic, no need to worry about a short
  37. period of time where the alias does not point to an index:
  38. [source,js]
  39. --------------------------------------------------
  40. POST /_aliases
  41. {
  42. "actions" : [
  43. { "remove" : { "index" : "test1", "alias" : "alias1" } },
  44. { "add" : { "index" : "test2", "alias" : "alias1" } }
  45. ]
  46. }
  47. --------------------------------------------------
  48. // CONSOLE
  49. // TEST[continued]
  50. Associating an alias with more than one index is simply several `add`
  51. actions:
  52. [source,js]
  53. --------------------------------------------------
  54. POST /_aliases
  55. {
  56. "actions" : [
  57. { "add" : { "index" : "test1", "alias" : "alias1" } },
  58. { "add" : { "index" : "test2", "alias" : "alias1" } }
  59. ]
  60. }
  61. --------------------------------------------------
  62. // CONSOLE
  63. // TEST[s/^/PUT test1\nPUT test2\n/]
  64. Multiple indices can be specified for an action with the `indices` array syntax:
  65. [source,js]
  66. --------------------------------------------------
  67. POST /_aliases
  68. {
  69. "actions" : [
  70. { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
  71. ]
  72. }
  73. --------------------------------------------------
  74. // CONSOLE
  75. // TEST[s/^/PUT test1\nPUT test2\n/]
  76. To specify multiple aliases in one action, the corresponding `aliases` array
  77. syntax exists as well.
  78. For the example above, a glob pattern can also be used to associate an alias to
  79. more than one index that share a common name:
  80. [source,js]
  81. --------------------------------------------------
  82. POST /_aliases
  83. {
  84. "actions" : [
  85. { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
  86. ]
  87. }
  88. --------------------------------------------------
  89. // CONSOLE
  90. // TEST[s/^/PUT test1\nPUT test2\n/]
  91. In this case, the alias is a point-in-time alias that will group all
  92. current indices that match, it will not automatically update as new
  93. indices that match this pattern are added/removed.
  94. It is an error to index to an alias which points to more than one index.
  95. It is also possible to swap an index with an alias in one operation:
  96. [source,js]
  97. --------------------------------------------------
  98. PUT test <1>
  99. PUT test_2 <2>
  100. POST /_aliases
  101. {
  102. "actions" : [
  103. { "add": { "index": "test_2", "alias": "test" } },
  104. { "remove_index": { "index": "test" } } <3>
  105. ]
  106. }
  107. --------------------------------------------------
  108. // CONSOLE
  109. <1> An index we've added by mistake
  110. <2> The index we should have added
  111. <3> `remove_index` is just like <<indices-delete-index>>
  112. [float]
  113. [[filtered]]
  114. === Filtered Aliases
  115. Aliases with filters provide an easy way to create different "views" of
  116. the same index. The filter can be defined using Query DSL and is applied
  117. to all Search, Count, Delete By Query and More Like This operations with
  118. this alias.
  119. To create a filtered alias, first we need to ensure that the fields already
  120. exist in the mapping:
  121. [source,js]
  122. --------------------------------------------------
  123. PUT /test1
  124. {
  125. "mappings": {
  126. "type1": {
  127. "properties": {
  128. "user" : {
  129. "type": "keyword"
  130. }
  131. }
  132. }
  133. }
  134. }
  135. --------------------------------------------------
  136. // CONSOLE
  137. Now we can create an alias that uses a filter on field `user`:
  138. [source,js]
  139. --------------------------------------------------
  140. POST /_aliases
  141. {
  142. "actions" : [
  143. {
  144. "add" : {
  145. "index" : "test1",
  146. "alias" : "alias2",
  147. "filter" : { "term" : { "user" : "kimchy" } }
  148. }
  149. }
  150. ]
  151. }
  152. --------------------------------------------------
  153. // CONSOLE
  154. // TEST[continued]
  155. [float]
  156. [[aliases-routing]]
  157. ==== Routing
  158. It is possible to associate routing values with aliases. This feature
  159. can be used together with filtering aliases in order to avoid
  160. unnecessary shard operations.
  161. The following command creates a new alias `alias1` that points to index
  162. `test`. After `alias1` is created, all operations with this alias are
  163. automatically modified to use value `1` for routing:
  164. [source,js]
  165. --------------------------------------------------
  166. POST /_aliases
  167. {
  168. "actions" : [
  169. {
  170. "add" : {
  171. "index" : "test",
  172. "alias" : "alias1",
  173. "routing" : "1"
  174. }
  175. }
  176. ]
  177. }
  178. --------------------------------------------------
  179. // CONSOLE
  180. // TEST[s/^/PUT test\n/]
  181. It's also possible to specify different routing values for searching
  182. and indexing operations:
  183. [source,js]
  184. --------------------------------------------------
  185. POST /_aliases
  186. {
  187. "actions" : [
  188. {
  189. "add" : {
  190. "index" : "test",
  191. "alias" : "alias2",
  192. "search_routing" : "1,2",
  193. "index_routing" : "2"
  194. }
  195. }
  196. ]
  197. }
  198. --------------------------------------------------
  199. // CONSOLE
  200. // TEST[s/^/PUT test\n/]
  201. As shown in the example above, search routing may contain several values
  202. separated by comma. Index routing can contain only a single value.
  203. If a search operation that uses routing alias also has a routing parameter, an
  204. intersection of both search alias routing and routing specified in the
  205. parameter is used. For example the following command will use "2" as a
  206. routing value:
  207. [source,js]
  208. --------------------------------------------------
  209. GET /alias2/_search?q=user:kimchy&routing=2,3
  210. --------------------------------------------------
  211. // CONSOLE
  212. // TEST[continued]
  213. [float]
  214. [[aliases-write-index]]
  215. ==== Write Index
  216. It is possible to associate the index pointed to by an alias as the write index.
  217. When specified, all index and update requests against an alias that point to multiple
  218. indices will attempt to resolve to the one index that is the write index.
  219. Only one index per alias can be assigned to be the write index at a time. If no write index is specified
  220. and there are multiple indices referenced by an alias, then writes will not be allowed.
  221. It is possible to specify an index associated with an alias as a write index using both the aliases API
  222. and index creation API.
  223. [source,js]
  224. --------------------------------------------------
  225. POST /_aliases
  226. {
  227. "actions" : [
  228. {
  229. "add" : {
  230. "index" : "test",
  231. "alias" : "alias1",
  232. "is_write_index" : true
  233. }
  234. }
  235. ]
  236. }
  237. --------------------------------------------------
  238. // CONSOLE
  239. // TEST[s/^/PUT test\n/]
  240. In this example, we associate the alias `alias1` to both `test` and `test2`, where
  241. `test` will be the index chosen for writing to.
  242. [source,js]
  243. --------------------------------------------------
  244. PUT /alias1/_doc/1
  245. {
  246. "foo": "bar"
  247. }
  248. --------------------------------------------------
  249. // CONSOLE
  250. // TEST[continued]
  251. The new document that was indexed to `/alias1/_doc/1` will be indexed as if it were
  252. `/test/_doc/1`.
  253. [source,js]
  254. --------------------------------------------------
  255. GET /test/_doc/1
  256. --------------------------------------------------
  257. // CONSOLE
  258. // TEST[continued]
  259. To swap which index is the write index for an alias, the Aliases API can be leveraged to
  260. do an atomic swap. The swap is not dependent on the ordering of the actions.
  261. [source,js]
  262. --------------------------------------------------
  263. POST /_aliases
  264. {
  265. "actions" : [
  266. {
  267. "add" : {
  268. "index" : "test",
  269. "alias" : "alias1",
  270. "is_write_index" : true
  271. }
  272. }, {
  273. "add" : {
  274. "index" : "test2",
  275. "alias" : "alias1",
  276. "is_write_index" : false
  277. }
  278. }
  279. ]
  280. }
  281. --------------------------------------------------
  282. // CONSOLE
  283. // TEST[s/^/PUT test\nPUT test2\n/]
  284. [IMPORTANT]
  285. =====================================
  286. Aliases that do not explicitly set `is_write_index: true` for an index, and
  287. only reference one index, will have that referenced index behave as if it is the write index
  288. until an additional index is referenced. At that point, there will be no write index and
  289. writes will be rejected.
  290. =====================================
  291. [float]
  292. [[alias-adding]]
  293. === Add a single alias
  294. An alias can also be added with the endpoint
  295. `PUT /{index}/_alias/{name}`
  296. where
  297. [horizontal]
  298. `index`:: The index the alias refers to. Can be any of `* | _all | glob pattern | name1, name2, …`
  299. `name`:: The name of the alias. This is a required option.
  300. `routing`:: An optional routing that can be associated with an alias.
  301. `filter`:: An optional filter that can be associated with an alias.
  302. You can also use the plural `_aliases`.
  303. [float]
  304. ==== Examples:
  305. Adding time based alias::
  306. +
  307. --
  308. [source,js]
  309. --------------------------------------------------
  310. PUT /logs_201305/_alias/2013
  311. --------------------------------------------------
  312. // CONSOLE
  313. // TEST[s/^/PUT logs_201305\n/]
  314. --
  315. Adding a user alias::
  316. +
  317. --
  318. First create the index and add a mapping for the `user_id` field:
  319. [source,js]
  320. --------------------------------------------------
  321. PUT /users
  322. {
  323. "mappings" : {
  324. "user" : {
  325. "properties" : {
  326. "user_id" : {"type" : "integer"}
  327. }
  328. }
  329. }
  330. }
  331. --------------------------------------------------
  332. // CONSOLE
  333. Then add the alias for a specific user:
  334. [source,js]
  335. --------------------------------------------------
  336. PUT /users/_alias/user_12
  337. {
  338. "routing" : "12",
  339. "filter" : {
  340. "term" : {
  341. "user_id" : 12
  342. }
  343. }
  344. }
  345. --------------------------------------------------
  346. // CONSOLE
  347. // TEST[continued]
  348. --
  349. [float]
  350. [[alias-index-creation]]
  351. === Aliases during index creation
  352. Aliases can also be specified during <<create-index-aliases,index creation>>:
  353. [source,js]
  354. --------------------------------------------------
  355. PUT /logs_20162801
  356. {
  357. "mappings" : {
  358. "type" : {
  359. "properties" : {
  360. "year" : {"type" : "integer"}
  361. }
  362. }
  363. },
  364. "aliases" : {
  365. "current_day" : {},
  366. "2016" : {
  367. "filter" : {
  368. "term" : {"year" : 2016 }
  369. }
  370. }
  371. }
  372. }
  373. --------------------------------------------------
  374. // CONSOLE
  375. [float]
  376. [[deleting]]
  377. === Delete aliases
  378. The rest endpoint is: `/{index}/_alias/{name}`
  379. where
  380. [horizontal]
  381. `index`:: `* | _all | glob pattern | name1, name2, …`
  382. `name`:: `* | _all | glob pattern | name1, name2, …`
  383. Alternatively you can use the plural `_aliases`. Example:
  384. [source,js]
  385. --------------------------------------------------
  386. DELETE /logs_20162801/_alias/current_day
  387. --------------------------------------------------
  388. // CONSOLE
  389. // TEST[continued]
  390. [float]
  391. [[alias-retrieving]]
  392. === Retrieving existing aliases
  393. The get index alias API allows to filter by
  394. alias name and index name. This api redirects to the master and fetches
  395. the requested index aliases, if available. This api only serialises the
  396. found index aliases.
  397. Possible options:
  398. [horizontal]
  399. `index`::
  400. The index name to get aliases for. Partial names are
  401. supported via wildcards, also multiple index names can be specified
  402. separated with a comma. Also the alias name for an index can be used.
  403. `alias`::
  404. The name of alias to return in the response. Like the index
  405. option, this option supports wildcards and the option the specify
  406. multiple alias names separated by a comma.
  407. `ignore_unavailable`::
  408. What to do if an specified index name doesn't
  409. exist. If set to `true` then those indices are ignored.
  410. The rest endpoint is: `/{index}/_alias/{alias}`.
  411. [float]
  412. ==== Examples:
  413. All aliases for the index users:
  414. [source,js]
  415. --------------------------------------------------
  416. GET /logs_20162801/_alias/*
  417. --------------------------------------------------
  418. // CONSOLE
  419. // TEST[continued]
  420. Response:
  421. [source,js]
  422. --------------------------------------------------
  423. {
  424. "logs_20162801" : {
  425. "aliases" : {
  426. "2016" : {
  427. "filter" : {
  428. "term" : {
  429. "year" : 2016
  430. }
  431. }
  432. }
  433. }
  434. }
  435. }
  436. --------------------------------------------------
  437. // TESTRESPONSE
  438. All aliases with the name 2016 in any index:
  439. [source,js]
  440. --------------------------------------------------
  441. GET /_alias/2016
  442. --------------------------------------------------
  443. // CONSOLE
  444. // TEST[continued]
  445. Response:
  446. [source,js]
  447. --------------------------------------------------
  448. {
  449. "logs_20162801" : {
  450. "aliases" : {
  451. "2016" : {
  452. "filter" : {
  453. "term" : {
  454. "year" : 2016
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. --------------------------------------------------
  462. // TESTRESPONSE
  463. All aliases that start with 20 in any index:
  464. [source,js]
  465. --------------------------------------------------
  466. GET /_alias/20*
  467. --------------------------------------------------
  468. // CONSOLE
  469. // TEST[continued]
  470. Response:
  471. [source,js]
  472. --------------------------------------------------
  473. {
  474. "logs_20162801" : {
  475. "aliases" : {
  476. "2016" : {
  477. "filter" : {
  478. "term" : {
  479. "year" : 2016
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. --------------------------------------------------
  487. // TESTRESPONSE
  488. There is also a HEAD variant of the get indices aliases api to check if
  489. index aliases exist. The indices aliases exists api supports the same
  490. option as the get indices aliases api. Examples:
  491. [source,js]
  492. --------------------------------------------------
  493. HEAD /_alias/2016
  494. HEAD /_alias/20*
  495. HEAD /logs_20162801/_alias/*
  496. --------------------------------------------------
  497. // CONSOLE
  498. // TEST[continued]