-1

您好我正在使用 elasticsearch java API 来更新带有脚本的文档。但我得到了例外

Exception in thread "main" MapperParsingException[object mapping for [content] tried to parse field [content] as object, but found a concrete value]
    at org.elasticsearch.index.mapper.DocumentParser.parseObject(DocumentParser.java:215)
    at org.elasticsearch.index.mapper.DocumentParser.parseObjectOrField(DocumentParser.java:308)
    at org.elasticsearch.index.mapper.DocumentParser.parseValue(DocumentParser.java:438)
    at org.elasticsearch.index.mapper.DocumentParser.parseObject(DocumentParser.java:264)
    at org.elasticsearch.index.mapper.DocumentParser.parseDocument(DocumentParser.java:124)
    at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:309)
    at org.elasticsearch.index.shard.IndexShard.prepareIndex(IndexShard.java:580)
    at org.elasticsearch.index.shard.IndexShard.prepareIndexOnPrimary(IndexShard.java:559)
    at org.elasticsearch.action.index.TransportIndexAction.prepareIndexOperationOnPrimary(TransportIndexAction.java:211)
    at org.elasticsearch.action.index.TransportIndexAction.executeIndexRequestOnPrimary(TransportIndexAction.java:223)
    at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:157)
    at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:66)
    at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryPhase.doRun(TransportReplicationAction.java:657)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
    at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryOperationTransportHandler.messageReceived(TransportReplicationAction.java:287)
    at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryOperationTransportHandler.messageReceived(TransportReplicationAction.java:279)
    at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:77)
    at org.elasticsearch.transport.TransportService$4.doRun(TransportService.java:376)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

以下是 ES 中的现有文档

{
  "_index": "index1",
  "_type": "type1",
  "_id": "1",
  "_version": 8,
  "found": true,
  "_source": {
    "content": {
      "contentId": 1,
      "metadata": {
        "title": "content one",
        "duration": 4500
      }
    },
    "custom": {
      "field1": "value1"
    }
  }
}

我想更新“内容”字段如下

"content": {
      "contentId": 1,
      "metadata": {
        "duration": 900
      }
    }

当我使用 REST 调用 (localhost:9200/index1/type1/1/_update) 进行更新时,它工作正常。我在 java API prepareUpdate 中遇到错误。

我有 3 个 java 类。DTO 类具有 Content 对象 Content 类具有 Metadata 对象和 contentId,只要 Metadata 类具有标题(字符串)和持续时间(长)。

下面是要更新的代码

Map<String, Object> params = new HashMap<>();
params.put("contentScript", dto.toString());
Script s = new Script("ctx._source.content=contentScript",ScriptType.INLINE,null,params);
UpdateResponse resp = client.prepareUpdate("index1", "type1", "1").setScript(s).setScriptedUpsert(true).get();

dto 是 DTO 类的对象,并相应地设置值。

请帮忙。

4

1 回答 1

0
params.put("contentScript", dto.toString());

您正在传递一个字符串,它需要一个对象。下面的代码可能会有所帮助。

String script = "ctx._source.pete = jsonMap";
Map<String, Object> jsonMap = new ObjectMapper().readValue(json, HashMap.class);
Map<String, Object> params = ImmutableMap.of("jsonMap", jsonMap);
return new Script(script, ScriptService.ScriptType.INLINE, null, params);

https://discuss.elastic.co/t/how-to-update-nested-objects-in-elasticsearch-2-2-script-via-java-api/43135/2

于 2016-10-13T23:00:58.140 回答