elasticsearch nested 增删改(转载)

原文地址:ES nested 数组元素增删改查_es对数组里的元素查询__细水长流的博客-CSDN博客

参考地址:es nested数组嵌套对象操作 增删改查 - meow_world - 博客园 (cnblogs.com)

  • 定义缓存结构体(重点关注nested):
PUT group/_mapping
{
  "properties": {
    "id": {
      "type": "long"
    },
    "name": {
      "type": "text"
    },
    "members": {
      "type": "nested",
      "properties": {
        "id": {
          "type": "long"
        },
        "name": {
          "type": "text"
        }
      }
    }
  }
}
  • 初始化缓存数据
PUT group/_doc/1
{
  "id": 1,
  "name": "Group1"
}

PUT group/_doc/2
{
  "id": 2,
  "name": "Group2",
  "members": [
    {
      "id": 3,
      "name": "王五"
    }
  ]
}
  • 在Group1的members(nested数组)中增加一个成员:郑十
POST group/_update/1
{
  "script": {
    "source": """
        if (ctx._source.members == null) {
            List ls = new ArrayList();
            ls.add(params.member);
            ctx._source.members = ls;
        } else {
            ctx._source.members.add(params.member);
        }
    """,
    "lang": "painless",
    "params": {
      "member": {
        "id": 10,
        "name": "郑十"
      }
    }
  }
}


【注意】:
务必增加if判断,因为member在doc创建时并没有该字段的值,故该字段此时为null,不能直接通过add追加元素,而应创建一个List并赋值过去。

# 确认member已增加:
GET group/_doc/1
{
  ...
  "_source" : {
    "id" : 1,
    "name" : "Group1",
    "members" : [
      {
        "name" : "郑十",
        "id" : 10
      }
    ]
  }
}
  • 在Group1的members(nested数组)中删除一个成员:id=10
POST group/_update/1
{
  "script": {
    "source": "ctx._source.members.removeIf(list_item -> list_item.id == params.member_id)",
    "lang": "painless",
    "params": {
      "member_id": 10
    }
  }
}

# 确认member已删除:
GET group/_doc/1
{
  ...
  "_source" : {
    "id" : 1,
    "name" : "Group1",
    "members" : [
      {
        "id" : 1,
        "name" : "张三"
      },
      {
        "id" : 2,
        "name" : "李四"
      }
    ]
  }
}
  • 在Group1的members(nested数组)中更新id=1的member.name为张三1
POST group/_update/1
{
    "script": {
      "source": "for (item in ctx._source.members) {if(item['id']==params.member_id){item['name']=params.name_new}}",
      "lang": "painless",
      "params": {
        "member_id":1,
        "name_new": "张三1"
      }
    }
}

# 确认member已更新:
GET group/_doc/1
{
  ...
  "_source" : {
    "id" : 1,
    "name" : "Group1",
    "members" : [
      {
        "id" : 1,
        "name" : "张三1"
      },
      {
        "id" : 2,
        "name" : "李四"
      }
    ]
  }
}

#批量修改文档 POST group/_update/5kG_5H8BmwYplCYFsIYV
{
"script": {
"source": "for(def i=0;i<params.update.length;i++)
{
for (item in ctx._source.user)
{
if (item['userId'] == params.update[i].userId)
{
item['userName'] = params.update[i].userName;
item['content'] = params.update[i].content
}
}
}",
"lang": "painless",
"params": {
"update":[
{ "userId":"4005", "userName": "小闫2", "content":"update" },
{ "userId":"4006", "userName": "小闫3", "content":"update" }
]
}
}
}

  • 查询含有id=1的member的Group(重点关注nested):
GET group/_search
{
  "query": {
    "nested": {
      "path": "members",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "members.id": 1
              }
            }
          ]
        }
      }
    }
  }
}

# 查询结果:
{
  ...
  "hits" : {
    ...
    "hits" : [
      {
        "_index" : "group",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "Group1",
          "members" : [
            {
              "id" : 1,
              "name" : "张三1"
            },
            {
              "id" : 2,
              "name" : "李四"
            }
          ]
        }
      }
    ]
  }
}

 

posted @ 2023-09-26 09:03  世人皆萌  阅读(391)  评论(0编辑  收藏  举报