Elasticsearch Helloworld example- Perform basic document operations with Elasticsearch | JavaInUse



Perform basic operations with Elasticsearch.



Overview

In the previous post we saw how to install ElasticSearch. In this post we perform the basic CRUD operations using Elasticsearch. For this we make use of the Sense plugin for google.

Lets Begin

We will be performing the following operations-
a. INDEXING A DOCUMENT USING ELASTICSEARCH.
b. FETCHING DOCUMENTS USING ELASTICSEARCH.
c. UPDATING DOCUMENTS USING ELASTICSEARCH.
d. DELETING DOCUMENTS USING ELASTICSEARCH.

Start the Elasticsearch
  • INDEXING A DOCUMENT USING ELASTICSEARCH-
    In order to index a first JSON object we make a PUT request to the REST API to a URL made up of the index name, type name and ID. That is: http://localhost:9200/<index>/<type>/[<id>].
    PUT /employees/employee/1
    {
        "name": "employee1",
        "address": "address1",
        "age": 30
    }
    
  • FETCHING DOCUMENTS USING ELASTICSEARCH-
    • Getting a complete document using Elasticsearch.
      On the basis of id we fetch the complete document.
      GET /employees/employee/1
      

    • Getting a part of a document using Elasticsearch.
      On the basis of id we fetch only the name of the document.
      GET /employees/employee/1?_source=name
      
  • UPDATING DOCUMENTS USING ELASTICSEARCH-
    • Updating a whole document using Elasticsearch.
      On the basis of id we update all the fields of a document.
      PUT /employees/employee/1 -d
      {
          "name": "new employee1",
          "address": "new address1",
          "age": 31
      }
      
    • Updating documents partially using Elasticsearch.
      On the basis of id we update only the name field of the document.
      POST /employees/employee/1/_update
      {
         "script": "ctx._source.name= \"updated_name\""
      }
      
  • DELETING DOCUMENTS USING ELASTICSEARCH-
    On the basis of id we delete the complete document.
    DELETE /employees/employee/1
    

See Also

Elasticsearch Tutorial- Download and install Elasticsearch. Installing the Head Plugin for Elasticsearch. Understanding Elasticsearch Cluster, Node, Index and Document using example. Elasticsearch-Main Menu. Top ElasticSearch frequently asked interview questions