标题翻译
Copy a map into Elastic Search's Update Document request
问题
以下是翻译好的内容:
我想要将一组键值对从 HashMap 添加到 Elastic Search 的 Java 高级 REST 客户端 API 中的 UpdateRequest 函数的请求体中。
我可以成功地遍历 HashMap 并提取出键和值。如何以简洁高效的方式将它们添加到构建器中呢?下面的代码是一个占位符,代表我想要用实际的 HashMap 数据实现的内容。
这是我一直在使用的 Elastic Search 文档。我们使用的是 7.5.2 版本。
链接:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-update.html
public void updateDocument(@PathParam("index_name") String index_name,
@PathParam("document_id") String document_id, HashMap<String, String> columnValues) throws IOException {
RestHighLevelClient client = createHighLevelRestClient();
Set<String> keys = columnValues.keySet();
for (String key : keys) {
System.out.println(key);
System.out.println(columnValues.get(key));
}
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.timeField("updated", new Date());
builder.field("animal", "crocodile");
}
builder.endObject();
UpdateRequest request = new UpdateRequest(
index_name, document_id);
request.docAsUpsert(true).doc(builder);
UpdateResponse updateResponse = client.update(
request, RequestOptions.DEFAULT);
System.out.println(updateResponse);
client.close();
}
如果您需要进一步帮助,请随时提问。
英文翻译
I would like to add a set of key value pairs from a HashMap to the request body of the UpdateRequest function in Elastic Search's Java High Level REST client API.
I can successfully loop through the HashMap and pull out the keys and values. How can I add these to the builder in a clean and efficient way? The
> updated: new Date(), animal: crocodile
code is a placeholder for what I would like to achieve with our real data from the HashMap.
Here is the documentation I have been using from Elastic Search.
We are using version 7.5.2.
public void updateDocument(@PathParam("index_name") String index_name,
@PathParam("document_id") String document_id, HashMap<String, String> columnValues) throws IOException {
RestHighLevelClient client = createHighLevelRestClient();
Set<String> keys = columnValues.keySet();
for (String key : keys) {
System.out.println(key);
System.out.println(columnValues.get(key));
}
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.timeField("updated", new Date());
builder.field("animal", "crocodile");
}
builder.endObject();
UpdateRequest request = new UpdateRequest(
index_name, document_id);
request.docAsUpsert(true).doc(builder);
UpdateResponse updateResponse = client.update(
request, RequestOptions.DEFAULT);
System.out.println(updateResponse);
client.close();
答案1
得分: 0
这个有效...
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
for (String key : keys) {
System.out.println(key);
System.out.println(columnValues.get(key));
builder.field(key, columnValues.get(key));
}
builder.endObject();
英文翻译
This works...
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
for (String key : keys) {
System.out.println(key);
System.out.println(columnValues.get(key));
builder.field(key, columnValues.get(key));
}
builder.endObject();
专注分享java语言的经验与见解,让所有开发者获益!
评论