2017-03-22 1 views
0

자바 프로그램의 Elastic Search 데이터를 TranportClient 클래스로 업데이트하려고합니다. 나는이 방법으로 업데이트 할 수 있습니다 알고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(); 

나는 후자의 방법을 시도하고, 나는이 예외 가지고 :이 있습니다

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

내가 자바 1.8.0.121를 사용하고, 그리고 ElasticSearch의 두 버전과 TransportClient을 5.1. 감사!

답변

0

대답은 내가 생각했던 것보다 훨씬 쉽습니다.

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

updateContent는 완료, 업데이트 할 것을 사용 후, JSON 문자열로 변환, 새로운 데이터를 포함하는 객체입니다.

관련 문제