Overview

In this article I will give examples of how to connect to the Elasticsearch API’s using either Postman or natively using PowerShell.

References

The full list of Elasticsearch Rest API’s can be found at: https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html

Tools

To make things easier I recommend Postman to send to the Elasticsearch API. There are examples of using Postman below.
I will also be giving some PowerShell examples, you won’t be required to install any additional PowerShell modules.

Examples

All of these examples will be targeting “localhost” – you will need to change the target to your particular requirements.

Assumptions

In the examples I am assuming the following:

  1. The node is listening on http and not https
  2. The node is listenng on port 9200 (tcp)

Allow Localhost

By default, Elasticsearch does not listen on “localhost”, you will need to change that if you are connecting locally.

  1. Browse to the “elasticsearch.yml” file
  2. Change the “network.host” value and add “_local_”

Example:

Get Cluster Overview

Run the following to get an overview of the cluster, this can be helpful if you need to get the following:

  • Cluster UUID
  • Node version
  • Build date
  • Lucene version
http://localhost:9200

Note: GET request

Get Cluster Health

Run the following if you need to get the health of the cluster. This can be helpful if you need to get the following:

  1. Cluster name
  2. The cluster status
  3. Number of nodes
  4. Number of data nodes
  5. Active shards
  6. Pending tasks
http://localhost:9200/_cluster/health?pretty=true

Note: GET request

Get Indicies

Run the following if you need to get an overview of all indicies. This can be helpful if you need to get the following:

  1. Indicies health
  2. Indicies name
  3. Indicies sizse
http://localhost:9200/_cat/indices

Note: GET request

Get Repositories

Run the following if you need to get an overview of all repositories. This can be helpful if you need to get the following:

  1. Repository names
  2. Repository types
  3. Repository locations
  4. Repository container location
  5. Repository folder location
  6. Any other details configured for the repository
http://localhost:9200/_cat/repositories

Create Index

If you want to create a new index, you can run the following:

http://localhost:9200/indexname

Note: Replace “indexname” with the name of the index you want to create

If using Postman, change to “body” tab, select “raw”, change to “JSON” and enter the following JSON below:

{
    "settings" : {
        "index" : {
            "number_of_shards" : 20, 
            "number_of_replicas" : 5 
        }
    }
}
$server= "localhost"
$index = "indexname"
$elasticurl = "http://$server:9200/$index"
$body = @{
    "settings" : {
        "index" : {
            "number_of_shards" : 20, 
            "number_of_replicas" : 5 
        }
    }
}
} | ConvertTo-Json
Invoke-RestMethod -Method POST -Uri $elasticurl -Body $body -ContentType 'application/json'

Note: PUT request

Conclusion

Whilst not an exhaustive list of examples, these will give you an idea of how to interact with Elasticsearch using third party tools or natively from Microsoft Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.