Elastic Search

How do I Create an Index in Elasticsearch?

Elasticsearch is a distributed open-source and free search engine and database. It helps us to find the data faster that we need. It is the most important component of the Elastic stack and was introduced by Apache Lucene. Elasticsearch stores various kinds of data such as textual data, numerical data, and semi-structured and unstructured data, and uses rest APIs to process the stored data.

This article will discuss:

What is Elasticsearch Index?

Elasticsearch is purely a NoSQL database and stores data in JSON format. Therefore, it uses indexes to store data instead of tables. Indexes are the same as tables in SQL databases (MySQL) and collections in NoSQL databases (MongoDB). Indexes are referred to as a collection of documents that store data in the field in JSON format.

How to Create and Use Index in Elasticsearch?

Indexes work with documents and each document consists of various fields to store data. Working with Elasticsearch indexes is quite different from any other SQL database. To create and use indexes in Elasticsearch, follow the provided demonstration.

Step 1: Start Elasticsearch

First, start the Elasticsearch engine. To do so, first, fire up the Windows Command Prompt terminal via Start menu. Then, utilize the “cd” command to navigate to the “bin” directory of Elasticsearch:

cd C:\Users\Dell\Documents\Elasticsearch\elasticsearch-8.7.0\bin

After that, execute the “elasticsearch.bat” command to start the Elasticsearch:

elasticsearch.bat

Step 2: Verify Elasticsearch in Executing

Next, navigate to the “localhost:9200” and check if Elasticsearch is executing or not. For this purpose, we have utilized the “curl” command to check whether Elasticsearch is running or not from the command line. However, you can directly navigate to the Elasticsearch URL from your system browser:

curl localhost:9200 -u elastic:jSo-sQ*XseQ8nygL=tL=

The output shows that Elasticsearch is running on the system:

Step 3: Create a New Index

Create the new index using “PUT” API and by providing the Elasticsearch URL along with an index name like “http://localhost:9200/<index-name>”. Here, the “-u” option is necessary to provide the Elasticsearch login credentials such as username and password:

curl -XPUT "http://localhost:9200/linuxhint" -H "kbn-xsrf: reporting" -u elastic:jSo-sQ*XseQ8nygL=tL=

Step 4: View Indexes For Verification

After creating the index, list all indexes through provided command and check new index is created or not:

curl http://localhost:9200/_aliases?pretty=true -u elastic:jSo-sQ*XseQ8nygL=tL=

From the output, you can see that we have successfully created the new index “linuxhint”:

Step 5: Insert Data
Next, in order to insert the data in the index, utilize the “POST” request. Here, the “–data-binary” option is used to add binary data. The URL “http://localhost:9200/linuxhint/_doc/1” contain Elasticsearch path, index name, type, and id:

curl -XPOST -H "Content-type:application/json" --data-binary "{\"add-field\": {\"Name\":\"Rafia\", \"Designation\":\"Technical-Author\"}}" http://localhost:9200/linuxhint/_doc/1 -u elastic:jSo-sQ*XseQ8nygL=tL=

Step 6: Get Data From Index

Access the data from the index using the “GET” command:

curl -X GET "localhost:9200/linuxhint/_doc/1?pretty" -u elastic:jSo-sQ*XseQ8nygL=tL=

The output indicates that we have successfully created and stored data in the new index “linuxhint”:

Bonus Tip: How to Delete Index in Elasticsearch?

Basic CRUD operation on Elasticsearch data and on the structure is quite different from other databases. In order to delete the index in Elasticsearch, utilize the “DELETE” index API. For the illustration, go through the listed steps.

Step 1: Delete Index

Delete the index that stores data in the Elasticsearch database just like tables in the SQL database through the “DELETE” index API. For instance, we have deleted the “linuxhint” index from Elasticsearch:

curl -X DELETE "localhost:9200/linuxhint?pretty" -u elastic:jSo-sQ*XseQ8nygL=tL=

The output “acknowledged” value “true” means we have deleted the “linuxhint” index successfully:

Step 2: Verification

For the verification, again list down all the indexes through the given command:

curl http://localhost:9200/_aliases?pretty=true -u elastic:jSo-sQ*XseQ8nygL=tL=

That’s all about how to create and use Elasticsearch indexes.

Conclusion

The Elasticsearch indexes are the main unit of the database that store data in documents just like tables in SQL databases. To create the Elasticsearch index, utilize the “curl -XPUT “http://localhost:9200/<index-name>”” command. This article has illustrated the method to create and use 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.