Elasticsearch CRUD operations using head plugin

Phase 01 — Introduction to Elasticsearch — Blog 04

Arun Mohan
elasticsearch
4 min readDec 9, 2017

--

In this article, we will familiarize with the setting up of a simple visualization tool called “elasticsearch-head” and how to do a few CRUD operations using this tool.

Purpose of the tool

Elasticsearch-head is primarily a visualisation tool for the data stored in Elasticsearch, which in turns enables the users to query and do other CRUD operations. It is a very handful and helpful tool when we are dealing with Elasticsearch, especially to try out new queries, check the mappings of the indices (analogous to the schema in SQL world) and also looking into the data structures of the documents ,which other wise has to be done either programmatically or the terminal.

Installation

  1. clone the repo in this link
  2. install nodeJs in your system. (you can find the installation guidelineshere)
  3. now browse inside the cloned repo and type “npm install”
  4. After the installation of the necessary packages in the previous step, type in “npm start”.

Now the UI is ready in the link http://localhost:9200

Common error in UI

A common error while we try to use the Elasticsearch-head UI is given in the screenshot below:

This is primarily a CORS issue, which can be solved by configuring Elasticsearch to allow access to the requests from localhost. This can be done by editing the “elasticsearch.yml” file. Add the following lines to the elasticsearch.yml file located in the elasticsearch installation path /etc/elasticsearch

Now restart the elasticsearch by using the command “sudo service elasticsearch restart”. Now upon refreshing the elasticsearch-head UI inhttp://localhost:9100, we will see the UI without any errors.

UI familiarisation — landing page

Now let us explore the elasticsearch-head plugin a bit more. Following is the screenshot of the landing page of the UI.

Let us explore the marked items in the above figure one by one :

1. Overview
Indicates which tab the UI is now on.

2. Connection bar
Here we specify the host machine and port of the Elasticsearch instance we want to connect. In this case we are using localhost as the host and the port as the 9200 since Elasticsearch is running in our local at the default port 9200.

3. Cluster health
Indicates the health of the cluster. The red, cluster health indicates there are some nodes (primary shards) in the cluster which are not yet available and this might be a serious issue, hence the color red. If some of the replica shards are not available it will be shown as yellow and lastly if every shard is available, it will be shown as green. This provides us a quick overview about the health status of the elasticsearch.

Note: The terms shard,cluster,node will be explained in a detail blog in the future.

4. Index name and other information
This is the index name and the size and number of documents in the index are shown here.

5. Info and actions tab
Each index has metadata and allows some actions to be performed on them. The info tab allows a list of metadata, which up on click would be displayed in the UI. One of the most useful metadata is the “mapping” which can be easily viewed from here.

6. Node list
In the introduction blog I have told that Elasticsearch is a distributed solution. This means it can be deployed on multiple systems or nodes. This column shows the list of nodes and provide options to view the node details using the “info” and “actions” dropdowns.

7. Shard info
Shards are the basic units of storage in Elasticsearch. Each index is divided in to shards . These shards may be distributed amongst different nodes or on a single node. Here for the index “training-test-01”, we have 5 shards on a single node “9CCT_A1”.The state and information about each shard is obtained by double clicking on it.

8. Info
The information tab enables us to see the stats on cluster health, nodes and other general things related the elasticsearch.

UI familiarisation — the request page

The next main page to explore the UI in elasticsearch-head is the request page as shown below:

This UI basically allows us to perform all the CRUD operations which we performed over the terminal in last blog .

1. The request string
The place where the request is made. Here in this example for s GET request we mention the index name, type name and the id of the document

2. The API column
We can specify the API to be used to interact with elasticsearch, if any, here. In this request shown in the screenshot, we don’t have any API methods called and hence it is left blank

3. The request type specifier
The type of the request, whether it is a POST, GET,PUT or DELETE can be specified here.

4. The query space
If there are any queries associated with the request, we can give it here.

5. The response area
This is the response area which will show the response of the request generated by the previous sections on pressing the “Reguest” button.

Conclusion
In this short article, we have seen the setting up the elasticsearch-head tool, and also the familiarisation with the same.

--

--