Elasticsearch REST APIs


preview imageProgramming
by Anurag Srivastava,Jul 31, 2018, 6:16:42 PM | 6 minutes |

Elasticsearch provides extensive REST APIs to integrate, query and manage the data. In this blog, I will discuss some of the main APIs which we can use regularly from an extensive list of REST APIs.

We can do many things using Elasticsearch REST APIs like:

  • Check our cluster, node, and index health, status, and statistics, etc.
  • Administer our cluster, node, and index data and metadata.
  • Perform CRUD (Create, Read, Update, and Delete) and search operations against our indexes.
  • Execute advanced search operations such as paging, sorting, filtering, scripting, aggregations, etc.

In this blog, I will explain some important APIs:

_cat API

_cat APIs are very handy as they provide us option to check cluster health, node details, index listing, etc.


Cluster Health:

We can get the cluster health by using the following API:

GET  /_cat/health?v

Above command will give the following output:

epoch      timestamp cluster             status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1641489934 17:25:34  elasticsearch_admin yellow          1         1     11  11    0    0        1             0                  -                 91.7%
Above output shows the cluster health details like status of cluster, total nodes, data nodes, total shards, unassigned shards, active shard percentage etc.


Node details:

If we want to get node details in Elasticsearch cluster:

GET /_cat/nodes?v

Above command will give the following output:

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role   master name
127.0.0.1            4         100  11    2.05                  cdfhilmrstw *      MacBook-Pro.local
Above response is showing node details.


List all index:

If we want to list down the indices in Elasticsearch cluster:

GET /_cat/indices?v

Above command will display list of indices for the cluster:

health status index                           uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .geoip_databases                reQ_IvOoRtuMibGelD33PA   1   0         44           33     69.5mb         69.5mb
green  open   .apm-custom-link                nWvD8ILPQQek11F0UygULw   1   0          0            0       226b           226b
green  open   .kibana_task_manager_7.16.0_001 paoINjlOSi-w4-NVZTAICQ   1   0         17       126526     15.9mb         15.9mb
yellow open   blogs                           RNEeyuOPSs-1cgXJG648cA   1   1          1            0      9.5kb          9.5kb
green  open   .apm-agent-configuration        76gpiUIqQhCiHumEshBG4g   1   0          0            0       226b           226b
green  open   .kibana_7.16.0_001              L3xWr6-rRjmAH9AIk16frw   1   0        467           12      2.4mb          2.4mb
green  open   .tasks                          lR_JpIUOQNGtmXp0Tsmw3g   1   0          2            0      7.7kb          7.7kb
Above response is showing list of all available index, their uuid, available document count, deleted document count, storage size, etc.

Create Index:

If we want to create an index. For example, create an index named blogs

PUT /blogs?pretty

In the above expression, we have provided pretty which displays the output in a pretty format. It will give the following response:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "blog"
}

Acknowledged as true means index has been created successfully.


Delete Index
:

To delete the index:

DELETE /customer?pretty

Above command will give the following response:

{
    "acknowledged": true
}

Acknowledged as true means index has been deleted successfully.



Create Document
:

Now we have created the index so let's create a document in the index.

PUT /blogs/technical/1?pretty
{
  "topic": "introduction to Elasticsearch"
}

Above command will give the following response:

