I'm trying to create new Template with ElasticSearch Java API. I've a JSON:
{ "template": "sample*",
"mappings": {
"someType": {
"_all": {
"enabled": false
},
"_id": {
"path": "id"
},
"properties": {
"id": {
"type": "string",
"index": "not_analyzed"
},
"name": {
"type": "string",
"index": "not_analyzed"
},
"someField":{
....
}
}
},
"someOtherType":{
...
},
"someOtherType2":{
...
}
}
}
I wanted to create a template with a name and here is what I've done:
public void putTemplate(Map<String, Object> jsonTemplateAsMap) {
PutIndexTemplateRequestBuilder builder = new PutIndexTemplateRequestBuilder(client.admin().indices(), templateName)
.setTemplate(String.valueOf(jsonTemplateAsMap.get("template")));
Map<String, Object> mapping = (Map<String, Object>) jsonTemplateAsMap.get("mappings");
for (Map.Entry<String, Object> entry : mapping.entrySet()) {
builder.addMapping(entry.getKey(), ((Map<String, Object>) entry.getValue()));
}
builder.get();
}
- Converting JSON String to Map.
- Traversing the map: key is document type, value is its mapping.
This code creates the template but I'm not sure if that's the standard way of creating an index template with Java API. I couldn't find any documentation on this. What is the standard way of doing this, is there any documentation? What I don't like about my current approach is the explicit conversions, which is arguably not very reliable.