[英]Put mapping with Elastic Search's High level REST JAVA client asynchronously - deprecated error
I am following this Elastic Search documentation to create a mapping for my Elastic Search index named "contacts".我正在按照这个 Elastic Search 文档为我的 Elastic Search 索引创建一个名为“contacts”的映射。
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-put-mapping.html
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-put-mapping.html
Running my code results in运行我的代码结果
failure to create mapping: Validation Failed: 1: mapping type is missing;
无法创建映射:验证失败:1:缺少映射类型;
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.
我使用的是 Java 客户端的 7.6.0 版。
Update.更新。 I downgraded to version 7.5.2 of the Java client because this is the version of my Elastic Search deployment.
我将 Java 客户端降级到 7.5.2 版,因为这是我的 Elastic Search 部署版本。 The put mapping command now works.
put mapping 命令现在可以工作了。 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.
如果我取消对该调用的注释,Eclipse 会告诉我该函数已被弃用,我应该使用新函数。 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?
我如何告诉 Eclipse 使用新版本?
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.
应该使用 putMappingAsync(PutMappingRequest, RequestOptions, ActionListener) 方法来代替,它接受一个新的请求对象。
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.indices().putMappingAsync(request, RequestOptions.DEFAULT, listener);
client.close();
}
I think you imported the wrong class.我认为你导入了错误的类。 There are two classes "PutMappingRequest" located in different packages:
有两个类“PutMappingRequest”位于不同的包中:
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.
它由 Java 高级 REST 客户端提供。 To fix the exception "mapping type is missing" , you just need to change your import statement and Eclipse will compile with the correct class.
要修复异常“缺少映射类型” ,您只需更改导入语句,Eclipse 将使用正确的类进行编译。 You don't need to downgrade your Elasticsearch version.
您无需降级 Elasticsearch 版本。
If you check the Java client source code, you will see two methods putMapping(...)
defined with different input parameters.如果您查看 Java 客户端源代码,您将看到使用不同输入参数定义的两个方法
putMapping(...)
。 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:也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.