aliases.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. Setting an index to be the write index with an alias also affects how the alias is manipulated during
  224. Rollover (see <<indices-rollover-index, Rollover With Write Index>>).
  225. [source,js]
  226. --------------------------------------------------
  227. POST /_aliases
  228. {
  229. "actions" : [
  230. {
  231. "add" : {
  232. "index" : "test",
  233. "alias" : "alias1",
  234. "is_write_index" : true
  235. }
  236. }
  237. ]
  238. }
  239. --------------------------------------------------
  240. // CONSOLE
  241. // TEST[s/^/PUT test\n/]
  242. In this example, we associate the alias `alias1` to both `test` and `test2`, where
  243. `test` will be the index chosen for writing to.
  244. [source,js]
  245. --------------------------------------------------
  246. PUT /alias1/_doc/1
  247. {
  248. "foo": "bar"
  249. }
  250. --------------------------------------------------
  251. // CONSOLE
  252. // TEST[continued]
  253. The new document that was indexed to `/alias1/_doc/1` will be indexed as if it were
  254. `/test/_doc/1`.
  255. [source,js]
  256. --------------------------------------------------
  257. GET /test/_doc/1
  258. --------------------------------------------------
  259. // CONSOLE
  260. // TEST[continued]
  261. To swap which index is the write index for an alias, the Aliases API can be leveraged to
  262. do an atomic swap. The swap is not dependent on the ordering of the actions.
  263. [source,js]
  264. --------------------------------------------------
  265. POST /_aliases
  266. {
  267. "actions" : [
  268. {
  269. "add" : {
  270. "index" : "test",
  271. "alias" : "alias1",
  272. "is_write_index" : true
  273. }
  274. }, {
  275. "add" : {
  276. "index" : "test2",
  277. "alias" : "alias1",
  278. "is_write_index" : false
  279. }
  280. }
  281. ]
  282. }
  283. --------------------------------------------------
  284. // CONSOLE
  285. // TEST[s/^/PUT test\nPUT test2\n/]
  286. [IMPORTANT]
  287. =====================================
  288. Aliases that do not explicitly set `is_write_index: true` for an index, and
  289. only reference one index, will have that referenced index behave as if it is the write index
  290. until an additional index is referenced. At that point, there will be no write index and
  291. writes will be rejected.
  292. =====================================
  293. [float]
  294. [[alias-adding]]
  295. === Add a single alias
  296. An alias can also be added with the endpoint
  297. `PUT /{index}/_alias/{name}`
  298. where
  299. [horizontal]
  300. `index`:: The index the alias refers to. Can be any of `* | _all | glob pattern | name1, name2, …`
  301. `name`:: The name of the alias. This is a required option.
  302. `routing`:: An optional routing that can be associated with an alias.
  303. `filter`:: An optional filter that can be associated with an alias.
  304. You can also use the plural `_aliases`.
  305. [float]
  306. ==== Examples:
  307. Adding time based alias::
  308. +
  309. --
  310. [source,js]
  311. --------------------------------------------------
  312. PUT /logs_201305/_alias/2013
  313. --------------------------------------------------
  314. // CONSOLE
  315. // TEST[s/^/PUT logs_201305\n/]
  316. --
  317. Adding a user alias::
  318. +
  319. --
  320. First create the index and add a mapping for the `user_id` field:
  321. [source,js]
  322. --------------------------------------------------
  323. PUT /users
  324. {
  325. "mappings" : {
  326. "user" : {
  327. "properties" : {
  328. "user_id" : {"type" : "integer"}
  329. }
  330. }
  331. }
  332. }
  333. --------------------------------------------------
  334. // CONSOLE
  335. Then add the alias for a specific user:
  336. [source,js]
  337. --------------------------------------------------
  338. PUT /users/_alias/user_12
  339. {
  340. "routing" : "12",
  341. "filter" : {
  342. "term" : {
  343. "user_id" : 12
  344. }
  345. }
  346. }
  347. --------------------------------------------------
  348. // CONSOLE
  349. // TEST[continued]
  350. --
  351. [float]
  352. [[alias-index-creation]]
  353. === Aliases during index creation
  354. Aliases can also be specified during <<create-index-aliases,index creation>>:
  355. [source,js]
  356. --------------------------------------------------
  357. PUT /logs_20162801
  358. {
  359. "mappings" : {
  360. "type" : {
  361. "properties" : {
  362. "year" : {"type" : "integer"}
  363. }
  364. }
  365. },
  366. "aliases" : {
  367. "current_day" : {},
  368. "2016" : {
  369. "filter" : {
  370. "term" : {"year" : 2016 }
  371. }
  372. }
  373. }
  374. }
  375. --------------------------------------------------
  376. // CONSOLE
  377. [float]
  378. [[deleting]]
  379. === Delete aliases
  380. The rest endpoint is: `/{index}/_alias/{name}`
  381. where
  382. [horizontal]
  383. `index`:: `* | _all | glob pattern | name1, name2, …`
  384. `name`:: `* | _all | glob pattern | name1, name2, …`
  385. Alternatively you can use the plural `_aliases`. Example:
  386. [source,js]
  387. --------------------------------------------------
  388. DELETE /logs_20162801/_alias/current_day
  389. --------------------------------------------------
  390. // CONSOLE
  391. // TEST[continued]
  392. [float]
  393. [[alias-retrieving]]
  394. === Retrieving existing aliases
  395. The get index alias API allows to filter by
  396. alias name and index name. This api redirects to the master and fetches
  397. the requested index aliases, if available. This api only serialises the
  398. found index aliases.
  399. Possible options:
  400. [horizontal]
  401. `index`::
  402. The index name to get aliases for. Partial names are
  403. supported via wildcards, also multiple index names can be specified
  404. separated with a comma. Also the alias name for an index can be used.
  405. `alias`::
  406. The name of alias to return in the response. Like the index
  407. option, this option supports wildcards and the option the specify
  408. multiple alias names separated by a comma.
  409. `ignore_unavailable`::
  410. What to do if an specified index name doesn't
  411. exist. If set to `true` then those indices are ignored.
  412. The rest endpoint is: `/{index}/_alias/{alias}`.
  413. [float]
  414. ==== Examples:
  415. All aliases for the index `logs_20162801`:
  416. [source,js]
  417. --------------------------------------------------
  418. GET /logs_20162801/_alias/*
  419. --------------------------------------------------
  420. // CONSOLE
  421. // TEST[continued]
  422. Response:
  423. [source,js]
  424. --------------------------------------------------
  425. {
  426. "logs_20162801" : {
  427. "aliases" : {
  428. "2016" : {
  429. "filter" : {
  430. "term" : {
  431. "year" : 2016
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }
  438. --------------------------------------------------
  439. // TESTRESPONSE
  440. All aliases with the name 2016 in any index:
  441. [source,js]
  442. --------------------------------------------------
  443. GET /_alias/2016
  444. --------------------------------------------------
  445. // CONSOLE
  446. // TEST[continued]
  447. Response:
  448. [source,js]
  449. --------------------------------------------------
  450. {
  451. "logs_20162801" : {
  452. "aliases" : {
  453. "2016" : {
  454. "filter" : {
  455. "term" : {
  456. "year" : 2016
  457. }
  458. }
  459. }
  460. }
  461. }
  462. }
  463. --------------------------------------------------
  464. // TESTRESPONSE
  465. All aliases that start with 20 in any index:
  466. [source,js]
  467. --------------------------------------------------
  468. GET /_alias/20*
  469. --------------------------------------------------
  470. // CONSOLE
  471. // TEST[continued]
  472. Response:
  473. [source,js]
  474. --------------------------------------------------
  475. {
  476. "logs_20162801" : {
  477. "aliases" : {
  478. "2016" : {
  479. "filter" : {
  480. "term" : {
  481. "year" : 2016
  482. }
  483. }
  484. }
  485. }
  486. }
  487. }
  488. --------------------------------------------------
  489. // TESTRESPONSE
  490. There is also a HEAD variant of the get indices aliases api to check if
  491. index aliases exist. The indices aliases exists api supports the same
  492. option as the get indices aliases api. Examples:
  493. [source,js]
  494. --------------------------------------------------
  495. HEAD /_alias/2016
  496. HEAD /_alias/20*
  497. HEAD /logs_20162801/_alias/*
  498. --------------------------------------------------
  499. // CONSOLE
  500. // TEST[continued]