Elasticsearch 1.4.1版(“lucene_version”:“4.10.2”)
我有一个像这样的文件:
$ curl 'http://localhost:9200/blog/article/1'
{
"_index":"blog",
"_type":"article",
"_id":"1",
"_version":4,
"found":true,
"_source":{
"id":"1",
"title":"First Elasticsearch doc!",
"testfield":"abcd"
}
}
我只是在这里尝试upsert示例:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-update.html#upserts
$ curl -XPOST 'http://localhost:9200/blog/article/1/_update' -d '{
> "script" : "ctx._source.counter += count",
> "params" : {
> "count" : 4
> },
> "upsert" : {
> "counter" : 1
> }
> }'
我收到此错误:
{"error":"ElasticsearchIllegalArgumentException[failed to execute script];
nested: GroovyScriptExecutionException[NullPointerException[Cannot execute null+null]]; ",
"status":400}
任何想法如何解决这个问题?
最佳答案
文档中的该部分说明以下内容:
There is also support for upsert. If the document does not already exists, the content of the upsert element will be used to index the fresh doc:
从中我了解到该示例适用于不存在的文档。如果不是,那么新文档将包含
counter
字段,值为1
。就您而言,您已经有一个不包含counter
的文档。这是一个问题,因为不会将新字段添加到其中,这仅适用于不存在的文档。在您的情况下,您需要在已经存在的文档中具有
counter
字段,或者执行以下操作:POST /blog/article/1/_update
{
"script": "if (!ctx._source.counter) {ctx._source.counter = 1};ctx._source.counter += count",
"params": {
"count": 4
}
}
意思是,在脚本中添加新的
counter
字段,并使用1
对其进行初始化,然后进行增量。
关于nullpointerexception - 在upsert上的ElasticsearchIllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27318220/