1. Overview

Elastic Search is near real-time search engine based on Apache Lucene. Elasticsearch provides a more useable and concise API, scalability, and operational tools on top of Lucene’s search implementation. Today Elastic Search is adopted by many organizations worldwide to solve real-life business problems including Log Indexing and analysis, Full-Text Search, Visualising data for more insights etc.

In our previous Create Index Setting article, we have discussed how to create Index and Index Setting with an example. In this article, we will discuss how to add field type definitions, custom analyzer, normalizer using Elastic Search Transport Client Java API.

The Mappingis similar to the database schema, Solr schema, which describes fields, and it’s various properties like field name, field type, tokenization behavior, how fields are indexed and store etc.

Prior to Elastic Search 6.0, each Index has one or more mapping types that divide the document into logical groups. For example, let’s say we have one index called “Document” and we have created two separate mapping types one is Email and another one is OfficeDocuments. Each type of documents has certain unique properties so we can separate it in two mapping types.

After Elastic Search 6.0, We can only create one mapping type for an index.

2. Elastic Search PUT Mapping

PutMappingRequest class is used to Create Mapping or PUT mapping. Its constructor takes Index Name as an argument.  If the mapping already exists then the new mapping will be merged with an old one and if in certain scenarios some of the elements or field type definition can’t be merged with a new one, so in those cases, the request will be rejected.

Elastic Search support various field data types. We can divide these data types into three main categories.

  1. Simple data types, It includes integer, long, double, date, text, keyword, etc.
  2. hierarchical data types like nested and object
  3. special data type includes completion and geographical location related data types like geo_point and geo_shape.

Now we will discuss various techniques available in Elastic Search Transport Client for Put Mapping with an example.

2.1 PUT Mapping Json Source

PutMappingRequest class has one method called sourcewhich take JSON Source as an argument. Pass PutMappingRequest reference to putMapping method of IndiciesAdminClient class. Refer below code snippets for more details.

public void putMappingJSONSoure() throws ExecutionException, InterruptedException {

    PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
    putMappingRequest.type(indexTypeName)
            .source("{\n" +
                    "      \"properties\": {\n" +
                    "        \"blogId\": {\n" +
                    "          \"type\": \"integer\"\n" +
                    "        },\n" +
                    "        \"isGuestPost\": {\n" +
                    "          \"type\": \"boolean\"\n" +
                    "        },\n" +
                    "        \"voteCount\": {\n" +
                    "          \"type\": \"integer\"\n" +
                    "        },\n" +
                    "        \"createdAt\": {\n" +
                    "          \"type\": \"date\"\n" +
                    "        }\n" +
                    "      }\n" +
                    "}",XContentType.JSON);

    AcknowledgedResponse acknowledgedResponse = client.admin().indices().putMapping(putMappingRequest).get();
    System.out.println("Put Mapping response : " + acknowledgedResponse.isAcknowledged());
}

2.2 PUT Mapping Nested Data Type

Nested Data Type is a specialized version of the object datatype that allows arrays of objects to be indexed in a way that they can be queried independently of each other.  In our example, we have created a nested data type called “comments”  which have two fields username and comments respectively.

public void putMappingNestedDataTypeJSONSoure() throws ExecutionException, InterruptedException {

    PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
    putMappingRequest.type(indexTypeName)
            .source("{\n" +
                    "      \"properties\": {\n" +
                    "        \"blogId\": {\n" +
                    "          \"type\": \"integer\"\n" +
                    "        },\n" +
                    "\t\t\"comments\": {\n" +
                    "          \"type\": \"nested\",\n" +
                    "          \"properties\": {\n" +
                    "            \"username\": {\n" +
                    "              \"type\": \"keyword\"\n" +
                    "            },\n" +
                    "            \"comment\": {\n" +
                    "              \"type\": \"text\"\n" +
                    "            }\n" +
                    "          }\n" +
                    "        }\n" +
                    "      }\n" +
                    "}",XContentType.JSON);

    AcknowledgedResponse acknowledgedResponse = client.admin().indices().putMapping(putMappingRequest).get();
    System.out.println("Put Mapping response : " + acknowledgedResponse.isAcknowledged());
}

2.3 PUT Mapping XContentBuilder

