'Find elasticsearch indices with no aliases

I have an elasticsearch cluster whose indices I need to investigate. Specifically, I want to find all indices that do not have aliases.

Is this even possible? If so, how?

(Using sense notation for this question.)

I know I can get all indices with their aliases, even if the alias field is empty:

GET _aliases

And I know I can get all indices that have aliases:

GET /*/_alias/*

But can I get all indices without aliases?

Clearly, I can just get all indices, and then use some tool like awk or whatever to do the work for me, but my naïve guess is that it's most efficient for elasticsearch to do all this work at once.



Solution 1:[1]

Short answer: There is no easy api for you to find out which indices have not been linked to an alias.

Long answer: You could try the '_cluster/state' endpoint like this:

GET _cluster/state?filter_path=metadata.indices.test.aliases

Where test is the name of your index. It gives the following result:

{
  "metadata": {
    "indices": {
      "test": {
        "aliases": []
      }
    }
  }
}

Now if I try it with an index that is actually linked:

GET _cluster/state?filter_path=metadata.indices.test_with_alias.aliases

I get the following result:

{
  "metadata": {
    "indices": {
      "test_with_alias": {
        "aliases": [
          "new_alias"
        ]
      }
    }
  }
}

It's not the prettiest way to do this, but it's possible :)

Hope this helps!

Solution 2:[2]

This shell script will help you

#!/bin/bash
ES="https://localhost:9316"
INDICES=`curl -XGET ${ES}/_cat/indices -s --insecure | awk '{print $3}'`
META=`curl -XGET "${ES}/_cluster/state?filter_path=metadata.indices.*.aliases" -s --insecure`
echo ${#META}
for INDEX in $INDICES
do
  if [[ ${INDEX} != \.* ]]
  then
    RES=`jq ".metadata.indices.${INDEX}.aliases" <<< ${META}`
    if [[ ${#RES} == 2 ]]
    then
      RES="NOALIAS"
    fi
    echo ${INDEX} $RES
  fi
done

Solution 3:[3]

You can determine this as follows via the Java REST High Level Client:

val indexResponse = client.indices().get(GetIndexRequest("*"), RequestOptions.DEFAULT)
val aliases = indexResponse.aliases

aliases is of type Map<String, List<AliasMetadata>>. Every Map entry with a empty list (without alias metadata) is a index without an alias.

With this in mind, here's what you need to do to get all the indices without aliases.

val indicesWithoutAliases = indexResponse.aliases.filterValues { it.isEmpty() }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Byron Voorbach
Solution 2 Vorapoap
Solution 3