Elastic Search

How to Get All Documents From an Index in Elasticsearch

Elasticsearch is a well-established analytical and distributed search engine that is typically used to store bulky, unstructured, or semi-structured data. These are stored in document form in JSON format. Further, these documents are managed by the Elasticsearch index just like databases in traditional SQL and relational databases. Sometimes users are required to access data from all documents from the index of Elasticsearch but do not know how to do it.

This post will elaborate on:

How to Get All Documents From an Index Using “curl” Command in Elasticsearch?

To access all the documents from Elasticsearch indexes, the “_search” queries can be used along with the “GET” API. For illustration, follow up on the below-mentioned instructions.

Step 1: Launch Elasticsearch Engine

First, navigate to the Elasticsearch “bin” folder:

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

Run the batch file “elasticsearch.bat” to start the engine on the system:

elasticsearch.bat

Elasticsearch will be started when the cluster health turns to “YELLOW” as shown below:

Step 2: Access Elasticsearch From CLI

Now, utilize the “curl” command to access data from the server. Here, we are accessing Elasticsearch data from the “localhost:9200”:

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

In the above command, “-u” is utilized to pass the “elastic” user credentials along with the command:

Step 3: Access All Documents From an Index

In order to access all the documents from a specific index, utilize the “_search” query in the “GET” API request:

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

In the above command:

  • GET” is an API that is utilized to access data from indexes.
  • linuxhint” is the name of the index where all documents are being extracted.
  • _search” is a query that is utilized to search all documents from an index:

The above output shows the data in a bulky form that is difficult to read. To make it readable, utilize the “?pretty” method as done below:

curl -X GET "localhost:9200/user/_search?pretty" -H "kbn-xsrf: reporting" -u elastic:<user-password>

The below output indicates that we have successfully accessed all the documents from the “linuxhint” index:

Executing messy commands is challenging and the chances of errors are more. To avoid using massive commands, users can utilize Kibana.

How to Get All Documents From an Index in Kibana?

Kibana is an Elasticsearch visualization tool to represent data in visual form. It also offers a console from where users can run queries to store, retrieve and manage data in Elasticsearch indexes. In order to access all the documents from an index, follow the listed instructions.

Step 1: Start Kibana

First, start the Kibana. To do so, first, open its “bin” directory through the “cd” command:

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

Then, run the Kibana’s batch file “kibana.bat” to start it on the system:

kibana.bat

Step 2: Sign In to Kibana

After Kibana is properly started, navigate to its default URL “localhost:5601” provide the “elastic” user credentials, and hit the “Log in” button:

Step 3: Open Kibana Console

Open the Kibana menu by hitting the “Triple bar” icon. After that, open the “Dev Tools” to launch the Kibana console:

Step 4: Access All Documents From Index

In order to access all documents from the index, simply use the “GET /<indexname>/_search” API request:

GET /linuxhint/_search?pretty

The output indicates that we have successfully accessed the documents from an index using the Kibana console:

That is all about retrieving all documents from the index in Elasticsearch.

Conclusion

To retrieve all the Documents from an index in Elasticsearch, users can either use the “curl” command along with the “GET” API request or use the Kibana console. Elasticsearch documents are retrieved by using the “_search” query with “GET” API such as the “Get /<index-name>/_search” request. This post has illustrated the methods to access all the documents from an index in Elasticsearch.

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.