aliases.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. [[alias-adding]]
  215. === Add a single alias
  216. An alias can also be added with the endpoint
  217. `PUT /{index}/_alias/{name}`
  218. where
  219. [horizontal]
  220. `index`:: The index the alias refers to. Can be any of `* | _all | glob pattern | name1, name2, …`
  221. `name`:: The name of the alias. This is a required option.
  222. `routing`:: An optional routing that can be associated with an alias.
  223. `filter`:: An optional filter that can be associated with an alias.
  224. You can also use the plural `_aliases`.
  225. [float]
  226. ==== Examples:
  227. Adding time based alias::
  228. +
  229. --
  230. [source,js]
  231. --------------------------------------------------
  232. PUT /logs_201305/_alias/2013
  233. --------------------------------------------------
  234. // CONSOLE
  235. // TEST[s/^/PUT logs_201305\n/]
  236. --
  237. Adding a user alias::
  238. +
  239. --
  240. First create the index and add a mapping for the `user_id` field:
  241. [source,js]
  242. --------------------------------------------------
  243. PUT /users
  244. {
  245. "mappings" : {
  246. "user" : {
  247. "properties" : {
  248. "user_id" : {"type" : "integer"}
  249. }
  250. }
  251. }
  252. }
  253. --------------------------------------------------
  254. // CONSOLE
  255. Then add the alias for a specific user:
  256. [source,js]
  257. --------------------------------------------------
  258. PUT /users/_alias/user_12
  259. {
  260. "routing" : "12",
  261. "filter" : {
  262. "term" : {
  263. "user_id" : 12
  264. }
  265. }
  266. }
  267. --------------------------------------------------
  268. // CONSOLE
  269. // TEST[continued]
  270. --
  271. [float]
  272. [[alias-index-creation]]
  273. === Aliases during index creation
  274. Aliases can also be specified during <<create-index-aliases,index creation>>:
  275. [source,js]
  276. --------------------------------------------------
  277. PUT /logs_20162801
  278. {
  279. "mappings" : {
  280. "type" : {
  281. "properties" : {
  282. "year" : {"type" : "integer"}
  283. }
  284. }
  285. },
  286. "aliases" : {
  287. "current_day" : {},
  288. "2016" : {
  289. "filter" : {
  290. "term" : {"year" : 2016 }
  291. }
  292. }
  293. }
  294. }
  295. --------------------------------------------------
  296. // CONSOLE
  297. [float]
  298. [[deleting]]
  299. === Delete aliases
  300. The rest endpoint is: `/{index}/_alias/{name}`
  301. where
  302. [horizontal]
  303. `index`:: `* | _all | glob pattern | name1, name2, …`
  304. `name`:: `* | _all | glob pattern | name1, name2, …`
  305. Alternatively you can use the plural `_aliases`. Example:
  306. [source,js]
  307. --------------------------------------------------
  308. DELETE /logs_20162801/_alias/current_day
  309. --------------------------------------------------
  310. // CONSOLE
  311. // TEST[continued]
  312. [float]
  313. [[alias-retrieving]]
  314. === Retrieving existing aliases
  315. The get index alias API allows to filter by
  316. alias name and index name. This api redirects to the master and fetches
  317. the requested index aliases, if available. This api only serialises the
  318. found index aliases.
  319. Possible options:
  320. [horizontal]
  321. `index`::
  322. The index name to get aliases for. Partial names are
  323. supported via wildcards, also multiple index names can be specified
  324. separated with a comma. Also the alias name for an index can be used.
  325. `alias`::
  326. The name of alias to return in the response. Like the index
  327. option, this option supports wildcards and the option the specify
  328. multiple alias names separated by a comma.
  329. `ignore_unavailable`::
  330. What to do if an specified index name doesn't
  331. exist. If set to `true` then those indices are ignored.
  332. The rest endpoint is: `/{index}/_alias/{alias}`.
  333. [float]
  334. ==== Examples:
  335. All aliases for the index users:
  336. [source,js]
  337. --------------------------------------------------
  338. GET /logs_20162801/_alias/*
  339. --------------------------------------------------
  340. // CONSOLE
  341. // TEST[continued]
  342. Response:
  343. [source,js]
  344. --------------------------------------------------
  345. {
  346. "logs_20162801" : {
  347. "aliases" : {
  348. "2016" : {
  349. "filter" : {
  350. "term" : {
  351. "year" : 2016
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. --------------------------------------------------
  359. // TESTRESPONSE
  360. All aliases with the name 2016 in any index:
  361. [source,js]
  362. --------------------------------------------------
  363. GET /_alias/2016
  364. --------------------------------------------------
  365. // CONSOLE
  366. // TEST[continued]
  367. Response:
  368. [source,js]
  369. --------------------------------------------------
  370. {
  371. "logs_20162801" : {
  372. "aliases" : {
  373. "2016" : {
  374. "filter" : {
  375. "term" : {
  376. "year" : 2016
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. --------------------------------------------------
  384. // TESTRESPONSE
  385. All aliases that start with 20 in any index:
  386. [source,js]
  387. --------------------------------------------------
  388. GET /_alias/20*
  389. --------------------------------------------------
  390. // CONSOLE
  391. // TEST[continued]
  392. Response:
  393. [source,js]
  394. --------------------------------------------------
  395. {
  396. "logs_20162801" : {
  397. "aliases" : {
  398. "2016" : {
  399. "filter" : {
  400. "term" : {
  401. "year" : 2016
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }
  408. --------------------------------------------------
  409. // TESTRESPONSE
  410. There is also a HEAD variant of the get indices aliases api to check if
  411. index aliases exist. The indices aliases exists api supports the same
  412. option as the get indices aliases api. Examples:
  413. [source,js]
  414. --------------------------------------------------
  415. HEAD /_alias/2016
  416. HEAD /_alias/20*
  417. HEAD /logs_20162801/_alias/*
  418. --------------------------------------------------
  419. // CONSOLE
  420. // TEST[continued]