参考1:? ?es nested数组嵌套对象操作 增删改查 - meow_world - 博客园
参考2:??????干货 | Elasticsearch Nested类型深入详解_铭毅天下的博客-CSDN博客_elasticsearch nested
在可视化工具kibana
1、创建索引
#创建索引
PUT /groups
#删除索引
DELETE /groups
#创建type
PUT /groups/group/_mapping
{
"properties": {
"group_id": {
"type": "keyword"
},
"group_name": {
"type": "text"
},
"group_count":{
"type": "long"
},
"group_desc":{
"type": "nested",
"properties": {
"key":{
"type" : "keyword"
},
"value":{
"type" : "keyword"
}
}
},
"user": {
"type": "nested",
"properties": {
"user_id": {
"type": "keyword"
},
"user_name": {
"type": "text"
},
"user_age":{
"type":"long"
},
"user_content": {
"type": "text"
}
}
}
}
}
#查看type
GET groups/group/_mapping
2、初始化数据
#添加数据
POST groups/group/1
{
"group_id": "1001",
"group_name": "聊天1群",
"group_count": 3,
"group_desc":[
{
"key":"k1",
"value":"1"
},
{
"key":"k2",
"value":"2"
},
{
"key":"k3",
"value":"3"
},
{
"key":"k4",
"value":"4"
}
],
"user":[
{
"user_id":"2001",
"user_name":"小李",
"user_age": 21,
"user_content":"小李作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"user_id":"2002",
"user_name":"小王",
"user_age": 22,
"user_content":"小王作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"user_id":"2003",
"user_name":"小张",
"user_age": 23,
"user_content":"小张作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
}
]
}
#添加数据
POST groups/group/2
{
"group_id": "1002",
"group_name": "聊天2群",
"group_count": 3,
"group_desc":[
{
"key":"k1",
"value":"3"
},
{
"key":"k2",
"value":"4"
},
{
"key":"k3",
"value":"5"
}
],
"user":[
{
"user_id":"2021",
"user_name":"二小李",
"user_age": 201,
"user_content":"二小李作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"user_id":"2022",
"user_name":"二小王",
"user_age": 202,
"user_content":"二小王作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"user_id":"2023",
"user_name":"二小张",
"user_age": 203,
"user_content":"二小张作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
}
]
}
#添加数据
POST groups/group/3
{
"group_id": "1003",
"group_name": "聊天3群",
"group_count": 2,
"group_desc":[
{
"key":"k1",
"value":"6"
},
{
"key":"k2",
"value":"1"
}
],
"user":[
{
"user_id":"2031",
"user_name":"三小李",
"user_age": 231,
"user_content":"三小李作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
},
{
"user_id":"2032",
"user_name":"三小王",
"user_age": 232,
"user_content":"三小王作为一级筛选条件单独使用表示,表示只返回聚合结果,不返回具体数据。"
}
]
}
3、查询
#根据嵌套对象外部条件查询
GET groups/group/_search
{
"query": {
"bool": {
"must": [
{ "match": { "group_id": "1002" }}
]
}
}
}
#根据嵌套对象内部数组条件查询
GET groups/group/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{ "match": { "user.user_id": "2031" }}
]
}
}
}
}
}
#根据嵌套对象内部、外部条件查询
GET groups/group/_search
{
"query": {
"bool": {
"must": [
{
"match":
{
"group_id": "1003"
}
},
{
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{
"match":
{
"user.user_id": "2031"
}
}
]
}
}
}
}
]
}
}
}
4、删除
#删除
POST groups/group/3/_update
{
"script": {
"lang": "painless",
"source": "ctx._source.user.removeIf(it -> it.user_id == '2032');"
}
}
#查询删除状态
GET groups/group/_search
{
"query": {
"match": {
"group_id": "1003"
}
}
}
5、修改
#修改user_id等于2031的数据,将user_age改为888
POST groups/group/3/_update
{
"script": {
"source": "for(e in ctx._source.user){if(e.user_id=='2031'){e.user_age=888;}}"
}
}
#查询
GET groups/group/_search
{
"query": {
"match": {
"group_id": "1003"
}
}
}
6、增
#第一种新增方式
POST groups/group/3/_update
{
"script" : {
"source": "ctx._source.user.add(params.user)",
"lang": "painless",
"params": {
"user": {
"user_id":"2033",
"user_name":"三小卡",
"user_age": 233,
"user_content":"不返回具体数据。"
}
}
}
}
#第二中新增方式
POST groups/group/3/_update
{
"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": {
"user_id":"2034",
"user_name":"4卡",
"user_age": 234,
"user_content":"不返回具体数据。"
}
}
}
}
7、聚合查询
参考:Elasticsearch实用的聚合操作Aggs - 知乎
#查询user.user_age最大值、最小值、平均值
GET groups/group/_search
{
"aggs": {
"agg": {
"nested": {
"path": "user"
},
"aggs": {
"min_age": {
"min": {
"field": "user.user_age"
}
},
"max_age":{
"max": {
"field": "user.user_age"
}
},
"avg_age":{
"avg": {
"field": "user.user_age"
}
}
}
}
}
}
#去重计数
GET groups/group/_search
{
"size": 0,
"aggs": {
"agg": {
"cardinality": {
"field": "group_id"
}
}
}
}
#列出全部统计数据:总数、最大值、最小值、平均值、求和
GET groups/group/_search
{
"size": 0,
"aggs": {
"agg": {
"stats": {
"field": "group_count"
}
}
}
}
#范围统计
GET groups/group/_search
{
"size": 0,
"aggs": {
"agg": {
"range": {
"field": "",
"ranges": [
{
"from": 1,
"to": 2
}
]
}
}
}
}
#数量
GET groups/group/_search
{
"size": 0,
"aggs": {
"agg": {
"nested": {
"path": "user"
},
"aggs": {
"agg": {
"terms": {
"field": "user.user_id",
"size": 100
}
}
}
}
}
}
8、聚合查询
#查询group_id等于1002中,统计group_desc.key=k1,并且group_desc.value分别等于1/2/3/4/5的个数。注意group_desc是按照key-value格式存储的。
GET groups/group/_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"group_id": [
"1002"
]
}
}
]
}
},
"aggs": {
"agg": {
"nested": {
"path": "group_desc"
},
"aggs": {
"filter_key": {
"filter": {
"terms": {
"group_desc.key": [
"k1"
]
}
},
"aggs": {
"key": {
"filters": {
"filters": {
"1": {
"term": {
"group_desc.value": "1"
}
},
"2": {
"term": {
"group_desc.value": "2"
}
},
"3": {
"term": {
"group_desc.value": "3"
}
},
"4": {
"term": {
"group_desc.value": "4"
}
},
"5": {
"term": {
"group_desc.value": "5"
}
}
}
}
}
}
}
}
}
}
}
|