Blog Detail

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 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 lets 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 different topic name..

Update Document:

To update a document we need to run 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 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 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 blogs.json file directly into Elasticsearch.

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


On BQ Stack, we have following other blogs on Elasticsearch:

Introduction to Elasticsearch
Elasticsearch Installation and Configuration on Ubuntu 14.04
Log analysis with Elastic stack
Basics of Data Search in Elasticsearch
Wildcard and Boolean Search in Elasticsearch
Configure Logstash to push MySQL data into Elasticsearch


In case of any confusions please leave your comment.




About Author

Anurag Srivastava

Author | Blogger | Tech Lead | Data Scientist | Innovator |

View Profile

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

Basics of Data Search in Elasticsearch

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

Introduction to Elasticsearch Aggregations

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

Metrics Aggregation in Elasticsearch

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

Bucket Aggregation in Elasticsearch

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

Wildcard and Boolean Search in Elasticsearch

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

Create word cloud in Python

Jun 30, 2018, 6:06:45 AM | Anurag Srivastava

Typecasting in PHP for short datatype

Jun 8, 2018, 8:03:52 AM | Lovish Sharma

Built-in PHP Arrays

Feb 20, 2018, 7:11:44 PM | Anurag Srivastava

Top Blogs

Build and deploy Angular code using Python

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Install Jenkins on Ubuntu

May 26, 2018, 6:42:02 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava