Reputation: 11023
Elasticsearch v. 1.4.1 ("lucene_version": "4.10.2")
I have a document like this:
$ 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"
}
}
I was just trying the upsert example here: 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
> }
> }'
and I am getting this error:
{"error":"ElasticsearchIllegalArgumentException[failed to execute script];
nested: GroovyScriptExecutionException[NullPointerException[Cannot execute null+null]]; ",
"status":400}
Any idea how to fix this?
Upvotes: 0
Views: 1492
Reputation: 52368
That section in the documentation states the following:
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:
From that I understand that that example is for a document that doesn't exist. If it doesn't then the new document will contain counter
field with value 1
. In your case, you already have a document which doesn't contain counter
. And this is a problem, as the new field will not be added to it, that's just for non-existent documents.
In your case, you either need to have counter
field in the already existent document or do something like this:
POST /blog/article/1/_update
{
"script": "if (!ctx._source.counter) {ctx._source.counter = 1};ctx._source.counter += count",
"params": {
"count": 4
}
}
meaning, in your script add the new counter
field and initialize it with 1
and then do the increment.
Upvotes: 0