0

I'm trying to add a new array field into a document with painless script but failed.

Here is the document to be updated

"_source": {
  "a": {}
}

I want to add a new array b under a. So I have this script

{
  "script": {
    "source": "ctx._source.a.b=[params.id]",
    "lang": "painless",
    "params": {
      "id": "id001"
    }
  }
}

But I got below error

{
    "status": 400, 
    "error": {
        "root_cause": [
            {
                "reason": "object mapping for [a.b] tried to parse field [null] as object, but found a concrete value", 
                "type": "mapper_parsing_exception"
            }
        ], 
        "type": "mapper_parsing_exception", 
        "reason": "object mapping for [a.b] tried to parse field [null] as object, but found a concrete value"
    }
}

I tried to create an empty array by

"ctx._source.a.b=new ArrayList()"

And then append element to the array

"ctx._source.a.b.add(params.id)"

Still got the same error

Could anyone help me point out what the issue is? Many thanks.

Jeff
  • 267
  • 5
  • 20
  • Can you share the mapping of your index? – Val Jun 05 '19 at 09:39
  • @Val you are right, the issue is about the index mapping. I used to created field b and then removed it. Thought a.b is not existed in the final document, it is still defined in the mappings. – Jeff Jun 06 '19 at 01:36
  • Cool, glad you figured it out – Val Jun 06 '19 at 05:18

1 Answers1

0

Found the root cause. a.b is defined in index mappings because I defined it before. Thought the field is deleted from the document then.

Jeff
  • 267
  • 5
  • 20