Elastic Search

Elasticsearch Get Alias

“In Elasticsearch, an index refers to an alternative name assigned to a given Elasticsearch resource, such as an index or a data stream. The alias is a secondary name that can be passed to various Elasticsearch API endpoints and perform the action on the resource. The primary purpose of an alias is to prevent name collisions, convenience, or to perform operations such as reindexing without downtime.

Although most API endpoints in Elasticsearch support the use of aliases, there are a few exceptions, such as destructive APIs. An example would be Elasticsearch delete index API.”

In this article, you will learn how to get the aliases of a given index or data stream using the Get Alias API.

Let’s explore.

Elasticsearch Create Index Alias

Before learning how to fetch the aliases of a given resource, let’s create a simple alias for an index. We use the aliases API and the action as ADD to create an alias for a given resource.

For example, suppose we have an index called “earthquake”. To add an alias to the index, we can run the query as shown.

curl -XPOST "http://localhost:9200/_aliases" -H "kbn-xsrf: reporting" -H "Content-Type: application/json" -d'
{
  "actions": [
    {
      "add": {
        "index": "earthquake",
        "alias": "quake"
      }
    }
  ]
}'

 
The request above creates an alias “quake” for the “earthquake” index. If successfully, the query should return true:

{
  "acknowledged": true
}

 

Elasticsearch View Cluster Aliases

To view the aliases in your cluster, we can use the aliases API as shown in the syntax below:

GET _alias

 
Accessing the _alias endpoint without any parameters returns all the aliases in your cluster. An example is as shown:

curl -XGET "http://localhost:9200/_alias?pretty=true" -H "kbn-xsrf: reporting"

 
The query above should return all the aliases in the cluster as shown in the example output below:


As you can guess, a resource can have more than one alias.

We can also use the cat API to get the list of aliases in your cluster. The request syntax is as shown:

GET _cat/aliases

 
For example, to show all the indices in the cluster in human-readable form, we can run the query as:

curl -XGET "http://localhost:9200/_cat/aliases?v" -H "kbn-xsrf: reporting"

 
Resulting output:


This returns the alias, the index or data stream on which the alias belongs, write status, etc.

Elasticsearch Show Alias for a Given Resource

To view the aliases associated with a given resource, you can use the request syntax as shown below:

GET <resource>/_alias

 
Where the resource is either an existing index or data stream.

For example, to view the aliases of the kibana_event_log index, we can run:

curl -XGET "http://localhost:9200/.kibana-event-log-8.3.3/_alias?pretty" -H "kbn-xsrf: reporting"

 
This should return the aliases associated with the specified index.

{
  ".kibana-event-log-8.3.3-000001": {
    "aliases": {
      ".kibana-event-log-8.3.3": {
        "is_write_index": true,
        "is_hidden": true
      }
    }
  }
}

 

Elasticsearch Show Resources Associated With a Given Alias

You can also show which resources are assigned a specific alias using the query syntax provided below:

GET _alias/<alias>

 
For example, to show which resource is using the “quake” alias, we can run:

curl -XGET "http://localhost:9200/_alias/quake?pretty" -H "kbn-xsrf: reporting"

 
The query above should return the output as:

{
  "earthquake": {
    "aliases": {
      "quake": {}
    }
  }
}

 
This indicates that the alias “quake” is assigned to the index “earthquake.”

Conclusion

In this article, you discovered how to view all the aliases in your cluster using the aliases and cat API. You also learned how to fetch the aliases of a given resource and vice versa.

Thanks for reading!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list