【发布时间】:2015-10-08 07:53:20
【问题描述】:
我有一个社区列表。我需要创建聚合查询来计算所有具有相同标题的数据。
[
{
"_id": "56161cb3cbdad2e3b437fdc3",
"_type": "Comunity",
"name": "public",
"data": [
{
"title": "sonder",
"creationDate": "2015-08-22T03:43:28 -03:00",
"quantity": 0
},
{
"title": "vule",
"creationDate": "2014-05-17T12:35:01 -03:00",
"quantity": 0
},
{
"title": "omer",
"creationDate": "2015-01-31T04:54:19 -02:00",
"quantity": 3
},
{
"title": "sonder",
"creationDate": "2014-05-22T05:09:36 -03:00",
"quantity": 3
}
]
},
{
"_id": "56161cb3dae30517fc133cd9",
"_type": "Comunity",
"name": "static",
"data": [
{
"title": "vule",
"creationDate": "2014-07-01T06:32:06 -03:00",
"quantity": 5
},
{
"title": "vule",
"creationDate": "2014-01-10T12:40:28 -02:00",
"quantity": 1
},
{
"title": "vule",
"creationDate": "2014-01-09T09:33:11 -02:00",
"quantity": 3
}
]
},
{
"_id": "56161cb32f62b522355ca3c8",
"_type": "Comunity",
"name": "public",
"data": [
{
"title": "vule",
"creationDate": "2014-02-03T09:55:28 -02:00",
"quantity": 2
},
{
"title": "vule",
"creationDate": "2015-01-23T09:14:22 -02:00",
"quantity": 0
}
]
}
]
所以愿望结果应该是
[
{
title: vule,
total: 6
},
{
title: omer,
total: 1
},
{
title: sonder,
total: 1
}
]
我写了一些聚合查询,但它仍然不起作用。我怎样才能得到渴望的结果?
PS:我尝试创建嵌套聚合
ejs.Request().size(0).agg(
ejs.NestedAggregation('comunities')
.path('data')
.agg(
ejs.FilterAggregation('sonder')
.filter(
ejs.TermsFilter('data.title', 'sonder')
).agg(
ejs.ValueCountAggregation('counts')
.field('data.title')
)
)
);
【问题讨论】:
-
发布您的聚合查询。
-
您是否在每个文档中存储了一个对象数组?
-
请提及您尝试过的解决方案、您得到的输出以及映射。
标签: node.js elasticsearch nosql