aliases.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.
  11. Here is a sample of associating the alias `alias1` with index `test1`:
  12. [source,js]
  13. --------------------------------------------------
  14. curl -XPOST 'http://localhost:9200/_aliases' -d '
  15. {
  16. "actions" : [
  17. { "add" : { "index" : "test1", "alias" : "alias1" } }
  18. ]
  19. }'
  20. --------------------------------------------------
  21. An alias can also be removed, for example:
  22. [source,js]
  23. --------------------------------------------------
  24. curl -XPOST 'http://localhost:9200/_aliases' -d '
  25. {
  26. "actions" : [
  27. { "remove" : { "index" : "test1", "alias" : "alias1" } }
  28. ]
  29. }'
  30. --------------------------------------------------
  31. Renaming an alias is a simple `remove` then `add` operation within the
  32. same API. This operation is atomic, no need to worry about a short
  33. period of time where the alias does not point to an index:
  34. [source,js]
  35. --------------------------------------------------
  36. curl -XPOST 'http://localhost:9200/_aliases' -d '
  37. {
  38. "actions" : [
  39. { "remove" : { "index" : "test1", "alias" : "alias1" } },
  40. { "add" : { "index" : "test1", "alias" : "alias2" } }
  41. ]
  42. }'
  43. --------------------------------------------------
  44. Associating an alias with more than one index are simply several `add`
  45. actions:
  46. [source,js]
  47. --------------------------------------------------
  48. curl -XPOST 'http://localhost:9200/_aliases' -d '
  49. {
  50. "actions" : [
  51. { "add" : { "index" : "test1", "alias" : "alias1" } },
  52. { "add" : { "index" : "test2", "alias" : "alias1" } }
  53. ]
  54. }'
  55. --------------------------------------------------
  56. Alternatively, you can use a glob pattern to associate an alias to
  57. more than one index that share a common name:
  58. [source,js]
  59. --------------------------------------------------
  60. curl -XPOST 'http://localhost:9200/_aliases' -d '
  61. {
  62. "actions" : [
  63. { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
  64. ]
  65. }'
  66. --------------------------------------------------
  67. In this case, the alias is a point-in-time alias that will group all
  68. current indices that match, it will not automatically update as new
  69. indices that match this pattern are added/removed.
  70. It is an error to index to an alias which points to more than one index.
  71. [float]
  72. [[filtered]]
  73. === Filtered Aliases
  74. Aliases with filters provide an easy way to create different "views" of
  75. the same index. The filter can be defined using Query DSL and is applied
  76. to all Search, Count, Delete By Query and More Like This operations with
  77. this alias.
  78. To create a filtered alias, first we need to ensure that the fields already
  79. exist in the mapping:
  80. [source,js]
  81. --------------------------------------------------
  82. curl -XPUT 'http://localhost:9200/test1' -d '{
  83. "mappings": {
  84. "type1": {
  85. "properties": {
  86. "user" : {
  87. "type": "string",
  88. "index": "not_analyzed"
  89. }
  90. }
  91. }
  92. }
  93. }
  94. --------------------------------------------------
  95. Now we can create an alias that uses a filter on field `user`:
  96. [source,js]
  97. --------------------------------------------------
  98. curl -XPOST 'http://localhost:9200/_aliases' -d '{
  99. "actions" : [
  100. {
  101. "add" : {
  102. "index" : "test1",
  103. "alias" : "alias2",
  104. "filter" : { "term" : { "user" : "kimchy" } }
  105. }
  106. }
  107. ]
  108. }'
  109. --------------------------------------------------
  110. [float]
  111. [[aliases-routing]]
  112. ==== Routing
  113. It is possible to associate routing values with aliases. This feature
  114. can be used together with filtering aliases in order to avoid
  115. unnecessary shard operations.
  116. The following command creates a new alias `alias1` that points to index
  117. `test`. After `alias1` is created, all operations with this alias are
  118. automatically modified to use value `1` for routing:
  119. [source,js]
  120. --------------------------------------------------
  121. curl -XPOST 'http://localhost:9200/_aliases' -d '
  122. {
  123. "actions" : [
  124. {
  125. "add" : {
  126. "index" : "test",
  127. "alias" : "alias1",
  128. "routing" : "1"
  129. }
  130. }
  131. ]
  132. }'
  133. --------------------------------------------------
  134. It's also possible to specify different routing values for searching
  135. and indexing operations:
  136. [source,js]
  137. --------------------------------------------------
  138. curl -XPOST 'http://localhost:9200/_aliases' -d '
  139. {
  140. "actions" : [
  141. {
  142. "add" : {
  143. "index" : "test",
  144. "alias" : "alias2",
  145. "search_routing" : "1,2",
  146. "index_routing" : "2"
  147. }
  148. }
  149. ]
  150. }'
  151. --------------------------------------------------
  152. As shown in the example above, search routing may contain several values
  153. separated by comma. Index routing can contain only a single value.
  154. If an operation that uses routing alias also has a routing parameter, an
  155. intersection of both alias routing and routing specified in the
  156. parameter is used. For example the following command will use "2" as a
  157. routing value:
  158. [source,js]
  159. --------------------------------------------------
  160. curl -XGET 'http://localhost:9200/alias2/_search?q=user:kimchy&routing=2,3'
  161. --------------------------------------------------
  162. [float]
  163. [[alias-adding]]
  164. === Add a single alias
  165. An alias can also be added with the endpoint
  166. `PUT /{index}/_alias/{name}`
  167. where
  168. [horizontal]
  169. `index`:: The index the alias refers to. Can be any of `* | _all | glob pattern | name1, name2, …`
  170. `name`:: The name of the alias. This is a required option.
  171. `routing`:: An optional routing that can be associated with an alias.
  172. `filter`:: An optional filter that can be associated with an alias.
  173. You can also use the plural `_aliases`.
  174. [float]
  175. ==== Examples:
  176. Adding time based alias::
  177. +
  178. --
  179. [source,js]
  180. --------------------------------------------------
  181. curl -XPUT 'localhost:9200/logs_201305/_alias/2013'
  182. --------------------------------------------------
  183. --
  184. Adding a user alias::
  185. +
  186. --
  187. First create the index and add a mapping for the `user_id` field:
  188. [source,js]
  189. --------------------------------------------------
  190. curl -XPUT 'localhost:9200/users' -d '{
  191. "mappings" : {
  192. "user" : {
  193. "properties" : {
  194. "user_id" : {"type" : "integer"}
  195. }
  196. }
  197. }
  198. }'
  199. --------------------------------------------------
  200. Then add the alias for a specific user:
  201. [source,js]
  202. --------------------------------------------------
  203. curl -XPUT 'localhost:9200/users/_alias/user_12' -d '{
  204. "routing" : "12",
  205. "filter" : {
  206. "term" : {
  207. "user_id" : 12
  208. }
  209. }
  210. }'
  211. --------------------------------------------------
  212. --
  213. [float]
  214. [[alias-index-creation]]
  215. === Aliases during index creation
  216. Aliases can also be specified during <<create-index-aliases,index creation>>:
  217. [source,js]
  218. --------------------------------------------------
  219. curl -XPUT localhost:9200/logs_20142801 -d '{
  220. "mappings" : {
  221. "type" : {
  222. "properties" : {
  223. "year" : {"type" : "integer"}
  224. }
  225. }
  226. },
  227. "aliases" : {
  228. "current_day" : {},
  229. "2014" : {
  230. "filter" : {
  231. "term" : {"year" : 2014 }
  232. }
  233. }
  234. }
  235. }'
  236. --------------------------------------------------
  237. [float]
  238. [[deleting]]
  239. === Delete aliases
  240. The rest endpoint is: `/{index}/_alias/{name}`
  241. where
  242. [horizontal]
  243. `index`:: `* | _all | glob pattern | name1, name2, …`
  244. `name`:: `* | _all | glob pattern | name1, name2, …`
  245. Alternatively you can use the plural `_aliases`. Example:
  246. [source,js]
  247. --------------------------------------------------
  248. curl -XDELETE 'localhost:9200/users/_alias/user_12'
  249. --------------------------------------------------
  250. [float]
  251. [[alias-retrieving]]
  252. === Retrieving existing aliases
  253. The get index alias api allows to filter by
  254. alias name and index name. This api redirects to the master and fetches
  255. the requested index aliases, if available. This api only serialises the
  256. found index aliases.
  257. Possible options:
  258. [horizontal]
  259. `index`::
  260. The index name to get aliases for. Partially names are
  261. supported via wildcards, also multiple index names can be specified
  262. separated with a comma. Also the alias name for an index can be used.
  263. `alias`::
  264. The name of alias to return in the response. Like the index
  265. option, this option supports wildcards and the option the specify
  266. multiple alias names separated by a comma.
  267. `ignore_unavailable`::
  268. What to do is an specified index name doesn't
  269. exist. If set to `true` then those indices are ignored.
  270. The rest endpoint is: `/{index}/_alias/{alias}`.
  271. [float]
  272. ==== Examples:
  273. All aliases for the index users:
  274. [source,js]
  275. --------------------------------------------------
  276. curl -XGET 'localhost:9200/users/_alias/*'
  277. --------------------------------------------------
  278. Response:
  279. [source,js]
  280. --------------------------------------------------
  281. {
  282. "users" : {
  283. "aliases" : {
  284. "user_13" : {
  285. "filter" : {
  286. "term" : {
  287. "user_id" : 13
  288. }
  289. },
  290. "index_routing" : "13",
  291. "search_routing" : "13"
  292. },
  293. "user_14" : {
  294. "filter" : {
  295. "term" : {
  296. "user_id" : 14
  297. }
  298. },
  299. "index_routing" : "14",
  300. "search_routing" : "14"
  301. },
  302. "user_12" : {
  303. "filter" : {
  304. "term" : {
  305. "user_id" : 12
  306. }
  307. },
  308. "index_routing" : "12",
  309. "search_routing" : "12"
  310. }
  311. }
  312. }
  313. }
  314. --------------------------------------------------
  315. All aliases with the name 2013 in any index:
  316. [source,js]
  317. --------------------------------------------------
  318. curl -XGET 'localhost:9200/_alias/2013'
  319. --------------------------------------------------
  320. Response:
  321. [source,js]
  322. --------------------------------------------------
  323. {
  324. "logs_201304" : {
  325. "aliases" : {
  326. "2013" : { }
  327. }
  328. },
  329. "logs_201305" : {
  330. "aliases" : {
  331. "2013" : { }
  332. }
  333. }
  334. }
  335. --------------------------------------------------
  336. All aliases that start with 2013_01 in any index:
  337. [source,js]
  338. --------------------------------------------------
  339. curl -XGET 'localhost:9200/_alias/2013_01*'
  340. --------------------------------------------------
  341. Response:
  342. [source,js]
  343. --------------------------------------------------
  344. {
  345. "logs_20130101" : {
  346. "aliases" : {
  347. "2013_01" : { }
  348. }
  349. }
  350. }
  351. --------------------------------------------------
  352. There is also a HEAD variant of the get indices aliases api to check if
  353. index aliases exist. The indices aliases exists api supports the same
  354. option as the get indices aliases api. Examples:
  355. [source,js]
  356. --------------------------------------------------
  357. curl -XHEAD -i 'localhost:9200/_alias/2013'
  358. curl -XHEAD -i 'localhost:9200/_alias/2013_01*'
  359. curl -XHEAD -i 'localhost:9200/users/_alias/*'
  360. --------------------------------------------------