Briefly, this error occurs when Elasticsearch is unable to add a filter to an alias due to issues like incorrect filter syntax, non-existent fields in the filter, or insufficient permissions. To resolve this, ensure the filter syntax is correct and the fields used in the filter exist in the index. Also, check the user permissions to ensure they have the rights to add a filter to an alias. If the problem persists, consider checking the Elasticsearch logs for more detailed error information.
This guide will help you check for common problems that cause the log ” [{}] failed to add alias [{}]; filter [{}] ” to appear. To understand the issues related to this log, read the explanation below about the following Elasticsearch concepts: alias, cluster, filter and indices.
Quick links:
- Overview – Data Stream Aliases and Index Aliases
- Creating and removing aliases
- Alias use cases
- Notes and common problems
Overview
There are two types of aliases:
- Data Stream Aliases: An alias for a data stream refers to one or more data streams. The names of data streams will be referred to by data stream aliases. In the cluster state, data stream aliases are kept distinct from data streams.
- Index Aliases: An index alias points to one or more indices.
The master node manages the cluster state, which includes aliases.
Creating and removing aliases
Creating an alias on a single index:
POST /_aliases?pretty { "actions": [ { "add": { "index": "index_1", "alias": "alias_1" } } ] }
Creating an alias that is tied to more than one index:
POST /_aliases?pretty { "actions": [ { "add": { "index": "index_1", "alias": "alias_1" } }, { "add": { "index": "index_2", "alias": "alias_1" } } ] }
Listing out all of the available aliases in an Elasticsearch cluster:
GET _cat/aliases
Removing an alias:
POST /_aliases?pretty { "actions": [ { "remove": { "index": "index_2", "alias": "alias_1" } } ] }
Alias use cases
Aliases are used for multiple purposes such as to search across more than one index with a single name, perform the reindexing process with zero downtime and query data based on predefined filters. Below are 6 different use cases for aliases.
1. Filter-based aliases to limit access to data
One use case is making a filter-based alias, which is quite useful when you need to limit access to data. When a query is executed, an alias can apply a filter automatically.
For example, consider an index named `opster-idx`, having an alias that points to the groups that contain the `opster` company, so you can create an alias that handles this filtering automatically as shown in the steps below.
Index documents:
PUT /opster-idx/_doc/1 { "title": "Taking Care of Your Entire Search Operation", "company": "Opster" }
PUT /opster-idx/_doc/2 { "title": "Streaming service", "company": "XYZ" }
Add the query to the actions in the `filter` param to create a filter alias. The query here is used to limit the documents that the alias can access.
POST /_aliases?pretty { "actions": [ { "add": { "index": "opster-idx", "alias": "opster-alias", "filter": { "term": { "company": "opster" } } } } ] }
While you perform a match-all query on `opster-alias`, only the documents that match the term query (which was added when building filter alias, i.e. documents with company name equal to `opster`) will appear in the search results.
Search query:
GET opster-alias/_search { "query":{ "match_all":{} } }
Search response:
"hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [ { "_index": "opster-idx", "_id": "1", "_score": 1.0, "_source": { "title": "Taking Care of Your Entire Search Operation", "company": "Opster" } } ] }
Now if you try to query those documents that have the company name, “XYZ”, as follows:
GET opster-alias/_search { "query": { "match": { "company": "XYZ" } } }
The search response will be:
"hits": { "total": { "value": 0, "relation": "eq" }, "max_score": null, "hits": [] }
2. Combining routing with aliases
When searching and indexing, the following example will filter out the company `opster` and add `1` to the route to limit where searches are done.
Routing is a string value that is used to route indexing and search operations to a specific shard.
POST /_aliases?pretty { "actions": [ { "add": { "index": "index_1", "alias": "alias_2", "filter": { "term": { "company": "opster" } }, "routing": "1" } } ] }
3. Transitioning to new indices
You can use aliases when your application needs to seamlessly transition from an old index to a new index with no downtime.
4. Creating sliding windows into distinct indices
For example, if you construct daily indices for your data, you might wish to establish an alias named `last-7-days` to produce a sliding window of the data from the previous seven days. Then, each day, when you create a new daily index, you may add it to the alias and delete the 8-day old index at the same time.
5. Aliases and ILM for updating or deleting documents
You can use an index alias and index template with ILM to manage and roll over the alias’s indices if you need to update or delete existing documents across numerous indices frequently.
6. Querying data from a frozen index
When using ILM, If you still need to query data from a frozen index, you can use the alias to do so. Instead of searching for data directly through that index, you may run a search query on the alias, which will increase performance and allow you to get a response with fewer resources.
Notes
- An Alias cannot be used for the indexing process if it points to more than one index. If attempted, Elasticsearch will throw an exception.
- Deleting an alias does not delete the actual index.
Common problems
When you try to index a document into an alias that points to more than one index, Elasticsearch returns an error because it doesn’t know which concrete index the document should be indexed to.
You will get the following error message:
{ "error" : { "root_cause" : [ { "type" : "illegal_argument_exception", "reason" : "no write index is defined for alias [my-alias]. The write index may be explicitly disabled using is_write_index=false or the alias points to multiple indices without one being designated as a write index" } ], "type" : "illegal_argument_exception", "reason" : "no write index is defined for alias [my-alias]. The write index may be explicitly disabled using is_write_index=false or the alias points to multiple indices without one being designated as a write index" }, "status" : 400 }
For a full troubleshooting guide on how to resolve this error, see here.
Overview
An Elasticsearch cluster consists of a number of servers (nodes) working together as one. Clustering is a technology which enables Elasticsearch to scale up to hundreds of nodes that together are able to store many terabytes of data and respond coherently to large numbers of requests at the same time.
Search or indexing requests will usually be load-balanced across the Elasticsearch data nodes, and the node that receives the request will relay requests to other nodes as necessary and coordinate the response back to the user.
Notes and good things to know
The key elements to clustering are:
Cluster State – Refers to information about which indices are in the cluster, their data mappings and other information that must be shared between all the nodes to ensure that all operations across the cluster are coherent.
Master Node – Each cluster must elect a single master node responsible for coordinating the cluster and ensuring that each node contains an up-to-date copy of the cluster state.
Cluster Formation – Elasticsearch requires a set of configurations to determine how the cluster is formed, which nodes can join the cluster, and how the nodes collectively elect a master node responsible for controlling the cluster state. These configurations are usually held in the elasticsearch.yml config file, environment variables on the node, or within the cluster state.
Node Roles – In small clusters it is common for all nodes to fill all roles; all nodes can store data, become master nodes or process ingestion pipelines. However as the cluster grows, it is common to allocate specific roles to specific nodes in order to simplify configuration and to make operation more efficient. In particular, it is common to define a limited number of dedicated master nodes.
Replication – Data may be replicated across a number of data nodes. This means that if one node goes down, data is not lost. It also means that a search request can be dealt with by more than one node.
Common problems
Many Elasticsearch problems are caused by operations which place an excessive burden on the cluster because they require an excessive amount of information to be held and transmitted between the nodes as part of the cluster state. For example:
- Shards too small
- Too many fields (field explosion)
Problems may also be caused by inadequate configurations causing situations where the Elasticsearch cluster is unable to safely elect a Master node. This situation is discussed further in:
Backups
Because Elasticsearch is a clustered technology, it is not sufficient to have backups of each node’s data directory. This is because the backups will have been made at different times and so there may not be complete coherency between them. As such, the only way to backup an Elasticsearch cluster is through the use of snapshots, which contain the full picture of an index at any one time.
Cluster resilience
When designing an Elasticsearch cluster, it is important to think about cluster resilience. In particular – what happens when a single node goes down? And for larger clusters where several nodes may share common services such as a network or power supply – what happens if that network or power supply goes down? This is where it is useful to ensure that the master eligible nodes are spread across availability zones, and to use shard allocation awareness to ensure that shards are spread across different racks or availability zones in your data center.
Overview
When a query is executed, Elasticsearch by default calculates the relevance score of the matching documents. But in some conditions, it does not require scores to be calculated. For instance, if a document falls in the range of two given timestamps or if a document contains a given list of tags. For all these Yes/No criteria, also known as structured search, a filter clause is used.
When it is not desired or not necessary to compute scores, filters should be used instead of queries, as frequently used filters can be cached automatically by Elasticsearch to improve performance.
There are multiple ways to specify filters, such as when using the `filter` and `must_not` parameters of the `bool` query, the `filter` parameter of the `constant_score` query or the `filter` aggregation.
What it is used for
When a query is executed, Elasticsearch by default calculates the relevance score of the matching documents. But in some conditions it does not require scores to be calculated, for instance if a document falls in the range of two given timestamps. For all these Yes/No criteria, a filter clause is used.
Examples
To return all the documents of a given index that fall between a date range, we can use the `range` filter, as shown below:
GET my_index/_search { "query": { "bool": { "filter": [ { "range": { "created_at": { "gte": "2020-01-01", "lte": "2020-01-10" } } } ] } } }
To retrieve all the documents that contain at least one tag from a given list, we can use the `terms` filter, as shown below:
GET my_index/_search { "query": { "bool": { "filter": [ { "terms": { "tags": ["tag1", "tag2", "tag3"] } } ] } } }
To retrieve all the documents that contain a given field having a non-null value, we can use the `exists` filter, as shown below:
GET my_index/_search { "query": { "bool": { "filter": [ { "exists": { "field": "field_name" } } ] } } }
There are many other filters that we can use in order to reduce the document set that needs to be scored, such as `fuzzy`, `prefix`, `wildcard`, `regexp`, `script`, and many more.
It is also worth noting that filters can be combined since the `bool/filter` and `bool/must_not` parameters are arrays. In the example below, we retrieve all documents falling within a data range, containing a list of tags and not having a specific field:
GET my_index/_search { "query": { "bool": { "filter": [ { "range": { "created_at": { "gte": "2020-01-01", "lte": "2020-01-10" } } }, { "terms": { "tags": ["tag1", "tag2", "tag3"] } } ], "must_not": [ { "exists": { "field": "field_name" } } ] } } }
Notes
- Queries are used to find out how relevant a document is to a particular query by calculating a score for each document, whereas filters are used to match certain criteria and are cacheable to enable faster execution.
- Filters do not contribute to scoring and thus are faster to execute.
- There are major changes introduced in Elasticsearch version 2.x onward related to how query and filters are written and performed internally and each newer version comes with its load of new improvements.
Common problems
- The most common problem with filters is incorrect use inside the query. If filters are not used correctly, query performance can be significantly affected. So filters must be used wherever there is scope of not calculating the score.
- Another problem often arises when using date range filters, if “now” is used to represent the current time. It has to be noted that “now” is continuously changing the timestamp and thus Elasticsearch cannot use caching of the response since the data set will keep changing.
Log Context
Log “[{}] failed to add alias [{}]; filter [{}]” classname is IndicesClusterStateService.java.
We extracted the following from Elasticsearch source code for those seeking an in-depth context :
} newAliases.put(alias; indexAliasesService.create(alias; filter)); } } } catch (Throwable e) { logger.warn("[{}] failed to add alias [{}]; filter [{}]"; e; index; alias; filter); } } indexAliasesService.addAll(newAliases); }