How to use index aliases in Elasticsearch

Giving an index an alias in elasticsearch can solve the problem of seamless switching between two indexes very elegantly. This function is very useful in some scenarios.

For example, in the core commodity index library of e-commerce, in addition to real-time incremental data, the index needs to be rebuilt every day to avoid inconsistency between the data in the index and the data in the db. Because the index is divided into shards, it is necessary to replace the shards one by one in full. , until all shards are replaced, the reconstruction can be declared successful. The whole process is actually quite risky. Although only one shard is replaced at a time to minimize the risk, if there is a problem with the reconstruction of the third or fourth shard, the entire index may have to be rolled back. This problem is actually an index The aliasing problem can be solved more elegantly.

The old index is called a and the new index is called b. They have a common alias c, and the index name of the dao layer query is also c. When the new full index b is rebuilt, it is only necessary to remove the relationship between the old index a and the alias c. Then add the relationship between the new index b and the alias c, you can complete the seamless switch, the middle is unaware to the user, if there is a problem with b, you can re-release the relationship of b and restore a at any time, which completes the so-called The rollback operation is very simple and elegant.

In es, index aliases is like a soft connection. It can map one or more indexes and provides very flexible features. Using it, we can do:

(1) Seamlessly switch one index to another in a running es cluster

(2) Grouping multiple indexes, such as indexes created by month, we can construct an index of the last 3 months through aliases

(3) Querying part of the data in an index constitutes a database-like view (views)

There are two api commands for manipulating index aliases in es:

_alias 执行单个别名操作

_aliases 原子的执行多个别名操作

how to use?

Suppose we have two indexes, my_index_v1 and my_index_v2. Now we want to achieve seamless switching through index aliases. Their external index aliases are my_index.

First we create the first old index and add aliases to you

PUT /my_index_v1   //构建索引

PUT /my_index_v1/_alias/my_index   //给索引添加别名

After the creation is complete, we can query their relationship:

GET /*/_alias/my_index  //查某个别名映射的所有index


GET /my_index_v1/_alias/* //查询某个index拥有的别名

The returned results are as follows:

{
    "my_index_v1" : {
        "aliases" : {
            "my_index" : { }
        }
    }
}

Now we build the new index:

PUT /my_index_v2   //构建索引

After the new index is built, we can execute the switch operation command:

POST /_aliases
{
    "actions": [
        { "remove": { "index": "my_index_v1", "alias": "my_index" }},
        { "add":    { "index": "my_index_v2", "alias": "my_index" }}
    ]
}

The above operations are performed sequentially. First contact the alias of the old index, and then add a new alias to the new index, so that the index will not be switched transparently, the user will not feel any changes, and there is no need to stop the operation.

Let's see how it works in the java api:

(1) Add an alias

  client.admin().indices().prepareAliases().addAlias("my_index_v1","my_index");

(2) Remove aliases

        client.admin().indices().prepareAliases().removeAlias("my_index_v1","my_index");

(3) Delete an alias and then add another

client.admin().indices().prepareAliases().removeAlias("my_index_v1","my_index")
                .addAlias("my_index_v2","my_index").execute().actionGet();

When the alias is added, we can use it directly in delete, search, and update:

 SearchRequestBuilder search=client.prepareSearch("my_index");

One thing to note is that after using an alias, the value of type type does not need to be filled in. If you fill in es, an exception will be thrown, because it thinks that your alias is a new index, so we only need to write the index name, es The server knows its type.

Summarize:

This article introduces the function and function of aliases in es and explains how to use aliases. If our index is not sure how to use it in the future, it is a good choice to add an alias to the index.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325449954&siteId=291194637