In the previous article, we have seen that how to download Elasticsearch in our machine and get working. Now, let’s discuss how Elasticsearch actually works. Instead of directly jump on integration with Django, let’s first understand data is manipulated in Elasticsearch engine. So in this article, we will see how Elasticsearch engine creates records and all other related stuff.

How Elasticsearch Works?

Elasticsearch provides a RESTful API that you can interact within a variety of ways. This basically is a type of interface that describes the way a client and server interact. If a server is said to be “RESTful”, it provides a way to interact through common HTTP methods (GET, POST, PUT, DELETE), and does not maintain state information. Each request is independent and resources are returned in common text formats like JSON.

Elasticsearch interaction, you specify the operation you are using to determine what kind of action to perform on the data that follows. To retrieve information, you may use a GET command. To create or update records, you can use a PUT or POST command.

CRUD Methods.

Elasticsearch provides API access that can perform all (POST, GET, PUT, and DELETE) of these functions. We will discuss Elasticsearch in terms of how to do these types of operations.

For ease of explanation, we will use curl to demonstrate Elasticsearch using terminal session. If you have not installed curl then use sudo apt-get install curl,

Create Content.

Data creation in Elasticsearch is usually referred to as indexing. This is simply the process of adding data to the store and deciding on categories. We can create objects in Elasticsearch using the HTTP methods PUT or POST. Turn on the Elastic server first.

Open the terminal and follow below command:

curl -XPUT "http://localhost:9200/first_index/car/1/_create" -d '

In this command, first_index is index, car is a type, 1 is object_id and _create is telling that we want to create a new record because PUT command can be used to update information as well.

The index can be thought of as a database. It is the top-level organizational unit that can separate some information from others. The -d flag, you pass in the object that you would like to store. This is a JSON-like object with a flexible format.

You should receive a response back indicating that the operation was successful:


The id can be specified if you are creating it using a PUT command, or left off to be auto-generated if you choose to use a POST command.

If you are creating the object using POST then a command is:

curl -XPOST "http://localhost:9200/first_index/car" -d '

The response should be like:


It means in POST request id can be default generated. So the benefit of use PUT request is you can get data easily using id.

Read Content.

We will use GET method to read content.

The most basic way to retrieve data is by specifying the exact object by the index, type, and id:

curl -XGET "http://localhost:9200/first_index/car/1"

This will return the information about the document that we received when we were creating the object. Appended onto this output is a key called _source that contains the document itself as its value:


For our purposes, we probably want the output more human-friendly, we can append ?pretty:

curl -XGET "http://localhost:9200/first_index/car/1?pretty"

Output like:

"_index" : "first_index",
"_type" : "car",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"manufacturer" : "BMW",
"model" : "M3"

If we will only want the document itself to be returned. We can do this by appending /_source:

curl -XGET "http://localhost:9200/first_index/car/1/_source?pretty"

We get the only document:

"manufacturer" : "BMW",
"model" : "M3"

Update Content.

We can easily update content by using the HTTP POST command. The other option is to just use a PUT command and basically “re-create” the object with a replacement (updated) object.

We can update a data object using the POST command and appending /_update to the URI.

We can update the object by a passing script and params Like:

curl -XPOST "http://localhost:9200/first_index/car/1/_update" -d '{ "script": "ctx._source", "params": { "manufacturer":"Porsche", "model":"911"} }'

But, I recommend you to use this command:

curl -XPOST "http://localhost:9200/first_index/car/1/_update" -d '{"doc":{"manufacturer":"Porsche","model":"911"}}

A response should be like:


Now, we can find the document again:

curl -XGET "http://localhost:9200/first_index/car/1/_source"

A response will be:


Delete Content.

The delete operation in Elasticsearch is straightforward. It simply deletes a document with the matching ID. We will use DELETE command.

For instance, to delete a document with the ID of 2:

curl -XDELETE "http://localhost:9200/first_index/car/2"

A response will be:


If we have no ID with id=7 and we run the command with appending /7 to it. We get:



ElasticSearch is a flexible solution that can index text objects easily and dynamically. If you can programmatically index content, then this solution is almost boundless in terms of its ability to generate search results that your applications or users can utilize.

Click here for more blogs…

At BoTree Technologies, we build enterprise applications with our Django team of 20+ engineers.

We also specialize in RPA, AI, Python, Ruby on Rails, JavaScript and ReactJS.

Consulting is free – let us help you grow!