Real-world applications have a lot of indices and queries that span on more indices. This scenario requires defining all the indices names on which queries; aliases allow grouping them in a common name.
Some common scenarios of this usage are as follows:
log_YYMMDD
) for which we want to create an alias for the last week, the last month, today, yesterday, and so on. This pattern is commonly used in log applications such as Logstash (http://logstash.net/).You need an up-and-running Elasticsearch installation, as used in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.
To execute curl
via the command line, you need to install curl
for your operative system.
The URL format for control aliases is:
http://<server>/_aliases
http://<server>/<index>/_alias/<alias_name>
For managing the index aliases, we will perform the steps given as follows:
GET
and an example of call is:curl -XGET 'http://localhost:9200/_aliases'
{ "myindex": { "aliases": {} }, "test": { "aliases": {} } }
_alias
endpoint:curl -XGET 'http://localhost:9200/myindex/_alias'
The result should be as follows:
{ "myindex" : { "aliases" : { "myalias1" : { } } } }
curl -XPUT 'http://localhost:9200/myindex/_alias/myalias1'
The result should be as follows:
{"acknowledged":true}
This action adds the myindex
index to the myalias1
alias.
curl -XDELETE 'http://localhost:9200/myindex/_alias/myalias1'
The result should be:
{"acknowledged":true}
The delete action removed myindex
from the myalias1
alias.
Elasticsearch, during search operations, automatically expands the alias, so the required indices are selected.
The alias metadata is kept in the cluster state. When an alias is added/deleted, all the changes are propagated to all the cluster nodes.
Aliases are mainly functional structures to simply manage indices when data is stored in multiple indices.
Aliases can also be used to define a filter and routing parameter.
Filters are automatically added to the query to filter out data. Routing via an alias allows us to control which shards to hit during searching and indexing.
An example of this call is:
curl -XPOST 'http://localhost:9200/myindex/_aliases/user1alias' -d ' { "filter" : { "term" : { "user" : "user_1" } }, "search_routing" : "1,2", "index_routing" : "2" }'
In this case, we are adding a new alias, user1alias, to a myindex
index, adding:
user_1
term.18.225.72.245