【发布时间】:2019-03-20 05:37:06
【问题描述】:
我对聚合有点陌生,我想创建一个等效于以下 SQL 的语句:
select fullname, natcode, count(1) from table where birthdate = '18-sep-1993' group by fullname, natcode having count(1) > 2 order by count(1) desc
如您所见,结果按全名和natcode分组,有count>2并按count排序
我已经设法形成以下查询:
{
"size": 0,
"aggs": {
"profs": {
"filter": {
"term": {
"birthDate": "18-Sep-1993"
}
},
"aggs": {
"name_count": {
"terms": {
"field": "fullName.raw"
},
"aggs": {
"nat_count": {
"terms": {
"field": "natCode"
},
"aggs": {
"my_filter": {
"bucket_selector": {
"buckets_path": {
"the_doc_count": "_count"
},
"script": {
"source": "params.the_doc_count>2"
}
}
}
}
}
}
}
}
}
}
}
实现了什么: 它在日期上过滤,在全名(name_count)上创建桶,在 natcode(nat_count)上创建子桶,并在文档计数上过滤 natcode 桶。
这个问题: 我也可以看到空的 name_count 存储桶。我只想要具有所需计数的存储桶。以下是结果示例
"aggregations": {
"profs": {
"doc_count": 3754,
"name_count": {
"doc_count_error_upper_bound": 4,
"sum_other_doc_count": 3732,
"buckets": [
{
"key": "JOHN SMITH",
"doc_count": 3,
"nat_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "111",
"doc_count": 3
}
]
}
},
{
"key": "MIKE CAIN",
"doc_count": 3,
"nat_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "205",
"doc_count": 3
}
]
}
},
{
"key": "JULIA ROBERTS",
"doc_count": 2,
"nat_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
},
{
"key": "JAMES STEPHEN COOK",
"doc_count": 2,
"nat_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
}
在结果中,我不想显示最后两个名字(JULIA ROBERTS 和 JAMES STEPHEN COOK)
另外缺少什么: 组的排序在最后。我希望显示计数最多的组(全名、natcode)
进一步要求: 分组需要在另外几个字段上完成,所以它们就像 4 个字段。
如果我可能使用了任何错误的术语,请原谅。希望您了解需要什么帮助。谢谢
【问题讨论】:
标签: elasticsearch elasticsearch-aggregation