Pages

Monday, 29 July 2013

ElasticSearch & Index Document

How to Index & Search Document in Elastic Search Using the Elastic Search Java API
  • Here we see the example how to insert to the Student  document in Elastic Search Engine & Search through the XContentBuilder.
  • Here are three Class in the Example
  • Student.java
  • Subject.java
  • ElasticSearchExample.java

    Student.java

    package com.piyushramani.example;

    import java.util.List;


    public class Student {


    private int id;

    private String fname;
    private String lname;
    private List<Subject> subjects;
     private String address;

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getFname() {
    return fname;
    }

    public void setFname(String fname) {
    this.fname = fname;
    }

    public String getLname() {
    return lname;
    }

    public void setLname(String lname) {
    this.lname = lname;
    }

    public List<Subject> getSubjects() {
    return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
    this.subjects = subjects;
    }

    public String getAddress() {
    return address;
    }

    public void setAddress(String address) {
    this.address = address;
    }   

    }



    Subject.java

    package com.piyushramani.example;


    public class Subject {


    private int subId ;

    private String subName;

    public int getSubId() {
    return subId;
    }

    public void setSubId(int subId) {
    this.subId = subId;
    }

    public String getSubName() {
    return subName;
    }

    public void setSubName(String subName) {
    this.subName = subName;
    }

    }


    ElasticSearchExample.java

    package com.piyushramani.example;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import org.elasticsearch.action.search.SearchRequestBuilder;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.action.search.SearchType;
    import org.elasticsearch.client.Client;
    import org.elasticsearch.common.xcontent.XContentBuilder;
    import org.elasticsearch.common.xcontent.XContentFactory;
    import org.elasticsearch.index.query.QueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.node.Node;
    import org.elasticsearch.node.NodeBuilder;
    import org.elasticsearch.search.SearchHit;

    public class ElasticSearchExample {

       private static final String INDEX_SOURCE = "STUDENT";
       private static final String INDEX_TYPE = "STUDENT_INFO";
       private Node node;
       private XContentBuilder getContentBuiler(Student student) throws IOException
        {
        XContentBuilder _student = XContentFactory.jsonBuilder();
       
        _student.startObject();
        _student.field("id", student.getId());
        _student.field("fname", student.getFname());
        _student.field("lname", student.getLname());
       
        List<Subject>  subjects =   student.getSubjects();
        _student.startArray("subjects");
        for(Subject subject : subjects) {
        _student.startObject();
        _student.field("subId",subject.getSubId());
        _student.field("subName",subject.getSubName());
          _student.endObject();
        }
        _student.endArray();
       
        _student.endObject();
       
        System.out.println(_student.string());
       
        return _student;
        }

       private Client buildNode() {
       
          node = NodeBuilder.nodeBuilder().build();
         
          return  node.client();
       }
       
       private void nodeClose() {
       
        if(!node.isClosed())
        node.close();
       }
       
    public static void main(String...strings) {

    ElasticSearchExample elasticSearchExample = new ElasticSearchExample();

    Client client =    elasticSearchExample.buildNode();

    List<Subject> subjects = new ArrayList<Subject>();
    Subject subject = new Subject();
    subject.setSubId(new Integer(11));
    subject.setSubName("Programming in Java");
    subjects.add(subject);

    Student student = new Student();
    student.setId(111);
    student.setFname("Piyush");
    student.setLname("Ramani");
    student.setSubjects(subjects);
    student.setAddress("India");

    try {

    //Sucessfully index the student document
    client.prepareIndex(INDEX_SOURCE, INDEX_TYPE, "1").setSource(elasticSearchExample.getContentBuiler(student)).execute().actionGet();

    //How to Search 
    QueryBuilder queryBuilder = QueryBuilders.termQuery("subName", "Java"); 
          SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_SOURCE); 
          searchRequestBuilder.setTypes(INDEX_TYPE); 
          searchRequestBuilder.setSearchType(SearchType.DEFAULT); 
          searchRequestBuilder.setQuery(queryBuilder); 
          searchRequestBuilder.setFrom(0).setSize(60).setExplain(true); 
          SearchResponse resp = searchRequestBuilder.execute().actionGet(); 
          for (SearchHit hit : resp.getHits()) 
           System.out.println("JSON Source: "+hit.getSourceAsString()); 
        
          elasticSearchExample.nodeClose();
    }catch(Exception e) {
    e.printStackTrace();
    }

    }


    }

           

    No comments:

    Post a Comment