Loc Ann
Loc Ann

Reputation: 475

Changing alias in ElasticSearch returns 200 and acknowledged but does not change alias

Using elasticsearch 8.4.3 with Java 17 and a cluster of 3 nodes where 3 are master eligible, we start with following situation:

We then start a job that creates a new index products-2023-01-12-1520 and at the end using elastic-rest-client on client side and alias API, we make this call:

At 2023-01-12 16:27:26,893:

POST /_aliases
{"actions":[
   {
    "remove": { 
       "alias":"current-products",
       "index":"products-*"
    }
   },
   { 
    "add":{
       "alias":"current-products",
       "index":"products-2023-01-12-1520"}
    }
]}

And we get the following response 26 millis after with HTTP response code 200:

{"acknowledged":true}

But looking at what we end up with, we still have old index with current-products alias.

I don't understand why it happens, and it does not happen 100% of the time (it happened 2 times out of around 10 indexations). Is it a known bug ? or a regular behaviour ?

Edit for @warkolm:

GET /_cat/aliases?v before indexation as of now:

alias               index                       filter routing.index routing.search is_write_index
current-products    products-2023-01-13-1510    -      -             -              -

Upvotes: 6

Views: 1829

Answers (1)

Prescol
Prescol

Reputation: 627

It appears that there might be an issue with the way you are updating the alias. When you perform a POST request to the _aliases endpoint with the "remove" and "add" actions, Elasticsearch will update the alias based on the current state of the indices at the time the request is executed.

However, it is possible that there are other processes or actions that are also modifying the indices or aliases at the same time, and this can cause conflicts or inconsistencies. Additionally, when you use the wildcard character (*) in the "index" field of the "remove" action, it will remove the alias from all indices that match the pattern, which may not be the intended behavior.

To avoid this issue, you could try using the Indices Aliases API instead of the _aliases endpoint. This API allows you to perform atomic updates on aliases, which means that the alias will only be updated if all actions succeed, and will roll back if any of the actions fail. Additionally, instead of using the wildcard character, you can explicitly specify the index that you want to remove the alias from.

Here is an example of how you could use the Indices Aliases API to update the alias:

POST /_aliases
{
    "actions": [
        { "remove": { "index": "products-2023-01-12-0900", "alias": "current-products" } },
        { "add": { "index": "products-2023-01-12-1520", "alias": "current-products" } }
    ]
}

This way, the alias will only be removed from the specific index "products-2023-01-12-0900" and added to the specific index "products-2023-01-12-1520". This can help avoid any conflicts or inconsistencies that may be caused by other processes or actions that are modifying the indices or aliases at the same time.

Additionally, it is recommended to use a version of elasticsearch that is equal or greater than 8.4.3, as it has many bug fixes that might be the cause of the issue you are facing.

In conclusion, the issue you are encountering may not be a known bug but it's a regular behavior if multiple processes are modifying the indices or aliases at the same time, and using the Indices Aliases API and specifying the exact index to remove or add the alias can help avoid this issue.

Upvotes: 1

Related Questions