2016-04-27 72 views
0

卷曲-XPOST '本地主机:9200 /测试/ TYPE1/1/_Update' -D“{ “脚本”: “ctx._source.name_of_new_field = \” value_of_new_field \ “” }'Elasticsearch与JSON作为值的Java API更新API

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

我想更新现有的文件与“新领域”,但与json作为“新领域的价值”而不是单一字符串作为“新领域的价值”。就像下面一样。

卷曲-XPOST '本地主机:9200 /测试/ TYPE1/1/_Update' -D“{ “脚本”: “ctx._source.test = {\” newTest \ “:\” 你好\” }“ }'

它返回下面的错误。

{ 
    "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值”更新现有文档?或者每个更新请求都应该有单个字符串值?

回答

0

您可以尝试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" 
     } 
    } 
}' 
+0

我仍然得到以下.. { “错误”:{ “ROOT_CAUSE”:[{ “类型”: “mapper_parsing_exception”, “理由”: “无法解析[测试]” } ], “类型”: “mapper_parsing_exception”, “原因”: “无法解析[测试]”, “caused_by”:{ “类型”: “illegal_argument_exception”, “原因”:“未知属性[ newTest]“ } }, ”status“:400 } –

0

如果你想添加一个JSON对象,只需使用常规的哈希,这样

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

你的对象将看起来像这样之后

{ 
    "test": { 
     "newTest": "hello" 
    } 
} 
+0

你把['newTest':'hello']称为groovy hash吗?这与json有何不同? –

+0

这是相同的,并将索引时转换为JSON。概念上的散列相当于JSON散列。 – Val