繁体   English   中英

ElasticSearch - 追加到整数数组

[英]ElasticSearch - Append to integer array

我是ES的新手,但我已经掌握了它。 它是一个非常强大的软件,但我不得不说文档确实缺乏并且有时令人困惑。

这是我的问题:我有一个整数数组,如下所示:

"hits_history" : [0,0]

我想通过“update_by_query”调用向该数组附加一个整数,我搜索并找到了这个链接: https ://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html这个例子:

POST test/type1/1/_update
{
    "script" : {
        "inline": "ctx._source.tags.add(params.tag)",
        "lang": "painless",
        "params" : {
            "tag" : "blue"
        }
    }
}

所以我试过了:

 curl -XPOST 'localhost:9200/example/example/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
     {
       "script": {
         "inline": "ctx._source.hits_history.add(params.hits)",
         "params": {"hits": 0}
       },
       "query": {
        "match_all": {}
        }
     }
     '

但它给了我这个错误:

 "ctx._source.hits_history.add(params.hits); ",
 "                                   ^---- HERE"
 "type" : "script_exception",
     "reason" : "runtime error",
     "caused_by" : {
       "type" : "illegal_argument_exception",
       "reason" : "Unable to find dynamic method [add] with [1] arguments for class [java.lang.Integer]."

所以,我进一步观察并发现了这个: https//www.elastic.co/guide/en/elasticsearch/guide/current/partial-updates.html

这个例子有:

我们还可以使用脚本向tags数组添加新标记。

 POST /website/blog/1/_update
 {
    "script" : "ctx._source.tags+=new_tag",
    "params" : {
       "new_tag" : "search"
    }
 }

所以我试了一下:

 curl -XPOST 'localhost:9200/example/example/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
 {
   "script": {
     "inline": "ctx._source.hits_history += 0;"
   },
   "query": {
    "match_all": {}
    }
 }
 '

结果:

 "type" : "script_exception",
     "reason" : "runtime error",
     "caused_by" : {
       "type" : "class_cast_exception",
       "reason" : "Cannot apply [+] operation to types [java.util.ArrayList] and [java.lang.Integer]."

那么,我如何将项目附加到arrayList? 我应该研究一下更新的文档吗?

我想做的只是这样: ctx._source.hits_history.add(ctx._source.today_hits); ctx._source.today_hits = 0; ctx._source.hits_history.add(ctx._source.today_hits); ctx._source.today_hits = 0;

谢谢

您应该将第一个值存储为数组(包含一个值)。 然后你可以使用add()方法。

POST /website/blog/1/_update
{
   "script" : "if (ctx._source.containsKey('tags')) { ctx._source.tags.add('next') } else { ctx._source.tags = ['first'] }"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM