json - 如何在Java中使用Elasticsearch jsonBuilder()?

标签 json elasticsearch

我有一个具有以下架构的文档,

 {
    "_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"
   }

并修改“员工”字段的值?

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

我再次尝试:
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();
    }

}

错误信息:
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)

最佳答案

使用json builder时,更新文档很容易。

在您的情况下,内部对象名称是employee,因此您必须将对象命名为“employee”。

以下代码可帮助您了解json构建器的用法。

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

您可以使用以下代码查看生成的json
String output = builder.string();

关于json - 如何在Java中使用Elasticsearch jsonBuilder()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38472394/

相关文章:

ruby-on-rails - 如何在 heroku Elastic Cloud、Elastic Search 5.x 上进行身份验证?

ios - 如何从 JSON 加载并解析到 iOS 5 上的 tableView

javascript - 从数组中的 JSON 对象内部获取记录

javascript - 获取 json 值时未捕获类型错误

elasticsearch - Elasticsearch 分组查询

elasticsearch - 在Elasticsearch中组合查询

elasticsearch - 如何使一个发现表链接到仪表板或其他地方的另一个发现表?

php - 使用 PHP/MySQL 创建 JSON 数据的正确方法

json - 如何将 HAL JSON 数据转换为 jQuery DataTables 的 JSON 数组

elasticsearch - 使用数组方法unique()的脚本更新导致错误