{
    "_index": "blogs",
    "_type": "technical",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

In the above response we have "results" as "created" means document has been created successfully. If we run the same command again then instead of created the response would be updated because it will update the same document id (1). 


Replace Document:

We can replace the data of a document:

PUT /blogs/technical/1?pretty
{
  "topic": "Elasticsearch Installation"
}

In the above expression, I have replaced the same document id with the different topic name.

Above command will give the following response:

{
    "_index": "blogs",
    "_type": "technical",
    "_id": "1",
    "_version": 5,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}

Above response "result" as "updated" means that the record has been updated with given id.


Update Document
:

To update a document we need to run the following expression:

POST /blogs/technical/1/_update?pretty
{
  "doc": { "topic": "introduction to Elasticsearch", "category": "ELK" }
}

In the above expression, I have updated the same document id with the different topic name and with additional category key and its value. 

Above command will give the following response:


{
    "_index": "blogs",
    "_type": "technical",
    "_id": "1",
    "_version": 6,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 1
}

Above response "result" as "updated" means that the record has been updated with given id.


Delete Document:
We can delete a document from the index:

DELETE /blogs/technical/1?pretty

In the above expression, I am deleting the document with id = 1. Above command will give the following response:


{
    "_index": "blogs",
    "_type": "technical",
    "_id": "2",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}

Above response "result" as "deleted" means that the record has been deleted for the given id.


Load Data:
We can also load data from an external file. For example, if we have a JSON data file we can directly push it into Elasticsearch:

curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@blogs.json"

In the above expression, I am indexing the data from the blogs.json file directly into Elasticsearch.

So in this blog, we have covered some basic REST APIs of Elasticsearch to create the index, delete the index, create documents, replace and update documents, delete documents and load data from an external file. In my next blog, I will explain the Search API of Elasticsearch and how we can apply different types of searches.

Other Blogs on Elastic Stack:
Introduction to Elasticsearch

Elasticsearch Installation and Configuration on Ubuntu 14.04
Log analysis with Elastic stack 
Elasticsearch Rest API
Basics of Data Search in Elasticsearch
Elasticsearch Rest API
Wildcard and Boolean Search in Elasticsearch
Configure Logstash to push MySQL data into Elasticsearch 
Metrics Aggregation in Elasticsearch
Bucket Aggregation in Elasticsearch
How to create Elasticsearch Cluster

If you found this article interesting, then you can explore “Mastering Kibana 6.0”, “Kibana 7 Quick Start Guide”, “Learning Kibana 7”, and “Elasticsearch 7 Quick Start Guide” books to get more insight about Elastic Stack, how to perform data analysis, and how you can create dashboards for key performance indicators using Kibana.


You can also follow me on:

- LinkedIn: https://www.linkedin.com/in/anubioinfo/

- Twitter: https://twitter.com/anu4udilse

- Medium: https://anubioinfo.medium.com




Comments (2)

  • user image
    jitender yadav
    Aug 1, 2018, 6:30:21 PM

    nice

  • user image
    Anurag Srivastava
    Aug 4, 2018, 11:37:15 AM

    Thanks Jeetu :)

Leave a comment

Related Blogs

preview thumbnail
Introduction to Kibana

Aug 1, 2020, 6:19:45 PM | Anurag Srivastava

preview thumbnail
Bucket Aggregation in Elasticsearch

Aug 29, 2018, 7:15:06 PM | Anurag Srivastava

preview thumbnail
Metrics Aggregations in Elasticsearch

Aug 18, 2018, 6:02:20 PM | Anurag Srivastava

preview thumbnail
Introduction to Elasticsearch Aggregations

Aug 14, 2018, 4:47:56 PM | Anurag Srivastava

preview thumbnail
Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

preview thumbnail
Basics of Data Search in Elasticsearch

Aug 4, 2018, 7:02:21 AM | Anurag Srivastava

Top Blogs

preview thumbnail
Wildcard and Boolean Search in Elasticsearch

Aug 10, 2018, 7:14:40 PM | Anurag Srivastava

preview thumbnail
Elasticsearch REST APIs

Jul 31, 2018, 6:16:42 PM | Anurag Srivastava

preview thumbnail
preview thumbnail
Create a Chess board in PHP

Mar 9, 2020, 8:45:41 AM | Rocky Paul

preview thumbnail
Bucket Aggregation in Elasticsearch

Aug 29, 2018, 7:15:06 PM | Anurag Srivastava

preview thumbnail
Metrics Aggregations in Elasticsearch

Aug 18, 2018, 6:02:20 PM | Anurag Srivastava