Cyberithub

Top 20 ElasticSearch API Query for Developers Part – 1

In this article, I will take you through 10 Best ElasticSearch API Query. Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch is developed in Java. Here we will see different types of Elasticsearch api query one can use to check the snapshot and indices details.

ElasticSearch API Query

In this example, i have firstly registered the testrepo repository in the AWS S3 Bucket. To register the repo you need to first create IAM User and assign valid policies to this user to perform S3 Operations. You can go through my article of How to create an IAM User and attach Policy in AWS to understand more on that.

Top 20 ElasticSearch API Query for Developers Part - 1 1

1. To setup the number of shards and replicas for your index

Ans. Below is the query to setup the number of shards and replicas.

curl -XPUT 'localhost:9200/index?pretty' -H 'Content-Type: application/json' -d '
{
"settings":{
           "number_of_shards":2,
           "number_of_replicas":1
           }
}
'

2. To get all the snapshot in a nice output

Below is the query to get all the snapshot in a proper format.

curl -XGET "localhost:9200/_snapshot/testrepo/_all?pretty"

3. To check the total number of snapshots 

Below is the query to check the total number of snapshots.

curl -XGET "localhost:9200/_snapshot/testrepo/_all?pretty" | grep -i snapshot | wc -l

4. To check the total number of indices 

Below is the query to check the total number of indices.

curl -XGET "localhost:9200/_snapshot/testrepo/_all?pretty" | grep -i indices | wc -l

5. To get only the indices from every snapshots

Below is the query to get only the indices from every snapshots

curl -XGET "localhost:9200/_snapshot/testrepo/_all?pretty" | jq -r '.snapshots[].indices'

6. To get the 11th indices of every snapshots

Below is the query to get 11th indices of every snapshots

curl -XGET "http://192.168.13.16:9200/_snapshot/testrepo/_all?pretty" | jq -r '.snapshots[].indices[10]'

7. To get only indices from first snapshot

Below is the query to get only indices from first snapshot.

curl -XGET "http://192.168.13.16:9200/_snapshot/testrepo/_all?pretty" | jq -r '.snapshots[0].indices[]'

8. To get the first indice from every snapshot

Below is the query to get the first indice from every snapshot

curl -XGET "localhost:9200/_snapshot/testrepo/_all?pretty" | jq -r '.snapshots[].indices[0]'

9. To get all indices from Second snapshot

Below is the query to get all indices from Second Snapshot.

curl -XGET "localhost:9200/_snapshot/testrepo/_all?pretty" | jq -r '.snapshots[1].indices[]'

10. To get Second indices of every snapshots

Below is the query to get the second indices of every snapshots.

curl -XGET "localhost:9200/_snapshot/testrepo/_all?pretty" | jq -r '.snapshots[].indices[1]'

Stay Tuned for more Elasticsearch api query examples in next article!!!!!!!

Also: Docker Installation in CentOS 7

Reference: ElasticSearch Documentation

Leave a Comment