如果它不是现有元素,则附加到Elasticsearch字段列表/数组 [英] Append to a Elasticsearch field list/array if it's not an existing element

查看:128
本文介绍了如果它不是现有元素,则附加到Elasticsearch字段列表/数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我们有:

  curl -XPOSThttp:// localhost:9200 / t / t / 1 d'
{
hobbies:[a,b]
}'

我知道我可以这样做来追加爱好:

  curl -XPOSThttp: // localhost:9200 / t / t / 1 / _update-d'
{
script:ctx._source.hobbies + = hobby,
params:{
hobby:c
}
}'

但是我怎么能这样做呢,如果爱好不是'c'而是'b',那么我不会因为爱好而被[a,b,b]结尾那么只有在c不是业余爱好的情况下才附加c?

解决方案

您可以通过修改OP中的脚本来实现相同的一点



示例:

  curl -XPOSThttp:// localhost:9200 / t / t / 1 / _update -d '
{
脚本:if(!ctx._source.hobbies.contains(hobby)){ctx._source.hobbies + = hobby},
params:{
hobby:c
}
}'

上面的例子假设字段的兴趣存在,而且是一个列表,否则你需要加入更多的逻辑来处理这些。


Say we have:

curl -XPOST "http://localhost:9200/t/t/1" -d'
{
     "hobbies" : ["a", "b"]
}'

I know I can do this to append to hobbies:

curl -XPOST "http://localhost:9200/t/t/1/_update" -d'
{
     "script" : "ctx._source.hobbies += hobby",
     "params" : {
         "hobby" : "c"
     }
}'

But how can I do it so if hobby isn't 'c' but is 'b', I won't end up with ["a", "b", "b"] for hobby? So it only appends "c" if "c" isn't a hobby already?

解决方案

You can achieve the same by modifying the script in OP a little

Example :

curl -XPOST "http://localhost:9200/t/t/1/_update -d'
{
     "script" : " if(! ctx._source.hobbies.contains(hobby)){ ctx._source.hobbies += hobby }",
     "params" : {
         "hobby" : "c"
     }
}'

The above example assumes that field hobbies exists and is a List else you would need to incorporate a bit more logic to handle these.

这篇关于如果它不是现有元素,则附加到Elasticsearch字段列表/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