Reputation: 1531
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();
}
Upvotes: 4
Views: 4541
Reputation: 5552
I think you imported the wrong class. There are two classes "PutMappingRequest" located in different packages:
org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest
org.elasticsearch.client.indices.PutMappingRequest
You should use the 2nd one to avoid the exception. It is provided by the Java High Level REST Client. To fix the exception "mapping type is missing", you just need to change your import statement and Eclipse will compile with the correct class. You don't need to downgrade your Elasticsearch version.
If you check the Java client source code, you will see two methods putMapping(...)
defined with different input parameters. They are overloaded methods. Only one is deprecated:
/**
* Updates the mappings on an index using the Put Mapping API.
* ...
*
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
* {@link #putMapping(PutMappingRequest, RequestOptions)} should be used instead, which accepts a new request object.
*/
@Deprecated
public AcknowledgedResponse putMapping(
org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest putMappingRequest,
RequestOptions options) throws IOException {
...
}
public AcknowledgedResponse putMapping(
PutMappingRequest putMappingRequest,
RequestOptions options) throws IOException {
...
}
Not sure I understood what you mean by the asynchronous call does not work. Here's an example of usage:
client
.indices()
.putMappingAsync(
request,
RequestOptions.DEFAULT,
new ActionListener<>() {
@Override
public void onResponse(AcknowledgedResponse r) {
// TODO handle response here
}
@Override
public void onFailure(Exception e) {
// TODO handle failure here
}
});
See also:
Upvotes: 3