Search code examples
elasticsearchnest

NEST - Atomically change an alias to point to another index


Elasticsearch supports renaming an alias atomically, see here:

Renaming an alias is a simple remove then add operation within the same API. This operation is atomic, no need to worry about a short period of time where the alias does not point to an index.

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test1", "alias" : "alias2" } }
    ]
}'

With NEST, the same can be achieved via Client.Rename.

Turns out, it's also possible to atomically update an alias to point to a different index:

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}'

Any way to do the latter directly in NEST?

Right now, I'm using Client.RemoveAlias followed by Client.Alias which is not atomic.

UPDATE: turns out, it's possible to do this by POSTing raw JSON via client.Raw.IndicesUpdateAliasesPost, but I still wonder if there's an easier way. If not, I'm planning to add this to NEST myself.


Solution

  • The NEST client currently supports this functionality. Please use the Client.Swap method.