0

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

This is one example from elasticsearch reference site to update existing document with new field. https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

And I want to update existing document with "new filed" but with json as a "value of new field" instead of single string as "value of new filed". just like below.

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

which returns error like below.

{
  "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
}

Is it even possible to update existing document with "json value"? or every update request should have single string value?

2 Answers 2

0

You can try Updates with a partial document: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_updates_with_a_partial_document

Like:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
"doc" : {
    "test" : {
         "newTest" : "hello"
        }
    }
}'
Sign up to request clarification or add additional context in comments.

1 Comment

I'm still getting below.. { "error": { "root_cause": [ { "type": "mapper_parsing_exception", "reason": "failed to parse [test]" } ], "type": "mapper_parsing_exception", "reason": "failed to parse [test]", "caused_by": { "type": "illegal_argument_exception", "reason": "unknown property [newTest]" } }, "status": 400 }
0

If you want to add a JSON object, simply use a groovy hash, like this

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

Your object will look like this afterwards

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

2 Comments

do you call ['newTest': 'hello'] as groovy hash? how is this different from json?
That's the same and will be transformed to JSON when indexed. A groovy hash is equivalent to a JSON hash conceptually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.