2017-08-14 72 views
0

我有一個Elasticsearch-DB,並希望將元素(字符串)添加到數組中。在這裏討論https://discuss.elastic.co/t/append-to-existing-field/16423/2之後,我設立了一個玩具模型。 我的init有:Elasticsearch,爲什麼被添加元素始終爲空

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

和更新陣列

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

不幸的是,更新的結果始終是 「零」,而不是 「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 
      ] 
     } 
     } 
    ] 
    } 
} 

我是什麼做錯了?

回答

0

script結構是不正確的,應該是這樣的:

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