我有个字段是结构如下,里面分别有两个ID,我想先聚合按parent_id聚合出来,再按chirld_id聚合。
JSON该如何写?聚合显示的时候,可以顺便将name显示出来。
JSON该如何写?聚合显示的时候,可以顺便将name显示出来。
"properties":{
"parent_id":{"type":"integer"},
"parent_name":{"type":"keyword"},
"chirld_id":{"type":"integer"},
"chirld_name":{"type":"keyword"}
}
2 个回复
bsll - ES认证考过咯,开心
赞同来自: zplzpl
DELETE /test_agg
PUT /test_agg
{
"mappings": {
"agg_type": {
"properties": {
"all":{
"type": "nested",
"properties": {
"parent_id": {
"type": "integer"
},
"child_id": {
"type": "integer"
}
}
}
}
}
}
}
POST /test_agg/agg_type/1
{
"all":{
"parent_id":1,
"child_id":2
}
}
POST /test_agg/agg_type/2
{
"all":{
"parent_id":1,
"child_id":3
}
}
POST /test_agg/agg_type/3
{
"all":{
"parent_id":2,
"child_id":3
}
}
POST /test_agg/_search
POST /test_agg/agg_type/_search
{
"size": 0,
"aggs": {
"category": {
"aggs": {
"term_list": {
"terms": {
"field": "all.parent_id"
},
"aggs": {
"term_list": {
"terms": {
"field": "all.child_id"
}
}
}
}
},
"nested": {
"path": "all"
}
}
}
}
bsll - ES认证考过咯,开心
赞同来自: zplzpl
上面的demo,可以做如下修改:
DELETE /test_agg
PUT /test_agg
{
"mappings": {
"agg_type": {
"properties": {
"all":{
"type": "nested",
"properties": {
"parent_id": {
"type": "integer"
},
"child_id": {
"type": "integer"
},
"parent_name": {
"type": "string",
"index": "not_analyzed"
},
"child_name": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
POST /test_agg/agg_type/1
{
"all":{
"parent_id":1,
"child_id":2,
"parent_name":"a",
"child_name":"b"
}
}
POST /test_agg/agg_type/2
{
"all":{
"parent_id":1,
"child_id":3,
"parent_name":"c",
"child_name":"d"
}
}
POST /test_agg/agg_type/3
{
"all":{
"parent_id":2,
"child_id":3,
"parent_name":"e",
"child_name":"ff"
}
}
POST /test_agg/_search
POST /test_agg/agg_type/_search
{
"size": 0,
"aggs": {
"category": {
"aggs": {
"term_list": {
"terms": {
"field": "all.parent_id"
},
"aggs": {
"term_list": {
"terms": {
"field": "all.child_id"
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"_source":{
"include":["parent_name","child_name"]
}
}
}
}
}
}
}
},
"nested": {
"path": "all"
}
}
}
}