public void putMappingXContentBuilder() throws IOException, ExecutionException, InterruptedException {
        PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);

        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject("properties");
            {
                builder.startObject("blogId");{
                    builder.field("type", "integer");
                }
                builder.endObject();
                builder.startObject("isGuestPost");{
                    builder.field("type", "boolean");
                }
                builder.endObject();
                builder.startObject("voteCount");{
                    builder.field("type", "integer");
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        putMappingRequest.type(indexTypeName)
                .source(builder);

        AcknowledgedResponse acknowledgedResponse = client.admin().indices().putMapping(putMappingRequest).get();
        System.out.println("Put Mapping response : " + acknowledgedResponse.isAcknowledged());

    }

 

2.4 PUT Mapping Map

public void putMappingUsingMap() throws ExecutionException, InterruptedException {
        PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
        Map<String, Object> properties = new HashMap<>();
        Map<String, Object> blogId = new HashMap<>(); //create HashMap for each field
        blogId.put("type", "integer");
        properties.put("blogId", blogId);

        Map<String, Object> isGuestPost = new HashMap<>();
        isGuestPost.put("type", "boolean");
        properties.put("isGuestPost", isGuestPost);

        Map<String, Object> voteCount = new HashMap<>();
        voteCount.put("type", "integer");
        properties.put("voteCount", voteCount);

        Map<String, Object> mappingMap = new HashMap<>();
        mappingMap.put("properties", properties);

        putMappingRequest.type(indexTypeName).source(mappingMap);
        AcknowledgedResponse acknowledgedResponse = client.admin().indices().putMapping(putMappingRequest).get();
        System.out.println("Put Mapping response : " + acknowledgedResponse.isAcknowledged());

    }

3. GET Mapping

Elastic Search transport client Java API provides GetMappingRequesta class to get all the mapping available.  The GetMapping request return GetMappingResponse as a response, we can convert this response using getSourceAsMap to map and do necessary operations.

3.1 Example

public void getMapping() {
        GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
        getMappingsRequest.indices(indexName);
        GetMappingsResponse getMappingsResponse = client.admin().indices().getMappings(getMappingsRequest).actionGet();
        ImmutableOpenMap<String, MappingMetaData> mapping = getMappingsResponse.mappings().get(indexName);
        Map<String, Object> sourceAsMap = mapping.get(indexTypeName).getSourceAsMap();

        sourceAsMap.entrySet().forEach((entry ->{
            System.out.println("Name : "+entry.getKey());
            if(entry.getValue() instanceof Map){

                ((Map<String,Object>)entry.getValue()).entrySet().forEach(s-> System.out.println(s.getKey()+"=>"+s.getValue()));
            }
        }));
}

3.2 Output

Name : properties
blogId=>{type=integer}
createdAt=>{type=date}
isGuestPost=>{type=boolean}
voteCount=>{type=integer}

4. GET Field Mapping

GET field mapping api allows us to get one or more fields mapping instead of all the indices and it’s a field. Get Field mapping is useful when we don’t want to get complete type mapping. GetFieldMapping class is used to get specific fields mapping.

4.1 Example

As you can see in below example we have used two methods indices and fieldsto get a mapping of the blogId field of the given index name. The GetFieldMapping response returns FieldMappingMetadata which provide several methods to get the hashcode and full names of requested fields.

public void getFieldMapping() throws ExecutionException, InterruptedException {

    GetFieldMappingsRequest request = new GetFieldMappingsRequest();
    request.indices(indexName);
    request.fields("blogId");
    GetFieldMappingsResponse getFieldMappingsResponse = client.admin().indices().getFieldMappings(request).get();
    GetFieldMappingsResponse.FieldMappingMetaData blogId = getFieldMappingsResponse.mappings().get(indexName).get(indexTypeName).get("blogId");
    System.out.println("Field Full Name : "+blogId.fullName());
    System.out.println("Field Hash Code : "+blogId.hashCode());

}

4.2 Output

Field Full Name : blogId
Field Hash Code : 1640691220

5. Conclusion

In this article, we have discussed various techniques of put mapping using JSON Source, XContentBuilder, using map, etc.. using Elastic Search Transport Client Java Api. GetMapping and GetFieldMapping to retrieve information of all the fields of given indices or specific fields of indices with an example.

6. References

7. Source Code

You can refer complete example source code in our Git repository.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *