【发布时间】:2016-09-24 22:38:04
【问题描述】:
我有以下简单的映射:
"element": {
"dynamic": "false",
"properties": {
"id": { "type": "string", "index": "not_analyzed" },
"group": { "type": "string", "index": "not_analyzed" },
"type": { "type": "string", "index": "not_analyzed" }
}
}
这基本上是一种存储Group对象的方法:
{
id : "...",
elements : [
{id: "...", type: "..."},
...
{id: "...", type: "..."}
]
}
我想知道有多少不同的组共享同一组元素类型(有序,包括重复)。
一个明显的解决方案是将架构更改为:
"element": {
"dynamic": "false",
"properties": {
"group": { "type": "string", "index": "not_analyzed" },
"concatenated_list_of_types": { "type": "string", "index": "not_analyzed" }
}
}
但是,由于要求,我们需要能够从 group by (aggregation) 中排除某些类型 :(
文档的所有字段都是 mongo id,所以在 SQL 中我会这样做:
SELECT COUNT(id), concat_value FROM (
SELECT GROUP_CONCAT(type_id), group_id
FROM table
WHERE type_id != 'some_filtered_out_type_id'
GROUP BY group_id
) T GROUP BY concat_value
在具有给定映射的 Elastic 中,过滤掉它真的很容易,假设我们有一个连接值,计算它也不是问题。不用说,sum 聚合不适用于字符串。
我怎样才能让它工作? :)
谢谢!
【问题讨论】:
标签: elasticsearch aggregation concat