Javatpoint Logo
Javatpoint Logo

Elasticsearch Populate

In this chapter, we will learn how to add index, document, data, and mapping to the elasticsearch database by using an elasticsearch-head plugin that we set up in previous chapter. We will execute the queries using this plugin. Before making a request, be sure to run the elasticsearch server using the elasticsearch.bat file.

We will discuss the following topics in this chapter -

Create an Index

To create an index, we need to specify the index name and request method. Therefore, use the PUT request method and execute the following command to create an index -

Here PUT is request method and student is index name.

Response

If the index is successfully created, you will get the following output in the response body -

Look at the screenshot below to understand it better -

Elasticsearch Populate

Create a document or Add data

After creating an index, we can now index the document to elasticsearch. We will use the POST method for this and specify some IDs for documents to identify them. Set request method = POST.

Use the following code to add data in the index -

Here POST is request method and _doc is used for document type to add data in the index.

Response

The data provided in request are successfully added if you get the following output in response body.

Look at the screenshot below to understand it better -

Elasticsearch Populate

Now we will add one more document in index "student". Similarly, we can add any number of documents in an index.

Response

One more document is successfully added if you get the following output in response body.

Look at the screenshot below to understand it better -

Elasticsearch Populate

Now go to the Overview tab to see that the data is actually added or not. Here refresh the UI by clicking on the Refresh button. You will able to see that the index is added containing two documents, which we added previously.

Elasticsearch Populate

Read a document

In elasticsearch, a document can be retrieved using a GET request. Create a new request to retrieve the document. We also need to specify the index name, and id of the document. Provide all three information along with the GET request, elasticsearch will fetch and display the result in response body. Let us see how it will be done practically -

Response

The below output will show in response body for the above request where index name is student and document id is 02.

In response, we can see the document data under the "_source" object.

Look at the screenshot below to see the result visually -

Elasticsearch Populate

Update a document

To update a document, we require API, request method, data for update, and request string. Elasticsearch provides _update API to update the documents. In this example, we will replace the city from New York to Los Angeles. So, use the following code to update the document.

Response

If you get the same output, the document is updated successfully.

Look at the below screenshot to see the result visually -

Elasticsearch Populate

Delete a document

Elasticsearch provides a DELETE request method to delete a document. So, just set the DELETE the request method and provide index name and document id.

Response

If you get the below output, document 01 is deleted successfully.

Look at the screenshot below to see on the browser -

Elasticsearch Populate

We will discuss all these request methods in detail in the further tutorial.

Delete an Index

To delete an index, just specify the index name and DELETE request method and click on Request button.


Next Topic#




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA