Elastic Search Introduction

ES ( Elastic Search ) is an open-source search engine built on top of Apache Lucene. ES is very useful for big projects with lots and lots of data. It has following features:

  1. It is a real time document storage where every field is indexed and searchable.
  2. It is capable of scaling to large number of servers and can handle huge structured or unstructured data
  3. It provides real time analytics
  4. Provides full text search capability
  5. Clustered structure allows high availability as failed or new nodes are detected automatically

To learn more about Elastic Search, click here

Elastic Search Keywords

Node   :     When we start an instance of ElasticSearch, we are starting a node. A collection of connected nodes is called a cluster. If we are running a single node of ElasticSearch, then we have a cluster of one node. Every node in the cluster can handle HTTP and Transport traffic. To learn more about nodes, click here.

Index  :     An index is like a database in a relational database management systems. It has a mapping which defines multiple types.

Type    :     A type represents a class of similar documents (similar to a table)

Setting up Elastic Search on Windows with Java

Let’s check out how we can setup and use ES step by step.

  1. Download the latest version of ES from here
  2. If you downloaded zip, extract it somewhere like in c:\elasticsearch-2.2.1
  3. Make sure your JAVA_HOME environment variable points to your Java installation
  4. Start a command prompt and go to the directory: c:\elasticsearch-2.2.1\bin and execute the command:  elasticsearch.bat. This will start the elastic search server.
  5. You can go to the browser and enter the following URL:
     http://localhost:9200

    At this moment, elastic search is running and ready to serve.
    Once ElasticSearch is up and running, we can talk to it using JSON based REST API.

Using Postman to talk to ES

Postman is a Google Chrome browser extension that we can use to make JSON based REST API calls.

Open Postman:-

To create a new index named blog, choose PUT as the method and enter following URL:

http://localhost:9200/blog

This will create a new index named blog and you will see an acknowledgement.

Create a type

Now we have to create a new type (like table) to store our data. Now, we will create a user type with name, gender as properties.

In Postman, choose method as PUT, enter the URL:

http://localhost:9200/blog/user/_mapping

Click raw and choose JSON instead of Text. Now enter the following JSON:

{
   "user": {
      "properties": {
          "name": {"type": "string"},
          "gender": {"type": "string"}
       }
   }
}

Click on the Send button and you will see the acknowledgement.

Create a record

To create a new record, enter the following URL and set the type as PUT:

http://localhost:9200/blog/user/1

and in the JSON section, enter:

{"name": "Ashutosh", "gender": "Male"}

Click on the Send button to see the acknowledgement.

Find a record

To find a record by id, enter the following URL with a GET request:

http://localhost:9200/blog/user/1

Using Java to talk to ES

Let us now check a Java example for working with ES. Here are the steps:

  1. Create a new Maven based Java project in Eclipse.File -> New -> Project -> Maven ProjectUse default Workspace locationmaven-archetype-quickstart
    Group Id: test , Artifact Id: myelastic
  2. Add dependencies to POM file:
    <dependencies>
    
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.2.1</version>
        </dependency>
    
        <dependency>
           <groupId>com.sun.jna</groupId>
           <artifactId>jna</artifactId>
           <version>3.0.9</version>
        </dependency>
    
    </dependencies>
  3. Create Java files in src/main/java under package test.myelastic

The following class contains methods to work with Elastic Search:

Data.java

package test.myelastic;

import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
import java.util.Map;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.search.SearchHit;

public class Data {

    private static String homePath = "C:/elasticsearch-2.2.1";  //location of elastic search
    private static String index = "db";
    private static String type = "students";

    public static Client getClient(){
 
       Node node = nodeBuilder().settings(Settings.builder().put("path.home", homePath)).node();
 
       Client client = node.client();
 
       return client;
    }
 
    public static Map<String, Object> getDocument(String id) {

       Client client = getClient();

       GetResponse getResponse = client.prepareGet(index, type, id).execute().actionGet();

       Map<String, Object> document = getResponse.getSource();

       return document;
    }

    public static void putDocument(Map<String, Object> document) {

       Client client = getClient();

       client.prepareIndex(index, type).setSource(document).execute();
    }

    public static SearchHit[] findByGender(String gender) {

       Client client = getClient();

       SearchResponse response = null;

       if (gender != null) {
            response = client.prepareSearch(index)
            .setTypes(type)
            .setSearchType(SearchType.QUERY_AND_FETCH)
            .setQuery(QueryBuilders.matchQuery("gender", gender))
            .execute()
            .actionGet();

            SearchHit[] results = response.getHits().getHits();

            if (results != null && results.length > 0)
               return results;
            else
               return null;
        } else
            return null;
    }

    public static SearchHit[] readDocuments() {

        Client client = getClient();

        SearchResponse response = client.prepareSearch(index)
                                 .setTypes(type)
                                 .setSearchType(SearchType.QUERY_AND_FETCH)
                                 .execute()
                                 .actionGet();

        SearchHit[] results = response.getHits().getHits();

        if (results != null && results.length > 0)
            return results;
        else
            return null;
    }
}

App.java

This is the main class that will use methods of Data.java

package test.myelastic;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.search.SearchHit;

public class App{
 
   public static void main(String[] args) throws IOException {

       saveDocument();
 
       readDocuments();
 
       findDocuments();
   }

   private static void readDocuments() {

       SearchHit[] documents = Data.readDocuments();
 
       if(documents==null){
            System.out.println("No documents found");
            return;
       }
 
       for (SearchHit hit : documents) {

           Map<String,Object> result = hit.getSource(); 
 
           System.out.println(result);
 
           System.out.println("-------------------------");
       }
   }

   private static void findDocuments() {

       SearchHit[] documents = Data.findByGender("Male");
 
      if(documents==null){
          System.out.println("No documents found");
          return;
      }
 
      for (SearchHit hit : documents) {

          Map<String,Object> result = hit.getSource(); 
 
          System.out.println(result);
 
          System.out.println("-------------------------");
      }
   }

   private static void saveDocument() {

       Map<String, Object> jsonDocument = new HashMap<String, Object>();

       jsonDocument.put("name", "Amit");
       jsonDocument.put("gender", "Male");
       jsonDocument.put("createDate", new Date());

       Data.putDocument(jsonDocument);

       jsonDocument = new HashMap<String, Object>();

       jsonDocument.put("name", "Shiva");
       jsonDocument.put("gender", "Male");
       jsonDocument.put("createDate", new Date());

       Data.putDocument(jsonDocument);

       jsonDocument = new HashMap<String, Object>();

       jsonDocument.put("name", "Shruti");
       jsonDocument.put("gender", "Female");
       jsonDocument.put("createDate", new Date());

       Data.putDocument(jsonDocument);
    } 
}

Now run your application to see the result.