0

我正在尝试将列表或单个对象添加到弹性搜索索引上的数组字段,索引如下所示:

"_index" : "class_index",
    "_id" : "1234",
    "_source" : {
      "externalId" : 1234,
      "description" : "10th grade",
      "users" : [
        {
          "name" : useer 1,
          "age" : "16",
          "active" : 1
        },
        {
          "name" : user 2,
          "age" : "15",
          "adtive" : 1
        }
      ]
    }

我正在使用 Java 高级 Rest 客户端通过将对象转换为 JsonNode 来将单个对象添加到数组字段。我的问题是两部分,是否可以传递用户对象数组而不是单个对象?另外,我遇​​到了一个问题,如下所示。

          //usere here is a java object 
           Map<String, Object> paramMap = new HashMap<>();
           ObjectMapper mapper = new ObjectMapper();
           //user object: {"name":user name,"agee":"25","active":0}

           JsonNode node = mapper.convertValue(userList.getUsers().get(0), JsonNode.class);
           Map<String, Object> jsonMap = mapper.convertValue(node, Map.class);
            
           final UpdateRequest indexRequest = new UpdateRequest(INDEX, TYPE, _id);

           Script inline = new Script(ScriptType.INLINE, "painless",
                    "ctx._source.characters += params.user)", singletonMap("user", jsonMap));
            indexRequest.script(inline); 
            restClient.update(indexRequest, RequestOptions.DEFAULT);

我不确定我是否在这里遗漏了一些东西,但尝试使用单个对象更新字段时仍然出现以下异常:

caused by: org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=illegal_argument_exception, reason=unexpected token [')'] was expecting one of [{<EOF>, ';'}].]
    at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:509) ~[elasticsearch-6.8.10.jar:6.8.10]
    at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:420) ~[elasticsearch-6.8.10.jar:6.8.10]
    at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:450) ~[elasticsearch-6.8.10.jar:6.8.10]

我们可以在不使用脚本的情况下实现目标吗?

感谢您对此的任何帮助。

4

0 回答 0