繁体   English   中英

带有json作为值的Elasticsearch Java API更新API

[英]Elasticsearch java api update API with json as value

curl -XPOST'localhost:9200 / test / type1 / 1 / _update'-d'{“ script”:“ ctx._source.name_of_new_field = \\” value_of_new_field \\“”}'

这是来自Elasticsearch参考站点的一个示例,用于使用新字段更新现有文档。 https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/docs-update.html

我想用“新文件”来更新现有文档,但用json作为“新字段的值”而不是单个字符串作为“新文件的值”。 就像下面一样。

curl -XPOST'localhost:9200 / test / type1 / 1 / _update'-d'{“ script”:“ ctx._source.test = {\\” newTest \\“:\\” hello \\“}”}'

返回如下错误。

{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[Ravage 2099][127.0.0.1:9300][indices:data/write/update[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "reason": "Failed to compile inline script [ctx._source.test2 = {\"test2\":\"hihi\"}] using lang [groovy]",
      "caused_by": {
        "type": "script_exception",
        "reason": "failed to compile groovy script",
        "caused_by": {
          "type": "multiple_compilation_errors_exception",
          "reason": "startup failed:\n4e487d5bc8afde27adf29b77e8427f5da1534843: 1: expecting '}', found ':' @ line 1, column 29.\n   ctx._source.test2 = {\"test2\":\"hihi\"}\n                               ^\n\n1 error\n"
        }
      }
    }
  },
  "status": 400
}

甚至可以使用“ json值”更新现有文档吗? 还是每个更新请求都应具有单个字符串值?

您可以尝试Updates with a partial documenthttps : //www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_updates_with_a_partial_document

喜欢:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
    "test" : {
         "newTest" : "hello"
        }
    }
}'

如果要添加JSON对象,只需使用常规的哈希,就像这样

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{ 
   "script" : "ctx._source.test = ['newTest':'hello']" 
}'

之后您的对象将如下所示

{
    "test": {
        "newTest": "hello"
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM