0

I have an Elasticsearch-DB and want to add elements (strings) to an array. Following the discussion over here https://discuss.elastic.co/t/append-to-existing-field/16423/2 I set up a toy-model. I init with:

curl -XPOST "http://localhost:9200/t/t/1/" -d' 
{ 
"hobbies" : ["a", "b"] 
}' 

and the update the array with

curl -XPOST "http://localhost:9200/t/t/1/_update" -d' 
{ 
"script" : "ctx._source.hobbies.add(params.hobby)", 
"params" : { 
"hobby" : "c" 
} 
}'

Unfortunately the updated result is always "null" and not "c":

curl -XGET 'http://localhost:9200/_search?q=_id:"1"&pretty'

{
  "took" : 29,
  "timed_out" : false,
  "_shards" : {
    "total" : 20,
    "successful" : 20,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "t",
        "_type" : "t",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "hobbies" : [
            "a",
            "b",
            null,
            null,
            null
          ]
        }
      }
    ]
  }
}

What am I doing wrong?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
david
  • 113
  • 10

1 Answers1

1

Your script structure is not correct, it should be like this:

curl -XPOST "http://localhost:9200/t/t/1/_update" -d' 
{ 
   "script" : {
      "inline": "ctx._source.hobbies.add(params.hobby)", 
      "params" : { 
         "hobby" : "c" 
      } 
   }
}'
Val
  • 207,596
  • 13
  • 358
  • 360