Elastic Search

How to Get Data From Elasticsearch API?

Elasticsearch is a well-developed, distributed, and open-source analytical search engine and database. It is utilized to analyze and save various types of data: numeric data, textual data, raw data, semi-structured and unstructured data. Elasticsearch stores data in a document in JSON format and uses Rest APIs to play with Elasticsearch indexes, data, and documents.

This article will cover:

How to Get Data From Elasticsearch API Using “curl” Command?

The Elasticsearch data can be managed through the “curl” command or the Kibana console. To directly execute Elasticsearch APIs from the system command line using the “curl” command, follow the given procedure.

Step 1: Start Elasticsearch Database

First, navigate to the Elasticsearch “bin” folder:

cd C:\Users\Dell\Documents\Elk stack\elasticsearch-8.9.0\bin

Execute the Elasticsearch batch file to start the database on the system:

elasticsearch.bat

Step 2: Access Elasticsearch From CLI

Next, access the Elasticsearch from the server in CLI, and utilize the “curl” command along with the default Elasticsearch URL:

curl localhost:9200 -u elastic:<user-password>

Step 3: Get Data From Elasticsearch

In order to get the data from Elasticsearch, utilize the “GET” API request along with the “curl” command. In the below command, we are accessing data from a document having id “1” from the “linuxhint” index:

curl -XGET "http://localhost:9200/linuxhint/_doc/1?pretty" -H "kbn-xsrf: reporting" -u elastic:<user-password>

In order to get data from other documents, provide the document “id” in the “GET” API request as shown below:

curl -XGET "http://localhost:9200/linuxhint/_doc/2?pretty" -H "kbn-xsrf: reporting" -u elastic:<user-password>

In the below output, we are accessing data from a document having id “2”:

How to Get Data From Elasticsearch API Using Kibana Console?

Kibana is a visualization project for Elasticsearch that is used to show Elasticsearch data in representative form. To use the Kibana console to get data from Elasticsearch API, take a look at the given instructions.

Step 1: Run Kibana

Run the Kibana on the system. For doing so, launch its “bin” directory:

cd C:\Users\Dell\Documents\Elk stack\kibana-8.9.0\bin

Execute the batch file “kibana.bat” to start Kibana on the system:

kibana.bat

Step 2: Sign in to Kibana

Next, open the default URL of Kibana “localhost:5601”. Provide the login information for Elasticsearch user account and press the “Log in” button:

Next, open the Kibana menu by hitting the “Three horizontal line” icon and choose the “Dev Tools” menu to launch the Kibana console:

Step 3: Get Data Using Elasticsearch API

Next, access the data by simply using “GET /<index-name>/_doc/<document-id>?pretty” API request:

GET /linuxhint/_doc/2?pretty

The output shows that we have retrieved the data using Elasticsearch API in the Kibana console:

That’s all about getting data from Elasticsearch API.

Conclusion

To get data from Elasticsearch API, the user can either use the “curl” command along with the “GET” API request or the Kibana console. In order to get data from the Kibana console, first start it on the system. Then, open the “Dev Tools” option and execute the “GET /<index-name>/_doc/<document-id>?pretty” API request to retrieve the data. This blog has illustrated the methods to get data from Elasticsearch API.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.