Elasticsearch mapping and indexing nested array object with java api.


Nested Object Mapping 

We will explain how create mapping and  indexing nested object in elasticsearch with Java API.Json object is as follows.How can you create mapping and indexing the following json object with java API?
Blog class has  an attribute and array of authors class object.

 {   
   "name":"www.tfasun.com",  
   "authors":[   
    {   
      "name":"tarik",  
      "surname":"fasun",  
      "numberOfArticles":3  
    },  
    {   
      "name":"tarik1",  
      "surname":"fasun1",  
      "numberOfArticles":4  
    }  
   ]  
 }  

  
/**
 * Created by tarik on 23/04/17.
 */
public class Blog implements Serializable {
    private String name;
    private List<Authors> authors;

    public Blog(String name, List authors) {
        this.name = name;
        this.authors = authors;
    }

    public Blog() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List getAuthors() {
        return authors;
    }

    public void setAuthors(List authors) {
        this.authors = authors;
    }
} 

Create a Blog  and Authors class.


 import java.io.Serializable;  
 /**  
  * Created by tarik on 23/04/17.  
  */  
 public class Authors implements Serializable{  
   private String name;  
   private String surname;  
   private int numberOfArticles;  
   public Authors(String name, String surname, int numberOfArticles) {  
     this.name = name;  
     this.surname = surname;  
     this.numberOfArticles = numberOfArticles;  
   }  
   public Authors() {  
   }  
   public String getName() {  
     return name;  
   }  
   public void setName(String name) {  
     this.name = name;  
   }  
   public String getSurname() {  
     return surname;  
   }  
   public void setSurname(String surname) {  
     this.surname = surname;  
   }  
   public int getNumberOfArticles() {  
     return numberOfArticles;  
   }  
   public void setNumberOfArticles(int numberOfArticles) {  
     this.numberOfArticles = numberOfArticles;  
   }  
 }  

Mapping method

Create a mapping as follows.index name is tfasun.

  private void mapping( Client client) {  
     try {  
       client.admin().indices().preparePutMapping("tfasun")  
            .setType("document_type")  
            .setSource(mapping("document_type"))  
            .get();  
     } catch (IOException e) {  
       throw new RuntimeException("Error while mapping nested object", e);  
     }  
   }  

Authors object's type is nested.


  private XContentBuilder mapping(String documentType) throws IOException {  
       XContentBuilder builder = jsonBuilder().prettyPrint().startObject().startObject(documentType);

        builder.startObject("_all").field("enabled", false).endObject()
                  .startObject("properties")

                  .startObject("name")
                  .field("type", "string")
                  .field("index", "not_analyzed")
                  .endObject()

                  .startObject("authors")
                  .field("type", "nested")
                  .startObject("properties")

                  .startObject("name")
                  .field("type", "string")
                  .field("index", "not_analyzed")
                  .endObject()

                  .startObject("surname")
                  .field("type", "string")
                  .field("index", "not_analyzed")
                  .endObject()

                  .startObject("numberOfArticles")
                  .field("type", "integer")
                  .field("index", "not_analyzed")
                  .endObject()

                  .endObject()
                  .endObject()

                  .endObject().endObject();

        return builder;
   }  

The mapping method will generate the following result.
You can analyze the following json object by running this code.


 builder.string(); 

 {  
  “document_type : {  
   "_all" : {  
    "enabled" : false  
   },  
   "properties" : {  
    "name" : {  
     "type" : "string",  
     "index" : "not_analyzed"  
    },  
    "authors" : {  
     "type" : "nested",  
     "properties" : {  
      "name" : {  
       "type" : "string",  
       "index" : "not_analyzed"  
      },  
      "surname" : {  
       "type" : "string",  
       "index" : "not_analyzed"  
      },  
      "numberOfArticles" : {  
       "type" : "integer",  
       "index" : "not_analyzed"  
      }  
     }  
    }  
   }  

Creating Index 

  private IndexResponse getResponse() throws IOException {  
     XContentBuilder contentBuilder = getXContentBuilder();  
     IndexRequestBuilder indexRequestBuilder = client.prepareIndex("tfasun", "document_type");  
     return indexRequestBuilder.setSource(contentBuilder).execute().actionGet();  
   }  



  private XContentBuilder getXContentBuilder() throws IOException {  
     Blog blog=new Blog();  
     blog.setName("www.tfasun.com");  
     blog.setAuthors(new ArrayList<>());  
     blog.getAuthors().add(new Authors("tarik","fasun",3));  
     blog.getAuthors().add(new Authors("tarik1","fasun1",4));  
     XContentBuilder contentBuilder = null;  
     try {  
       contentBuilder = jsonBuilder().prettyPrint().startObject();  
       contentBuilder  
            .field("name", blog.getName());  
       contentBuilder.startArray("authors");  
       for (Authors authors : blog.getAuthors()) {  
         contentBuilder.startObject()  
              .field("name", authors.getName())  
              .field("surname", authors.getSurname())  
              .field("numberOfArticles", authors.getNumberOfArticles())  
              .endObject();  
       }  
       contentBuilder.endArray();  
     } catch (IOException ex) {  
       throw new RuntimeException("Error", ex);  
     }  
     return contentBuilder;  
   }  

Result




Comments

Post a Comment

Popular posts from this blog

JMeter multiple thread group and queue example

Spring data JPA transaction simple Isolation(READ_UNCOMMITTED,READ_COMMITTED,REPEATABLE_READ) example 1.