I am following this Elastic Search documentation to create a mapping for my Elastic Search index named "contacts".
Running my code results in
failure to create mapping: Validation Failed: 1: mapping type is missing;
Here is my code.
public void createElasticMapping() throws IOException {
RestHighLevelClient client = createHighLevelRestClient();
PutMappingRequest request = new PutMappingRequest("contacts");
ArrayList<Field> fields = new ArrayList<Field>();
fields.add(new Field("list_id", "integer"));
fields.add(new Field("contact_id", "integer"));
Map<String, Object> properties = new HashMap<>();
for (Field fieldToAdd : fields) {
Map<String, Object> fieldData = new HashMap<>();
fieldData.put("type", fieldToAdd.type);
properties.put(fieldToAdd.name, fieldData);
}
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("properties", properties);
request.source(jsonMap);
@SuppressWarnings("deprecation")
org.elasticsearch.action.support.master.AcknowledgedResponse putMappingResponse = client.indices()
.putMapping(request, RequestOptions.DEFAULT);
System.out.print(putMappingResponse);
client.close();
}
This is odd because I am following the documentation's example. I am using version 7.6.0 of the Java client.
Update. I downgraded to version 7.5.2 of the Java client because this is the version of my Elastic Search deployment. The put mapping command now works. However, I cannot get the asynchronous call to work. If I uncomment that call, Eclipse tells me that this function is deprecated and that I should use the new one. However, the new method looks identical to the deprecated method (same parameters, same name). What's the difference? And how do I tell Eclipse to use the new version?
Deprecated. This method uses an old request object which still refers to types, a deprecated feature. The method putMappingAsync(PutMappingRequest, RequestOptions, ActionListener) should be used instead,which accepts a new request object.
Only the synchronous one.
@POST
@Path("/mapping")
public void createElasticMapping() throws IOException {
RestHighLevelClient client = createHighLevelRestClient();
PutMappingRequest request = new PutMappingRequest("contacts");
ArrayList<Field> fields = new ArrayList<Field>();
fields.add(new Field("list_id", "integer"));
fields.add(new Field("contact_id", "integer"));
Map<String, Object> properties = new HashMap<>();
for (Field fieldToAdd : fields) {
Map<String, Object> fieldData = new HashMap<>();
fieldData.put("type", fieldToAdd.type);
properties.put(fieldToAdd.name, fieldData);
}
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("properties", properties);
request.source(jsonMap);
org.elasticsearch.action.support.master.AcknowledgedResponse putMappingResponse = client.indices()
.putMapping(request, RequestOptions.DEFAULT);
System.out.print(putMappingResponse);
// here is the asynchronous call that does not work. // client.indices().putMappingAsync(request, RequestOptions.DEFAULT, listener);
client.close();
}