我正在遵循此Elastic Search文档为名为“ contacts”的Elastic Search索引创建映射。
运行我的代码会导致
无法创建映射:验证失败:1:缺少映射类型;
这是我的代码。
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();
}
这很奇怪,因为我正在遵循文档的示例。我正在使用Java客户端的7.6.0版。
更新。我将Java客户端的版本降级为7.5.2,因为这是我的Elastic Search部署的版本。现在,放置映射命令起作用。但是,我无法使异步调用正常工作。如果我取消对该调用的注释,则Eclipse会告诉我该函数已被弃用,我应该使用新函数。但是,新方法看起来与不推荐使用的方法相同(相同的参数,相同的名称)。有什么不同?以及如何告诉Eclipse使用新版本?
已弃用。此方法使用一个旧的请求对象,该对象仍然引用类型(不推荐使用的功能)。应该改用putMappingAsync(PutMappingRequest,RequestOptions,ActionListener)方法,该方法接受一个新的请求对象。
仅同步之一。
@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);
//这是无效的异步调用。 // client.indices()。putMappingAsync(request,RequestOptions.DEFAULT,listener);
client.close();
}
答案 0 :(得分:0)
我认为您输入了错误的课程。在不同的程序包中有两个类“ PutMappingRequest”:
org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest
org.elasticsearch.client.indices.PutMappingRequest
您应该使用第二个以避免异常。它由Java高级REST客户端提供。要修复“缺少映射类型” 异常,您只需要更改import语句,Eclipse将使用正确的类进行编译。您无需降级Elasticsearch版本。
如果检查Java客户端源代码,将看到两个使用不同输入参数定义的方法putMapping(...)
。它们是重载方法。仅弃用了一个:
/**
* 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 {
...
}
不确定我是否理解异步调用的含义不起作用。这是用法示例:
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
}
});
另请参阅: