aliases.asciidoc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. "properties": {
  127. "user" : {
  128. "type": "keyword"
  129. }
  130. }
  131. }
  132. }
  133. --------------------------------------------------
  134. // CONSOLE
  135. Now we can create an alias that uses a filter on field `user`:
  136. [source,js]
  137. --------------------------------------------------
  138. POST /_aliases
  139. {
  140. "actions" : [
  141. {
  142. "add" : {
  143. "index" : "test1",
  144. "alias" : "alias2",
  145. "filter" : { "term" : { "user" : "kimchy" } }
  146. }
  147. }
  148. ]
  149. }
  150. --------------------------------------------------
  151. // CONSOLE
  152. // TEST[continued]
  153. [float]
  154. [[aliases-routing]]
  155. ===== Routing
  156. It is possible to associate routing values with aliases. This feature
  157. can be used together with filtering aliases in order to avoid
  158. unnecessary shard operations.
  159. The following command creates a new alias `alias1` that points to index
  160. `test`. After `alias1` is created, all operations with this alias are
  161. automatically modified to use value `1` for routing:
  162. [source,js]
  163. --------------------------------------------------
  164. POST /_aliases
  165. {
  166. "actions" : [
  167. {
  168. "add" : {
  169. "index" : "test",
  170. "alias" : "alias1",
  171. "routing" : "1"
  172. }
  173. }
  174. ]
  175. }
  176. --------------------------------------------------
  177. // CONSOLE
  178. // TEST[s/^/PUT test\n/]
  179. It's also possible to specify different routing values for searching
  180. and indexing operations:
  181. [source,js]
  182. --------------------------------------------------
  183. POST /_aliases
  184. {
  185. "actions" : [
  186. {
  187. "add" : {
  188. "index" : "test",
  189. "alias" : "alias2",
  190. "search_routing" : "1,2",
  191. "index_routing" : "2"
  192. }
  193. }
  194. ]
  195. }
  196. --------------------------------------------------
  197. // CONSOLE
  198. // TEST[s/^/PUT test\n/]
  199. As shown in the example above, search routing may contain several values
  200. separated by comma. Index routing can contain only a single value.
  201. If a search operation that uses routing alias also has a routing parameter, an
  202. intersection of both search alias routing and routing specified in the
  203. parameter is used. For example the following command will use "2" as a
  204. routing value:
  205. [source,js]
  206. --------------------------------------------------
  207. GET /alias2/_search?q=user:kimchy&routing=2,3
  208. --------------------------------------------------
  209. // CONSOLE
  210. // TEST[continued]
  211. [float]
  212. [[aliases-write-index]]
  213. ===== Write Index
  214. It is possible to associate the index pointed to by an alias as the write index.
  215. When specified, all index and update requests against an alias that point to multiple
  216. indices will attempt to resolve to the one index that is the write index.
  217. Only one index per alias can be assigned to be the write index at a time. If no write index is specified
  218. and there are multiple indices referenced by an alias, then writes will not be allowed.
  219. It is possible to specify an index associated with an alias as a write index using both the aliases API
  220. and index creation API.
  221. Setting an index to be the write index with an alias also affects how the alias is manipulated during
  222. Rollover (see <<indices-rollover-index, Rollover With Write Index>>).
  223. [source,js]
  224. --------------------------------------------------
  225. POST /_aliases
  226. {
  227. "actions" : [
  228. {
  229. "add" : {
  230. "index" : "test",
  231. "alias" : "alias1",
  232. "is_write_index" : true
  233. }
  234. },
  235. {
  236. "add" : {
  237. "index" : "test2",
  238. "alias" : "alias1"
  239. }
  240. }
  241. ]
  242. }
  243. --------------------------------------------------
  244. // CONSOLE
  245. // TEST[s/^/PUT test\nPUT test2\n/]
  246. In this example, we associate the alias `alias1` to both `test` and `test2`, where
  247. `test` will be the index chosen for writing to.
  248. [source,js]
  249. --------------------------------------------------
  250. PUT /alias1/_doc/1
  251. {
  252. "foo": "bar"
  253. }
  254. --------------------------------------------------
  255. // CONSOLE
  256. // TEST[continued]
  257. The new document that was indexed to `/alias1/_doc/1` will be indexed as if it were
  258. `/test/_doc/1`.
  259. [source,js]
  260. --------------------------------------------------
  261. GET /test/_doc/1
  262. --------------------------------------------------
  263. // CONSOLE
  264. // TEST[continued]
  265. To swap which index is the write index for an alias, the Aliases API can be leveraged to
  266. do an atomic swap. The swap is not dependent on the ordering of the actions.
  267. [source,js]
  268. --------------------------------------------------
  269. POST /_aliases
  270. {
  271. "actions" : [
  272. {
  273. "add" : {
  274. "index" : "test",
  275. "alias" : "alias1",
  276. "is_write_index" : false
  277. }
  278. }, {
  279. "add" : {
  280. "index" : "test2",
  281. "alias" : "alias1",
  282. "is_write_index" : true
  283. }
  284. }
  285. ]
  286. }
  287. --------------------------------------------------
  288. // CONSOLE
  289. // TEST[s/^/PUT test\nPUT test2\n/]
  290. [IMPORTANT]
  291. =====================================
  292. Aliases that do not explicitly set `is_write_index: true` for an index, and
  293. only reference one index, will have that referenced index behave as if it is the write index
  294. until an additional index is referenced. At that point, there will be no write index and
  295. writes will be rejected.
  296. =====================================