aliases.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 allow to alias 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 aliases 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. An alias can also be removed, for example:
  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 are 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. [float]
  96. [[filtered]]
  97. === Filtered Aliases
  98. Aliases with filters provide an easy way to create different "views" of
  99. the same index. The filter can be defined using Query DSL and is applied
  100. to all Search, Count, Delete By Query and More Like This operations with
  101. this alias.
  102. To create a filtered alias, first we need to ensure that the fields already
  103. exist in the mapping:
  104. [source,js]
  105. --------------------------------------------------
  106. PUT /test1
  107. {
  108. "mappings": {
  109. "type1": {
  110. "properties": {
  111. "user" : {
  112. "type": "keyword"
  113. }
  114. }
  115. }
  116. }
  117. }
  118. --------------------------------------------------
  119. // CONSOLE
  120. Now we can create an alias that uses a filter on field `user`:
  121. [source,js]
  122. --------------------------------------------------
  123. POST /_aliases
  124. {
  125. "actions" : [
  126. {
  127. "add" : {
  128. "index" : "test1",
  129. "alias" : "alias2",
  130. "filter" : { "term" : { "user" : "kimchy" } }
  131. }
  132. }
  133. ]
  134. }
  135. --------------------------------------------------
  136. // CONSOLE
  137. // TEST[continued]
  138. [float]
  139. [[aliases-routing]]
  140. ==== Routing
  141. It is possible to associate routing values with aliases. This feature
  142. can be used together with filtering aliases in order to avoid
  143. unnecessary shard operations.
  144. The following command creates a new alias `alias1` that points to index
  145. `test`. After `alias1` is created, all operations with this alias are
  146. automatically modified to use value `1` for routing:
  147. [source,js]
  148. --------------------------------------------------
  149. POST /_aliases
  150. {
  151. "actions" : [
  152. {
  153. "add" : {
  154. "index" : "test",
  155. "alias" : "alias1",
  156. "routing" : "1"
  157. }
  158. }
  159. ]
  160. }
  161. --------------------------------------------------
  162. // CONSOLE
  163. // TEST[s/^/PUT test\n/]
  164. It's also possible to specify different routing values for searching
  165. and indexing operations:
  166. [source,js]
  167. --------------------------------------------------
  168. POST /_aliases
  169. {
  170. "actions" : [
  171. {
  172. "add" : {
  173. "index" : "test",
  174. "alias" : "alias2",
  175. "search_routing" : "1,2",
  176. "index_routing" : "2"
  177. }
  178. }
  179. ]
  180. }
  181. --------------------------------------------------
  182. // CONSOLE
  183. // TEST[s/^/PUT test\n/]
  184. As shown in the example above, search routing may contain several values
  185. separated by comma. Index routing can contain only a single value.
  186. If a search operation that uses routing alias also has a routing parameter, an
  187. intersection of both search alias routing and routing specified in the
  188. parameter is used. For example the following command will use "2" as a
  189. routing value:
  190. [source,js]
  191. --------------------------------------------------
  192. GET /alias2/_search?q=user:kimchy&routing=2,3
  193. --------------------------------------------------
  194. // CONSOLE
  195. // TEST[continued]
  196. If an index operation that uses index routing alias also has a parent routing, the
  197. parent routing is ignored.
  198. [float]
  199. [[alias-adding]]
  200. === Add a single alias
  201. An alias can also be added with the endpoint
  202. `PUT /{index}/_alias/{name}`
  203. where
  204. [horizontal]
  205. `index`:: The index the alias refers to. Can be any of `* | _all | glob pattern | name1, name2, …`
  206. `name`:: The name of the alias. This is a required option.
  207. `routing`:: An optional routing that can be associated with an alias.
  208. `filter`:: An optional filter that can be associated with an alias.
  209. You can also use the plural `_aliases`.
  210. [float]
  211. ==== Examples:
  212. Adding time based alias::
  213. +
  214. --
  215. [source,js]
  216. --------------------------------------------------
  217. PUT /logs_201305/_alias/2013
  218. --------------------------------------------------
  219. // CONSOLE
  220. // TEST[s/^/PUT logs_201305\n/]
  221. --
  222. Adding a user alias::
  223. +
  224. --
  225. First create the index and add a mapping for the `user_id` field:
  226. [source,js]
  227. --------------------------------------------------
  228. PUT /users
  229. {
  230. "mappings" : {
  231. "user" : {
  232. "properties" : {
  233. "user_id" : {"type" : "integer"}
  234. }
  235. }
  236. }
  237. }
  238. --------------------------------------------------
  239. // CONSOLE
  240. Then add the alias for a specific user:
  241. [source,js]
  242. --------------------------------------------------
  243. PUT /users/_alias/user_12
  244. {
  245. "routing" : "12",
  246. "filter" : {
  247. "term" : {
  248. "user_id" : 12
  249. }
  250. }
  251. }
  252. --------------------------------------------------
  253. // CONSOLE
  254. // TEST[continued]
  255. --
  256. [float]
  257. [[alias-index-creation]]
  258. === Aliases during index creation
  259. Aliases can also be specified during <<create-index-aliases,index creation>>:
  260. [source,js]
  261. --------------------------------------------------
  262. PUT /logs_20162801
  263. {
  264. "mappings" : {
  265. "type" : {
  266. "properties" : {
  267. "year" : {"type" : "integer"}
  268. }
  269. }
  270. },
  271. "aliases" : {
  272. "current_day" : {},
  273. "2016" : {
  274. "filter" : {
  275. "term" : {"year" : 2016 }
  276. }
  277. }
  278. }
  279. }
  280. --------------------------------------------------
  281. // CONSOLE
  282. [float]
  283. [[deleting]]
  284. === Delete aliases
  285. The rest endpoint is: `/{index}/_alias/{name}`
  286. where
  287. [horizontal]
  288. `index`:: `* | _all | glob pattern | name1, name2, …`
  289. `name`:: `* | _all | glob pattern | name1, name2, …`
  290. Alternatively you can use the plural `_aliases`. Example:
  291. [source,js]
  292. --------------------------------------------------
  293. DELETE /logs_20162801/_alias/current_day
  294. --------------------------------------------------
  295. // CONSOLE
  296. // TEST[continued]
  297. [float]
  298. [[alias-retrieving]]
  299. === Retrieving existing aliases
  300. The get index alias api allows to filter by
  301. alias name and index name. This api redirects to the master and fetches
  302. the requested index aliases, if available. This api only serialises the
  303. found index aliases.
  304. Possible options:
  305. [horizontal]
  306. `index`::
  307. The index name to get aliases for. Partially names are
  308. supported via wildcards, also multiple index names can be specified
  309. separated with a comma. Also the alias name for an index can be used.
  310. `alias`::
  311. The name of alias to return in the response. Like the index
  312. option, this option supports wildcards and the option the specify
  313. multiple alias names separated by a comma.
  314. `ignore_unavailable`::
  315. What to do if an specified index name doesn't
  316. exist. If set to `true` then those indices are ignored.
  317. The rest endpoint is: `/{index}/_alias/{alias}`.
  318. [float]
  319. ==== Examples:
  320. All aliases for the index users:
  321. [source,js]
  322. --------------------------------------------------
  323. GET /logs_20162801/_alias/*
  324. --------------------------------------------------
  325. // CONSOLE
  326. // TEST[continued]
  327. Response:
  328. [source,js]
  329. --------------------------------------------------
  330. {
  331. "logs_20162801" : {
  332. "aliases" : {
  333. "2016" : {
  334. "filter" : {
  335. "term" : {
  336. "year" : 2016
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }
  343. --------------------------------------------------
  344. // TESTRESPONSE
  345. All aliases with the name 2016 in any index:
  346. [source,js]
  347. --------------------------------------------------
  348. GET /_alias/2016
  349. --------------------------------------------------
  350. // CONSOLE
  351. // TEST[continued]
  352. Response:
  353. [source,js]
  354. --------------------------------------------------
  355. {
  356. "logs_20162801" : {
  357. "aliases" : {
  358. "2016" : {
  359. "filter" : {
  360. "term" : {
  361. "year" : 2016
  362. }
  363. }
  364. }
  365. }
  366. }
  367. }
  368. --------------------------------------------------
  369. // TESTRESPONSE
  370. All aliases that start with 20 in any index:
  371. [source,js]
  372. --------------------------------------------------
  373. GET /_alias/20*
  374. --------------------------------------------------
  375. // CONSOLE
  376. // TEST[continued]
  377. Response:
  378. [source,js]
  379. --------------------------------------------------
  380. {
  381. "logs_20162801" : {
  382. "aliases" : {
  383. "2016" : {
  384. "filter" : {
  385. "term" : {
  386. "year" : 2016
  387. }
  388. }
  389. }
  390. }
  391. }
  392. }
  393. --------------------------------------------------
  394. // TESTRESPONSE
  395. There is also a HEAD variant of the get indices aliases api to check if
  396. index aliases exist. The indices aliases exists api supports the same
  397. option as the get indices aliases api. Examples:
  398. [source,js]
  399. --------------------------------------------------
  400. HEAD /_alias/2016
  401. HEAD /_alias/20*
  402. HEAD /logs_20162801/_alias/*
  403. --------------------------------------------------
  404. // CONSOLE
  405. // TEST[continued]