首页 > 其他分享 >elasticsearch nested 增删改(转载)

elasticsearch nested 增删改(转载)

时间:2023-09-26 09:11:58浏览次数:40  
标签:group name nested member source elasticsearch members 增删 id

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

  • 定义缓存结构体(重点关注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" : "李四"
      }
    ]
  }
}
  • 查询含有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" : "李四"
            }
          ]
        }
      }
    ]
  }
}

 

标签:group,name,nested,member,source,elasticsearch,members,增删,id
From: https://www.cnblogs.com/shuaimeng/p/17729337.html

相关文章

  • Logstash报错: Could not index event to Elasticsearch
    Logstash报错:CouldnotindexeventtoElasticsearch 一、环境说明Logstash7.3ElasticSearch7.3二、现象logstash/logs/logstash-plain.log大量警告:ValidationFailed:1:thisactionwouldadd[1]totalshards,butthisclustercurrentlyhas[1000]/[1000]maximum......
  • elasticsearch 基础知识
    正向索引和倒排索引环境下载windows版本HTTPS报错elasticsearch报错[2023-09-24T21:04:50,988][WARN][o.e.h.n.Netty4HttpServerTransport][HOMEDESKTOP-TG]receivedplaintexthttptrafficonanhttpschannel,closingconnectionNetty4HttpChannel于Windows版......
  • Docker 部署 Elasticsearch 8.6.2
    Docker部署Elasticsearch8.6.2dockerpullelasticsearch:8.6.2mkdir-pv/home/zonglin/elasticsearch/pluginssudodockerrun--nameelasticsearch-p9200:9200-p9300:9300\--restart=always\-e"discovery.type=single-node"\-eES_JAVA_......
  • Elasticsearch 常用指令
    Elasticsearch常用指令查询所有节点$curl'http://127.0.0.1:9200/_cat/nodes'192.168.31.127496102.162.112.03dilmrt*node-1查询集群状态$curl-k'http://127.0.0.1:9200/_cluster/health?pretty'{"cluster_name":"docker-clus......
  • ElasticSearch 查询练习
    ......
  • Elasticsearch 配置参数
    Elasticsearch配置参数1.elasticsearch配置文件说明:elasticsearch: bin: lib: modules: logs: plugins: config: elasticsearch.yml#elasticsearch配置文件 jvm.options#jvm配置文件 log4j2.properties#......
  • elasticsearch 集群搭建
    elasticsearch集群搭建elasticsearch.ymlcluster.name:bigdatanode.name:node-1path.data:/usr/local/las/data/elasticsearchpath.logs:/usr/local/las/log/elasticsearchbootstrap.memory_lock:falsebootstrap.system_call_filter:falsenetwork.host:0.0.0.0ne......
  • ElasticSearch RestFul 风格
    ......
  • elasticsearch 自定义字典
    ......
  • Docker 安装 Elasticsearch
    Docker安装Elasticsearch1.拉取镜像:sudodockerpullelasticsearch:8.6.22.创建docker容器挂载目录:sudomkdir-pv/home/zonglin/elasticsearch/configsudomkdir-pv/home/zonglin/elasticsearch/datasudomkdir-pv/home/zonglin/elasticsearch/pluginsmkdir......