es nested数组嵌套对象操作 增删改查
目录:
一、需求
二、数据演示
三、关于es painless 说明
四、参考文章链接
一、需求
对nested(数组)进行操作:
- 增加元素
- 删除元素
- 更新某个元素的值
- 查询包含有指定特性(如id)的缓存
二、数据演示
PUT /group
{
"mappings": {
"properties": {
"groupId": {"type": "text"},
"groupName": {"type": "keyword"},
"user": {
"type": "nested",
"properties": {
"userId": {
"type": "text"
},
"userName": {
"type": "text"
},
"content": {
"type": "keyword"
}
}
}
}
}
}
#查询定义的结构
GET group/_mapping
#插入数据
POST group/_doc
{
"groupId": "1001",
"groupName": "聊天1群",
"user":[
{
"userId":"2001",
"userName":"小李",
"content":"作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"userId":"2002",
"userName":"小王",
"content":"作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"userId":"2003",
"userName":"小张",
"content":"作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
}
]
}
POST group/_doc
{
"groupId": "1002",
"groupName": "聊天2群",
"user":[
{
"userId":"3001",
"userName":"小李2",
"content":"2作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"userId":"3002",
"userName":"小王2",
"content":"2作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"userId":"3003",
"userName":"小张2",
"content":"2作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
}
]
}
#查询数据
GET group/_search
#根据嵌套对象外部条件查询
GET group/_search
{
"query": {
"bool": {
"must": [
{ "match": { "groupId": "1002" }}
]
}
}
}
#根据嵌套对象内部数组条件查询
GET group/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.userId": "3001" }}
]
}
}
}
}
}
#根据嵌套对象内部数组条件查询且有外部条件
GET group/_search
{
"query":{
"bool":{
"must":[
{"match":{"groupName":"聊天"}},
{
"nested":{
"path":"user",
"query":{
"bool":{
"must":[
{ "match": { "user.userId": "3001" }}
]
}
}
}
}
]
}
}
}
#新增 第一种写法
POST group/_update/50Bh5H8BmwYplCYFGcvg
{
"script": {
"source": """
if (ctx._source.user == null) {
List ls = new ArrayList();
ls.add(params.user);
ctx._source.user = ls;
} else {
ctx._source.user.add(params.user);
}
""",
"lang": "painless",
"params": {
"user": {
"userId":"3004",
"userName":"小闫",
"content":",不返回具体数据。"
}
}
}
}
#新增 第二种写法
POST group/_update/50Bh5H8BmwYplCYFGcvg
{
"script" : {
"source": "ctx._source.user.add(params.user)",
"lang": "painless",
"params": {
"user": {
"userId":"3005",
"userName":"小卡",
"content":"不返回具体数据。"
}
}
}
}
#删除文档
POST group/_update/50Bh5H8BmwYplCYFGcvg
{
"script": {
"source": "ctx._source.user.removeIf(item -> item.userId == params.userId)",
"lang": "painless",
"params": {
"userId": "3005"
}
}
}
#遍历修改文档
POST group/_update/50Bh5H8BmwYplCYFGcvg
{
"script": {
"source": "for (item in ctx._source.user) { if (item['userId'] == params.userId) { item['userName'] = params.userName;item['content'] = params.content}}",
"lang": "painless",
"params": {
"userId":"3004",
"userName": "小闫2",
"content":"update"
}
}
}
#根据下标修改内部文档
POST group/_update/qpYS5H8BavSRcXzlpytr
{
"script": {
"lang": "painless",
"source": "ctx._source.user[1].content = params.text",
"params": {
"text": "blue"
}
}
}
#批量修改文档
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"
}
]
}
}
}
#动态修改字段值
POST group/_update/mEqhIIABmwYplCYFctest
{
"script": {
"source": "for (item in ctx._source.user) { if (item['id'] == params.update.id) { item[params.update.field] = params.update.value;}}",
"lang": "painless",
"params": {
"update":{
"id":"1001",
"field":"userName",
"value": "test"
}
}
}
}
三 painless 说明
上面的演示数据中 script 是 painless的语法,painless是es中对脚本支持较好的。
四、参考文章
#查看插件
GET /_cat/plugins
#查看索引
GET _cat/indices
#删除索引
DELETE listen_speech_result_srt_index
#查询索引结构
GET listen_speech_result_doc_index/_mapping
#查询索引数据
GET listen_speech_result_doc_index_dev/_search
#根据id删除数据
DELETE listen_speech_result_index_dev/_doc/1001
#分词效果
GET _analyze
{
"analyzer": "standard",
"text": "奥迪a4l"
}
#修改索引结构
POST _reindex
{
"source": {
"index": "my_index"
},
"dest": {
"index": "my_index2"
}
}
#修改内嵌数组长度限制 默认10000
PUT listen_speech_result_index/_settings
{
"index.mapping.nested_objects.limit":50000
}
touch fish