【发布时间】:2016-08-20 02:57:45
【问题描述】:
我想类似地查询以下 SQL 查询:
select countryName, count( distinct(countryId) ) as findCount from city group by countryName having findCount > 1
谁知道如何在es中实现?
感谢您的回答!
【问题讨论】:
标签: elasticsearch lucene
我想类似地查询以下 SQL 查询:
select countryName, count( distinct(countryId) ) as findCount from city group by countryName having findCount > 1
谁知道如何在es中实现?
感谢您的回答!
【问题讨论】:
标签: elasticsearch lucene
您可以使用 terms 聚合和 min_doc_count: 2 像这样
{
"size": 0,
"aggs": {
"countries": {
"terms": {
"field": "countryName"
},
"aggs": {
"ids": {
"terms": {
"field": "countryId",
"min_doc_count": 2
}
}
}
}
}
}
请注意,countryName 字段应为 not_analyzed 以使其正常工作,或者 countryName 字段是带有 multi-field 部分的 multi-field。
【讨论】:
count(distinct(countryId)) == count(distinct(countryName)) 对吗?