nullpointerexception - 在upsert上的ElasticsearchIllegalArgumentException

标签 nullpointerexception elasticsearch

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/

相关文章:

java - 当CPU非常繁忙时,我的多线程java程序出现意外异常

java - 对包含空元素的数组进行排序

json - 如何将 ElasticSearch 快照从存储库下载到本地计算机?

elasticsearch - 文档版本控制 Elasticsearch : How do I compare different document versions?

java - 为什么 NullPointerException 未声明为已检查异常

android - 在 android 中将 quickblox 与 Proguard 集成

lucene - elasticsearch 有复合索引吗?

java - ElasticSearch - 无法获得节点锁

elasticsearch - ElasticSearch映射日期

java - 空指针异常、EJB、JPA Web 应用程序