我的文件示例:
[
{
username: 'userA',
action: 'click',
page: 'home'
},
{
username: 'userA',
action: 'click',
page: 'home'
},
{
username: 'userA',
action: 'scroll',
page: 'home'
},
{
username: 'userA',
action: 'click',
page: 'productA'
},
{
username: 'userB',
action: 'scroll',
page: 'productA'
},
...
]
我需要的嵌套聚合示例:
{
userA: {
home: {
click: 2,
scroll: 1
},
productA: {
click: 1
},
},
userB: {
productA: {
scroll: 1
}
}
...
}
到目前为止,我可以使用此代码,但我不知道如何嵌套:
POST /index/_search?size=0
{
"aggs" : {
"usernames" : {
"terms": {
"field" : "username.keyword",
"size": 10000
}
}
}
}
这为我提供了所有用户名,这是一个很好的开始,但是如何获得每个用户名的第二个嵌套聚合?
答案 0 :(得分:1)
这是一个检索所需数据的示例。
Elasticsearch具有盒装格式,用于表示这种带有嵌套存储桶的聚合。
您必须解析响应以准确检索问题中描述的格式:)
POST myindex/_search
{
"size": 0,
"aggs": {
"by_name": {
"terms": {
"field": "username.keyword",
"size": 10
},
"aggs": {
"by_action": {
"terms": {
"field": "action.keyword",
"size": 10
},
"aggs": {
"by_page": {
"terms": {
"field": "page.keyword",
"size": 10
}
}
}
}
}
}
}
}
响应(汇总部分):
"aggregations": {
"by_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "userA",
"doc_count": 3,
"by_action": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "click",
"doc_count": 3,
"by_page": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "home",
"doc_count": 2
},
{
"key": "productA",
"doc_count": 1
}
]
}
}
]
}
},
{
"key": "userB",
"doc_count": 1,
"by_action": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "scroll",
"doc_count": 1,
"by_page": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "productA",
"doc_count": 1
}
]
}
}
]
}
}
]
}
}