簡體   English   中英

使用TransportClient更新Java中存儲在ElasticSearch中的數據

[英]Update data stored in ElasticSearch in Java with TransportClient

我正在嘗試使用TranportClient類在Java程序中更新Elastic Search中的數據。 我知道我可以這樣更新:

   XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
       .field("source.ivarId", source.ivarId)
       .field("source.channel", source.channel)
       .field("source.bacId", source.bacId).endObject();
   UpdateResponse response = client.prepareUpdate(_index, _type, _id).setDoc(builder.string()).get();

source是我的用戶定義類,其中包含3個字段: ivarIdchannelbacId

但是我想知道是否有任何方法可以做同樣的事情,但是使用另一種更有效更輕松的方法,這樣我就不需要在類中分配每個字段了? 例如,我可以這樣嗎?

   XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
       .field("source", source).endObject();
   UpdateResponse response = client.prepareUpdate(_index, _type, _id).setDoc(builder.string()).get();

我嘗試了后一種方法,但出現了這個異常:

MapperParsingException[object mapping for [source] tried to parse field [source] as object, but found a concrete value]

我正在使用Java 1.8.0.121ElasticSearchTransportClient的兩個版本均為5.1 謝謝!

答案比我想象的要容易得多。

Gson gson = new Gson();
String sourceJsonString = gson.toJson(updateContent);
UpdateResponse response = client
    .prepareUpdate(_index, "logs", id).setDoc(sourceJsonString).get();

updateContent是包含新數據的對象,只是將其轉換為Json字符串,然后使用它來完成更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM