When changing the index of an Elasticsearch alias via API, do write operations immediately point to the right index after the response? - elasticsearch

Let's say I got the alias car-alias pointing to car-index-1. I now want car-alias to point to car-index-2.
I therefore perform the following POST request to the Aliases API:
{
"actions": [
{
"remove": {
"index": "car-index-1",
"alias": "car-alias"
}
},
{
"add": {
"index": "car-index-2",
"alias": "car-alias"
}
}
]
}
I receive the following response:
{
"acknowledged": true
}
Can I now immediately index data into the car-alias and it ends up in the car-index-2?
Or does the "acknowledged": true response not guarantee that write operations point to the right index immediately?

Yes, the alias is changed atomically and will point to car-index-2 immediately when the call returns.
As stated in the documentation "...during the swap, the alias has no downtime and never points to both streams at the same time."

In addition to #Val's answer:
Since in your case, the alias only points to one index, the "next" index is automatically set as the write index.
From the docs regarding the is_write_index option of the add action:
is_write_index (Optional, Boolean)
If true, sets the write index or data stream for the alias.
If an alias points to multiple indices or data streams and
is_write_index isn’t set, the alias rejects write requests. If an
index alias points to one index and is_write_index isn’t set, the
index automatically acts as the write index. [...]
Only the add action supports this parameter.

Related

Reindexing more than 10k documents in Elasticsearch

Let's say I have an index- A. It contains 26k documents. Now I want to change a field status with type as Keyword. As I can't change A's status field type which is already existing, I will create a new index: B with my setting my desired type.
I followed reindex API:
POST _reindex
{
"source": {
"index": "A",
"size": 10000
},
"dest": {
"index": "B",
"version_type": "external"
}
}.
But the problem is, here I can migrate only 10k docs. How to copy the rest?
How can I copy all the docs without losing any?
delete the size: 10000 and problem will be solved.
by the way the size field in Reindex API means that what batch size elasticsearch should use to fetch and reindex docs every time. by default the batch size is 100. (you thought it means how many document you want to reindex)

2 indexes with same alias but only one is marked as write index

I have 2 indexes in Elastic search which are having the same alias name but only for one of them, is_write_index is true. Now, when I try to write to the indexes by alias name, I am getting following error:
Alias [alias_name] has more than one indices associated with it [[indexname1, indexname2]], can't execute a single index op
This is something I am doing which is failing:
let doc = {
script: {
source: ...,
lang: "painless",
params: {
...document
}
},
upsert: {
...document
}
};
await client.update({
index: alias_name,
id: docId,
body: doc
});
What can be the issue here? My thinking was if only one of them is marked as write index, it would just write to that and writes should not fail (https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html#aliases-write-index). Am I understanding wrong?
The problem is that an update is a combo operation where the document is first retrieved using GET, then updated using the script and finally reindexed using the index operation.
What doesn't work in your case is the first operation as GET cannot be done over an alias that spans multiple indexes, even if only one of them as the is_write_index flag set to true, there's no guarantee that the document to be updated is actually in that index.

Do you need to delete Elasticsearch aliases?

Can't seem to find a simple yes or no answer to this question.
When you have an index with one or more aliases can you just delete the index without any negative side effects? Will deleting the index also delete the aliases? Should you remove all aliases first before deleting an index?
What is considered best practice?
A simple test provides the answer.
First create an index:
PUT my_index
Then create an alias:
POST _aliases
{
"actions": [
{
"add": {
"index": "my_index",
"alias": "alias1"
}
}
]
}
Verify the alias exists:
GET _aliases # should return the alias named alias1
GET alias1 # should return documents from my_index
Delete the index:
DELETE my_index
Check that the alias is gone too
GET _aliases # should be empty
GET alias1 # should return "no such index"
To sum it up, no you don't need to delete aliases before/after deleting an index. Simply deleting the index will take care of deleting the orphan alias as well.

Why are Elasticsearch aliases not unique

The Elasticsearch documentation describes aliases as feature to reindex data with zero downtime:
Create a new index and index the whole data
Let your alias point to the new index
Delete the old index
This would be a great feature if aliases would be unique but it's possible that one alias points to multiple indexes. Considering that maybe the deletion of the old index fails my application might speak to two indexes which might not be in sync. Even worse: the application doesn't know about that.
Why is it possible to reuse an alias?
It allows you to easily have several indexes that are both used individually and together with other indexes. This is useful for example when having a logging index where sometimes you want to query the most recent (logs-recent alias) and sometimes want to query everything (logs alias). There are probably lots of other use cases but this one pops up as the first for me.
As per the documentation you can send both the remove and add in one request:
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}'
After that succeeds you can remove your old index and if that fails you will just have an extra index taking up some space until its cleaned out.

Is it possible to write to multiple indexes with an ElasticSearch alias?

The ElasticSearch Docs reads:
An alias can also be mapped to more than one index, and when specifying it, the alias will automatically expand to the aliases indices.
But when I try to add an alias to 2 indices and write to both, neither seem to get updated with the document. If I remove one of the aliases, it will write correctly to the alias that still exists.
Fails with multiple write aliases:
$ curl -XGET 'http://localhost:9200/_aliases'
result:
{
"dev_01": {
"aliases": {
"dev_read": {},
"dev_write": {}
}
},
"dev": {
"aliases": {
"dev_write": {}
}
}
}
Works with single alias:
$ curl -XGET 'http://localhost:9200/_aliases'
result:
{
"dev_01": {
"aliases": {
"dev_read": {},
"dev_write": {}
}
},
"dev": {
"aliases": {}
}
}
Does elasticsearch support writing to multiple indices? Are aliases Read-Only if pointed at multiple indices?
the answer is No
So it appears I should have triaged this a beep deeper, but the response my client gets from es is:
ElasticSearchIllegalArgumentException[Alias [dev_write] has more than one indices associated with it [[dev_01, dev]], can't execute a single index op
Just wish the docs were a little more explicit up front, as they confused me a bit
At first seems to imply you can:
The index aliases API allow to alias an index with a name, with all APIs automatically converting the alias name to the actual index name. An alias can also be mapped to more than one index...
Associating an alias with more than one index are simply several add actions...
Further down the page lets you know you can not:
It is an error to index to an alias which points to more than one index.

Resources