elasticsearch upsert 问题

elasticsearch 新手遇到了小问题:

首先我创建了一个文档:

curl -XPOST http://localhost:9200/blog/article/ -d '{"title":"new version of Elasticsearch released!", "countent":"version 1.0 released today!", "tags":["announce","elasticsearch","release"]}'

然后返回:

{"_index":"blog","_type":"article","_id":"AV05vk4Jp9eqwTx-9EmQ","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}

这个是创建成功了的,然后我尝试使用 upsert 功能:

curl -XPOST http://localhost:9200/blog/article/AV05vk4Jp9eqwTx-9EmQ/_update -d '{"script":"ctx._source.count += 1", "upsert":{"count":0}}'

按照书上敲得,预期是将 count 字段加1,若是不存在,则插入 count 字段并设置为 0,但是报错了:

{"error":{"root_cause":[{"type":"remote_transport_exception","reason":"[neil-1][10.142.14.160:9300][indices:data/write/update[s]]"}],"type":"illegal_argument_exception","reason":"failed to execute script","caused_by":{"type":"script_exception","reason":"runtime error","script_stack":["ctx._source.count += 1","                     ^---- HERE"],"script":"ctx._source.count += 1","lang":"painless","caused_by":{"type":"null_pointer_exception","reason":null}}},"status":400}

求问这是什么原因

阅读 9.5k
3 个回答
curl -XPOST http://localhost:9200/blog/article/AV05vk4Jp9eqwTx-9EmQ/_update -d '{"script":{"inline":"ctx._source.counter += params.count","lang":"painless","params":{"count":4}},"upsert":{"counter":1}}'

这样就 ok 啦

6.6.1

{
    "script" : "ctx._source.new_field = 'value_of_new_field'"
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