Wednesday 6 January 2016

Create explicit mappings in Elasticsearch index

An Elasticsearch index can have more than one type. Each type will have its own set of fields. In this post, let’s see how fields can be mapped to various datatypes in Elasticsearch.

Elasticsearch will do its own mapping. Additionally, we can tell it in which way the fields in a document should be stored, indexed and analyzed.

The field that needs to be treated as string or number or geolocation can be specified. The type of analyzer to be used for a particular field and when it should be applied can be defined in field mapping. (Analyzers will be added as settings to an index and it will be applicable for all types. It need not be added for individual document type in Elasticsearch index.)

Field mapping using REST Service:

curl -XPUT http://localhost:9200/simplyjava/user -d '{
"user":{
            "properties": {
                        "name":{
                        "type":"string","search_analyzer":"a1"
                        },
                        "age":{
                        "type":"long"
                        },
                        "dateofbirth":{
                        "type":"date",
                        "format":"yyyy-MM-dd"
                        },
                        "location":{
                        "type": "geo_point"
                        }
            }
}'

Field mapping using Java API:

XContentBuilder mappingBuilder = XContentFactory.jsonBuilder()
                                                                    .startObject().startObject("user")
                                          .startObject("properties")
                                          .startObject("name")
                                            .field("type", "string")
                                            .field("search_analyzer", "a1")
                                          .endObject()
                                          .startObject("age")
                                            .field("type","long")
                                          .endObject()
                                          .startObject("dateofbirth")
                                            .field("type","date")
                                            .field("format","yyyy-MM-dd")
                                          .endObject()
                                          .startObject("location")
                                            .field("type","geo_point")
                                          .endObject()
                                         .endObject()
                                       .endObject()
                                      .endObject();

PutMappingResponse response =client.admin().indices().preparePutMapping("simplyjava").
setType("user").setSource(mappingBuilder).execute().actionGet();


  Note:
 Mappings of indexed document fields cannot be updated. The documents should be deleted and re-indexed in order to get the mapping type changed for the fields in a type.

No comments:

Post a Comment