aliases.asciidoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. It is an error to index to an alias which points to more than one index.
  57. [float]
  58. [[filtered]]
  59. === Filtered Aliases
  60. Aliases with filters provide an easy way to create different "views" of
  61. the same index. The filter can be defined using Query DSL and is applied
  62. to all Search, Count, Delete By Query and More Like This operations with
  63. this alias. Here is an example:
  64. [source,js]
  65. --------------------------------------------------
  66. curl -XPOST 'http://localhost:9200/_aliases' -d '
  67. {
  68. "actions" : [
  69. {
  70. "add" : {
  71. "index" : "test1",
  72. "alias" : "alias2",
  73. "filter" : { "term" : { "user" : "kimchy" } }
  74. }
  75. }
  76. ]
  77. }'
  78. --------------------------------------------------
  79. [float]
  80. [[aliases-routing]]
  81. ==== Routing
  82. It is possible to associate routing values with aliases. This feature
  83. can be used together with filtering aliases in order to avoid
  84. unnecessary shard operations.
  85. The following command creates a new alias `alias1` that points to index
  86. `test`. After `alias1` is created, all operations with this alias are
  87. automatically modified to use value `1` for routing:
  88. [source,js]
  89. --------------------------------------------------
  90. curl -XPOST 'http://localhost:9200/_aliases' -d '
  91. {
  92. "actions" : [
  93. {
  94. "add" : {
  95. "index" : "test",
  96. "alias" : "alias1",
  97. "routing" : "1"
  98. }
  99. }
  100. ]
  101. }'
  102. --------------------------------------------------
  103. It's also possible to specify different routing values for searching
  104. and indexing operations:
  105. [source,js]
  106. --------------------------------------------------
  107. curl -XPOST 'http://localhost:9200/_aliases' -d '
  108. {
  109. "actions" : [
  110. {
  111. "add" : {
  112. "index" : "test",
  113. "alias" : "alias2",
  114. "search_routing" : "1,2",
  115. "index_routing" : "2"
  116. }
  117. }
  118. ]
  119. }'
  120. --------------------------------------------------
  121. As shown in the example above, search routing may contain several values
  122. separated by comma. Index routing can contain only a single value.
  123. If an operation that uses routing alias also has a routing parameter, an
  124. intersection of both alias routing and routing specified in the
  125. parameter is used. For example the following command will use "2" as a
  126. routing value:
  127. [source,js]
  128. --------------------------------------------------
  129. curl -XGET 'http://localhost:9200/alias2/_search?q=user:kimchy&routing=2,3'
  130. --------------------------------------------------
  131. [float]
  132. [[alias-adding]]
  133. === Add a single alias
  134. An alias can also be added with the endpoint
  135. `PUT /{index}/_alias/{name}`
  136. where
  137. [horizontal]
  138. `index`:: The index the alias refers to. Can be any of `blank | * | _all | glob pattern | name1, name2, …`
  139. `name`:: The name of the alias. This is a required option.
  140. `routing`:: An optional routing that can be associated with an alias.
  141. `filter`:: An optional filter that can be associated with an alias.
  142. You can also use the plural `_aliases`.
  143. [float]
  144. ==== Examples:
  145. Adding time based alias:
  146. [source,js]
  147. --------------------------------------------------
  148. curl -XPUT 'localhost:9200/logs_201305/_alias/2013'
  149. --------------------------------------------------
  150. Adding user alias:
  151. [source,js]
  152. --------------------------------------------------
  153. curl -XPUT 'localhost:9200/users/_alias/user_12' -d '{
  154. "routing" : "12",
  155. "filter" : {
  156. "term" : {
  157. "user_id" : 12
  158. }
  159. }
  160. }'
  161. --------------------------------------------------
  162. [float]
  163. [[alias-index-creation]]
  164. === Aliases during index creation
  165. added[1.1.0]
  166. Aliases can also be specified during <<create-index-aliases,index creation>>:
  167. [source,js]
  168. --------------------------------------------------
  169. curl -XPUT localhost:9200/logs_20142801 -d '{
  170. "aliases" : {
  171. "current_day" : {},
  172. "2014" : {
  173. "filter" : {
  174. "term" : {"year" : 2014 }
  175. }
  176. }
  177. }
  178. }'
  179. --------------------------------------------------
  180. [float]
  181. [[deleting]]
  182. === Delete aliases
  183. The rest endpoint is: `/{index}/_alias/{name}`
  184. where
  185. [horizontal]
  186. `index`:: `* | _all | glob pattern | name1, name2, …`
  187. `name`:: `* | _all | glob pattern | name1, name2, …`
  188. Alternatively you can use the plural `_aliases`. Example:
  189. [source,js]
  190. --------------------------------------------------
  191. curl -XDELETE 'localhost:9200/users/_alias/user_12'
  192. --------------------------------------------------
  193. [float]
  194. [[alias-retrieving]]
  195. === Retrieving existing aliases
  196. The get index alias api allows to filter by
  197. alias name and index name. This api redirects to the master and fetches
  198. the requested index aliases, if available. This api only serialises the
  199. found index aliases.
  200. Possible options:
  201. [horizontal]
  202. `index`::
  203. The index name to get aliases for. Partially names are
  204. supported via wildcards, also multiple index names can be specified
  205. separated with a comma. Also the alias name for an index can be used.
  206. `alias`::
  207. The name of alias to return in the response. Like the index
  208. option, this option supports wildcards and the option the specify
  209. multiple alias names separated by a comma.
  210. `ignore_unavailable`::
  211. What to do is an specified index name doesn't
  212. exist. If set to `true` then those indices are ignored.
  213. The rest endpoint is: `/{index}/_alias/{alias}`.
  214. [float]
  215. ==== Examples:
  216. All aliases for the index users:
  217. [source,js]
  218. --------------------------------------------------
  219. curl -XGET 'localhost:9200/users/_alias/*'
  220. --------------------------------------------------
  221. Response:
  222. [source,js]
  223. --------------------------------------------------
  224. {
  225. "users" : {
  226. "aliases" : {
  227. "user_13" : {
  228. "filter" : {
  229. "term" : {
  230. "user_id" : 13
  231. }
  232. },
  233. "index_routing" : "13",
  234. "search_routing" : "13"
  235. },
  236. "user_14" : {
  237. "filter" : {
  238. "term" : {
  239. "user_id" : 14
  240. }
  241. },
  242. "index_routing" : "14",
  243. "search_routing" : "14"
  244. },
  245. "user_12" : {
  246. "filter" : {
  247. "term" : {
  248. "user_id" : 12
  249. }
  250. },
  251. "index_routing" : "12",
  252. "search_routing" : "12"
  253. }
  254. }
  255. }
  256. }
  257. --------------------------------------------------
  258. All aliases with the name 2013 in any index:
  259. [source,js]
  260. --------------------------------------------------
  261. curl -XGET 'localhost:9200/_alias/2013'
  262. --------------------------------------------------
  263. Response:
  264. [source,js]
  265. --------------------------------------------------
  266. {
  267. "logs_201304" : {
  268. "aliases" : {
  269. "2013" : { }
  270. }
  271. },
  272. "logs_201305" : {
  273. "aliases" : {
  274. "2013" : { }
  275. }
  276. }
  277. }
  278. --------------------------------------------------
  279. All aliases that start with 2013_01 in any index:
  280. [source,js]
  281. --------------------------------------------------
  282. curl -XGET 'localhost:9200/_alias/2013_01*'
  283. --------------------------------------------------
  284. Response:
  285. [source,js]
  286. --------------------------------------------------
  287. {
  288. "logs_20130101" : {
  289. "aliases" : {
  290. "2013_01" : { }
  291. }
  292. }
  293. }
  294. --------------------------------------------------
  295. There is also a HEAD variant of the get indices aliases api to check if
  296. index aliases exist. The indices aliases exists api supports the same
  297. option as the get indices aliases api. Examples:
  298. [source,js]
  299. --------------------------------------------------
  300. curl -XHEAD 'localhost:9200/_alias/2013'
  301. curl -XHEAD 'localhost:9200/_alias/2013_01*'
  302. curl -XHEAD 'localhost:9200/users/_alias/*'
  303. --------------------------------------------------