首页 文章

Elasticsearch java api update API,以json为值

提问于
浏览
0

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/en/elasticsearch/reference/current/docs-update.html

我想用“new filed”更新现有文档,但是将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值”更新现有文档吗?或者每个更新请求应该有单个字符串值?

2 回答

  • 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

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

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

    之后您的对象将如下所示

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

相关问题