Using index aliases

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 indices divided by date (that is, 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/).
  • Collecting website contents in several indices (New York Times, The Guardian, ...) for those we want refers as an index alias called sites.

Getting ready

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.

How to do it...

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:

  1. We are reading the aliases, status for all indices via the REST API, so the method will be GET and an example of call is:
            curl -XGET 'http://localhost:9200/_aliases'
    
  2. Giving a response similar to this one:
            { 
                "myindex": { 
                    "aliases": {} 
                }, 
                "test": { 
                    "aliases": {} 
                } 
            } 
    
  3. Aliases can be changed with add and delete commands.
  4. To read an alias for a single index, we use the _alias endpoint:
              curl -XGET 'http://localhost:9200/myindex/_alias'
    

    The result should be as follows:

                    { 
                        "myindex" : { 
                        "aliases" : { 
                        "myalias1" : { } 
                        } 
                      } 
                    }  
    
  5. To add an alias, type the following command:
            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.

  6. To delete an alias, type the following command:
              curl -XDELETE 'http://localhost:9200/myindex/_alias/myalias1'
    

    The result should be:

                {"acknowledged":true}
    

The delete action removed myindex from the myalias1 alias.

How it works...

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.

There's more...

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:

  • A filter to select only documents that match a field user with a user_1 term.
  • A list and a routing key to select the shards to be used during a search.
  • A routing key to be used during indexing. The routing value is used to modify the destination shard of the document.

Note

The search_routing parameter allows multi value routing keys. The index_routing parameter is single value only.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.72.245