2017-03-22 5 views
0

JavaプログラムのElastic SearchのデータをTranportClientクラスで更新しようとしています。私は、このようにUPDATEすることができます知っている:ivarId、チャンネルをbacId:3つのフィールドが含まれて私のユーザー定義クラスであるTransferClientを使用してJavaでElasticSearchに保存されたデータを更新する

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(); 

ソースながら。

しかし、私が知りたいのと同じことを行うことができます任意の方法があるが、私は内側に各フィールドを割り当てる必要がないように、別の効率的より簡単に方法を使用してクラス?たとえば、私はこれを行うことはできますか?

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

私は後者の方法を試みたが、私はこの例外だ:私はのJava 1.8.0.121を使用していて、ElasticSearchのtransportClientの両方のバージョンがある

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

を5.1。ありがとう!

答えて

0

答えは私が思ったよりずっと簡単です。

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

updateContentだけで行われ、更新することを使用し、その後、JSON文字列に変換するために、新しいデータが含まれていたオブジェクトです。

関連する問題