Blog Detail

Covid-19 Tracker Ask Question

preview image Programming
by Anurag Srivastava, Jul 31, 2018, 6:16:42 PM | 3 minutes |

Elasticsearch Rest API

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.

Now I will explain some of them here:

_cat API

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

GET  /_cat/health?v

If we want to get node details in Elasticsearch cluster:

GET /_cat/nodes?v

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

GET /_cat/indices?v

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.

Delete Index:
To delete the index:

DELETE /customer?pretty

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"

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.

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.

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

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, I 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.

Comments (2)

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


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

    Thanks Jeetu :)

Leave a comment

Related Blogs

Create Sequence Diagrams using PlantUML

Jun 27, 2021, 12:50:31 PM | Anurag Srivastava

Improving your productivity on Linux Terminal

Nov 29, 2020, 5:16:40 PM | Anurag Srivastava

Elastic Stack Interview Questions

Sep 12, 2020, 3:58:55 PM | Anurag Srivastava

Introduction to Kibana

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

Create a Chess board in PHP

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

Handle Excel file using Python (Part 2)

Dec 31, 2019, 1:33:53 PM | Anurag Srivastava

Top Blogs

Wildcard and Boolean Search in Elasticsearch

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

Elasticsearch Rest API

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