2016-07-20 12 views
0

JavaでElasticsearch jsonBuilder()を使用する方法は?私は、次のスキーマを持つ文書を持って

{ 
    "_index": "test", 
    "_type": "user", 
    "_id": "2", 
    "_score": 1, 
    "_source": { 
     "id": 2, 
     "accountExpired": false, 
     "accountLocked": false, 
     "dateCreated": "2016-07-19T03:13:07Z", 
     "employee": { 
      "class": "bropen.framework.core.osm.Employee", 
      "id": 1 
     } 
     } 
} 

私は、私が試してみました。この フィールド「従業員」の値を変更したい:

String employee="\"{\"class\":\"bropen.framework.core.osm.Employee\",\"id\":1,\"birthdate\":null,\"code\":null,\"dateCreated\":\"2016-07-19T03:13:07Z\",\"degree\":null,\"disabled\":false,\"email\":null,\"entryDate\":null,\"graduateDate\":null,\"groups\":[],\"identities\":[{\"class\":\"bropen.framework.core.osm.EmployeeIdentity\",\"id\":1},{\"class\":\"bropen.framework.core.osm.EmployeeIdentity\",\"id\":33}],\"identityNumber\":null,\"lastUpdated\":\"2016-07-19T07:58:34Z\",\"level\":0.10,\"location\":null,\"mainIdentityId\":1,\"major\":null,\"mobile\":null,\"name\":\"张三\",\"nation\":null,\"nativePlace\":null,\"notes\":null,\"organization\":{\"class\":\"bropen.framework.core.osm.Organization\",\"id\":2},\"payrollPlace\":null,\"professionalRank\":null,\"qualification\":null,\"rank\":null,\"sequence\":10,\"sex\":null,\"syncId\":null,\"telephoneNumber\":null,\"title\":null,\"type\":1,\"user\":{\"class\":\"bropen.framework.core.security.User\",\"id\":2},\"workingDate\":null,\"workingYears\":null}\""; 


    try { 
      client.prepareUpdate("test", "user", "2") 
        .setDoc(jsonBuilder().startObject().field("testfield", employee).endObject()).get(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
    } 

リターンエラー情報:

java.util.concurrent.ExecutionException: RemoteTransportException[[node-1][192.168.0.224:9300][indices:data/write/update]]; nested: RemoteTransportException[[node-1] 
[192.168.0.224:9300][indices:data/write/update[s]]]; nested: 
MapperParsingException[object mapping for [employee] tried to parse field [employee] as object, but found a concrete value]; 
    at org.elasticsearch.common.util.concurrent.BaseFuture$Sync.getValue(BaseFuture.java:290) 

jsonBuilder()を使用してjsonをこのように構築する方法:

employee:{ 
      "id":"1", 
      "class":"bropen", 
      "name":"Tom" 
    } 

フィールド "employee"の値を変更しますか?

--------------更新------------------

私は再び試みた:

public static void upMethod1() { 

    XContentBuilder json; 
    try { 
     json = jsonBuilder().startObject("employee").field("id", 1) 
       .field("class", "bropen").field("name", "Tom").endObject(); 
     UpdateRequest request = new UpdateRequest(); 
     request.index("test"); 
     request.type("user"); 
     request.id("2"); 
     request.doc(json); 
     client.update(request).get(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 

} 

エラー情報:あなたはJSONビルダーを使用する場合

java.util.concurrent.ExecutionException: RemoteTransportException[[node-1][127.0.0.1:9300][indices:data/write/update]]; nested: RemoteTransportException[[node-1][192.168.0.87:9300][indices:data/write/update[s]]]; nested: NotSerializableExceptionWrapper[not_x_content_exception: Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes]; 
at org.elasticsearch.common.util.concurrent.BaseFuture$Sync.getValue(BaseFuture.java:290) 

答えて

0
json = jsonBuilder().startObject() 
     .startObject("employee") 
     .field("id", 1) 
     .field("class", "bropen") 
     .field("name", "Tom") 
     .endObject() 
     .endObject(); 

UpdateRequest request = new UpdateRequest(); 
      request.index("test"); 
      request.type("user"); 
      request.id("2"); 
      request.doc(json); 


System.out.println(json.toString()); 
client.update(request).get(); 
0

更新文書が簡単です。

あなたのケースでは、内部オブジェクト名は従業員なので、 "employee"という名前のオブジェクトを作成する必要があります。

以下は、json Builderの使用方法を理解するのに役立つコードです。

XContentBuilder json = jsonBuilder() 
     .startObject("employee") 
      .field("id", 1) 
      .field("class", "bropen") 
      .field("name","Tom") 
     .endObject(); 

    UpdateRequest request = new UpdateRequest(); 
    request.index("test"); 
    request.type("user"); 
    request.id("2"); 
    request.doc(json); 

    client.update(request).get(); 

私はあなたの方法を使用して再度試してみましたが、それは動作しませんコード

String output = builder.string(); 
+0

の下に使用して生成されたJSONを見ることができます。質問の最後にエラー情報を追加します。 –

関連する問題