An index exists with the same name as the alias – How to solve this Elasticsearch exception

Opster Team

August-23, Version: 6.8-7.13

Briefly, this error occurs when an Elasticsearch alias is being created or updated, but an index already exists with the same name. This is problematic because aliases and indices must have unique names. To resolve this issue, you can either rename the existing index or choose a different name for the alias. Alternatively, you can delete the existing index if it’s no longer needed, but ensure to backup any important data before doing so.

This guide will help you check for common problems that cause the log ” an index exists with the same name as the alias ” to appear. To understand the issues related to this log, read the explanation below about the following Elasticsearch concepts: cluster, metadata, index, alias.

Overview

This error message is Elasticsearch informing you that you tried to create an alias with the same name as an index that already exists. Since 7.14 it will also produce a similar error message if a data stream exists with the same name of the alias you are trying to create.

What it means

When you send a search request to Elasticsearch you usually(*) specify on which collection(s) of documents you want your search to run. You do that by calling the _search API on one of the following targets:

  • An index, which can also be specified as a comma separated list of indices and that also support * as a wildcard operator 
  • An alias, which will be translated to one or more indices
  • A data stream (since 7.9)
  • The special _all placeholder that stands for all indices

An alias is a convenient way to hide many indices and data streams behind a facade, so your application doesn’t have to know the names of every index and list them in the search request. You create an alias by simply giving it a name and a pattern of indices you want this alias to translate into:

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "house-sensors*",
        "alias": "house-sensors"
      }
    }
  ]
}

Since aliases, indices and data streams share the same namespace, Elasticsearch will produce an error if you try to create an alias with a name that collides with any of those other resource types:

PUT some-index/_doc/1
{
  "some_field": "some_value"
}
 
POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "some-index*",
        "alias": "some-index"
      }
    }
  ]
}
 
Response:
 
{
  "error" : {
    "root_cause" : [
      {
        "type" : "invalid_alias_name_exception",
        "reason" : "Invalid alias name [some-index], an index exists with the same name as the alias",
        "index_uuid" : "K75RkLc6StWkkfjtyHbREQ",
        "index" : "some-index"
      }
    ],
    "type" : "invalid_alias_name_exception",
    "reason" : "Invalid alias name [some-index], an index exists with the same name as the alias",
    "index_uuid" : "K75RkLc6StWkkfjtyHbREQ",
    "index" : "some-index"
  },
  "status" : 400
}

If you are running at least Elasticsearch 7.14 the error message will be slightly different:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "invalid_alias_name_exception",
        "reason" : "Invalid alias name [some-index], an index or data stream exists with the same name as the alias",
        "index_uuid" : "K75RkLc6StWkkfjtyHbREQ",
        "index" : "some-index"
      }
    ],
    "type" : "invalid_alias_name_exception",
    "reason" : "Invalid alias name [some-index], an index exists with the same name as the alias",
    "index_uuid" : "K75RkLc6StWkkfjtyHbREQ",
    "index" : "some-index"
  },
  "status" : 400
}

*The _search API actually allows you to send a search request without specifying against which indices you want your search to run. In that case the search request will consider all indices. 

How to resolve it

You should simply choose another alias name, or rename your indices so you can use that name for the alias.
You can use the _cat API to list all existing indices and aliases in your cluster:

GET _cat/indices
 
GET _cat/aliases

Log Context

Log “an index exists with the same name as the alias” class name is AliasValidator.java. We extracted the following from Elasticsearch source code for those seeking an in-depth context :

 throw new IllegalArgumentException("index name is required");
 }  IndexMetadata indexNamedSameAsAlias = indexLookup.apply(alias);
 if (indexNamedSameAsAlias != null) {
 throw new InvalidAliasNameException(indexNamedSameAsAlias.getIndex(); alias; "an index exists with the same name as the alias");
 }
 }  void validateAliasStandalone(String alias; String indexRouting) {
 if (Strings.hasText(alias) == false) {

 

 [ratemypost]