使用javascript API从数组中删除满足弹性搜索条件的对象 [英] remove objects from array that satisfying the condition in elastic search with javascript api

查看:49
本文介绍了使用javascript API从数组中删除满足弹性搜索条件的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的弹性搜索文档中有一个数组类型字段,如下所示:-

Suppose I have a array type field in my document of elastic search, like below:-

"prog_lists": [
 {
  "description": "engineer",
  "age": 25,
  "name": "ashu"
 },
 {
  "description": "programmer",
  "age": 26,
  "name": "rajit"
 },
 {
  "description": "designer",
  "age": 27,
  "name": "naveen"
 }
]

我想删除名称等于ashu的对象,或者删除满足查询条件的多个对象,例如年龄大于25岁的

I want to remove objects which have name equals to ashu, or remove multiple object which satisfy the query like whose age is greater then 25

我可以通过指定对象内的所有内容来做到这一点,如下所示:-

I am able to do this by specifying all the things within object like below:-

client.update({
        "index": "daffo_netgear",
        "type": "array",
        "id": "2201",
        "body": {
            "script":"ctx._source.prog_lists.remove(list)",
            "params" : {
                    "list" :{
                        "description": "engineer",
                        "age": 25,
                        "name": "ashu"
                     }
             }
        }})

但是我只想指定姓名或年龄

but i want to do this just specifying name or age

client.update({
        "index": "daffo_netgear",
        "type": "array",
        "id": "2201",
        "body": {
            "script":"ctx._source.prog_lists.remove(list)",
            "params" : {
                    "list" :{
                          "name": "ashu"
                     }
             }
        }})

我的节点模块弹性搜索版本是2.4.2弹性搜索服务器是1.3.2.

My node module elastic search version is 2.4.2 elastic search server is 1.3.2.

推荐答案

我已经回答了,它的解决方案是

POST /twitter/twit/1/_update

{
  "script": "item_to_remove = nil; foreach (item : ctx._source.list) { if (item['tweet_id'] == tweet_id) { item_to_remove=item; } } if (item_to_remove != nil) ctx._source.list.remove(item_to_remove);",
  "params": {"tweet_id": "123"}
}

如果您有多个符合条件的项目,请改用列表:

POST /twitter/twit/1/_update
{
  "script": "items_to_remove = []; foreach (item : ctx._source.list) { if (item['tweet_id'] == tweet_id) { items_to_remove.add(item); } } foreach (item : items_to_remove) {ctx._source.list.remove(item);}",
  "params": {"tweet_id": "123"}
}

这篇关于使用javascript API从数组中删除满足弹性搜索条件的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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