Elasticsearch is a robust, well-liked solution to store bulky, unstructured, and semi-structural data. It is purely a NoSQL database and uses a totally different approach to store, manage, and retrieve data. It stores data in a document in JSON format and uses rest APIs to perform different operations on stored data.
In this blog, we will demonstrate:
- How Elasticsearch Works to Store and Search Data?
- What are Elasticsearch Documents?
- How to Store Data in an Elasticsearch Document?
How Elasticsearch Works to Store and Search Data?
The Elasticsearch major components or hierarchy that is used to store data is listed below:
- Document: The document is the main part of Elasticsearch that stores data in JSON format. Like
- Indices: Indices are referred to as indexes. It is a collection of documents. Like in SQL, it is referred to as a Database.
- Inverted Indexes: It supports very speedy full-text search. It stores the word as an index and the name of the document as the reference.
What are Elasticsearch Documents?
The Elasticsearch document is a storage unit of data in JSON format. Like in relational databases, the document can be referred to as a table or a row of a database that is stored in some index. The index can have multiple documents and is referred to as a database that has multiple tables. It usually stores a complex data structure and sterilizes the data in JSON format.
Additionally, each document can contain multiple fields which are “key:value” pairs to store the data just like a table has multiple columns or fields in a relational database. Then, these key-value pairs are supposed to be indexed in a way to determine the document mapping. The mapping then defines the data type of the document according to the field data such as text, float, geo point, time, and many more.
Elasticsearch never bound us to pre-define the index field structure and the documents can have different field structure in an index. However, if the mapping of the field is defined for a specific data type, then all Elasticsearch documents in an index must follow the same mapping type. To check out the working of the document to store data in Elasticsearch, go through the next section.
How to Store Data in an Elasticsearch Document?
To store data in Elasticsearch, the user first needs to create an index. Then, specify the fields to store the data in the Elasticsearch document. For the demonstration, go through the listed steps.
Step 1: Start Elasticsearch
To run the Elasticsearch database or engine on the system, launch the system terminal such as Command Prompt. After that, visit the “bin” folder of Elasticsearch through the “cd” command:
After that, execute the batch file of Elasticsearch to run the database on the system:
Step 2: Start Kibana
Next, execute the Kibana on the system. To do so, visit its “bin” folder from Command Prompt:
Next, run the below command to start executing Kibana:
Note: If you have not installed and set up Elasticsearch and Kibana on the system, navigate to our posts, and check out the step-by-step procedure to install them on the system.
For Elasticsearch, visit our “Install and Set up Elasticsearch With .zip on Windows” article. In order to set up Kibana on Windows, follow the “Setup Kibana for Elasticsearch” article.
Step 3: Login to Kibana
After starting the Kibana on the system, navigate to the default address of Kibana “localhost:5601” in the browser, and provide the login credentials of Elasticsearch such as “elastic” user and password. After that, hit the “Log in” button:
Step 4: Open Kibana “Dev Tool”
After that, click on the “Three horizontal bars” icon and open the Kibana “Dev Tool” to use APIs to store, retrieve and update the data:
Step 5: Create Index
Now, create a new index using “PUT /<index-name>” API request:
The output shows that the “employee-data” index is successfully created:
Step 6: Insert Data in Document
Now, use the “POST” API to store the data in the index. In the below request, “employee-data” is an index of Elasticsearch, “_doc” is used to store data in Elasticsearch document, and “1” is the id:
{
"Name":"Rafia",
"DOB":"19-NOV-1997",
"stored":true
}
Step 7: Retrieve Data From Elasticsearch Document
To access the data from the index or Elasticsearch document, utilize the “GET” API as used below:
The output shows that we have successfully extracted the data from the Elasticsearch document having id “1”:
That is all about the Elasticsearch Document.
Conclusion
The Elasticsearch document is usually used to store data in JSON format. Like in relational databases, the document can be referred to as a row that is stored in some index. These indexes can have multiple documents just like databases have different tables. These documents contain multiple fields which are “key:value” pairs to store the data. This article has demonstrated what are Elasticsearch Documents and how they work in Elasticsearch.