elasticsearch - Elasticsearch - 聚合多个字段,按计数过滤并按计数排序
问题描述
我对聚合有点陌生,我想创建一个等效于以下 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 个字段。
如果我可能使用了任何错误的术语,请原谅。希望您了解需要什么帮助。谢谢
解决方案
以下是您的查询应该如何。
必填查询(最终答案)
POST <your_index_name>/_search
{
"size": 0,
"query": {
"bool": {
"filter": {
"term": {
"birthDate": "18-sep-1993"
}
}
}
},
"aggs": {
"groupby_fullname": {
"terms": {
"field": "fullName.raw",
"size": 2000
},
"aggs": {
"natcode_filter": {
"bucket_selector": {
"buckets_path": {
"hits": "groupby_natcode._bucket_count"
},
"script": "params.hits > 0"
}
},
"groupby_natcode": {
"terms": {
"field": "natCode",
"size": 2000,
"min_doc_count": 2
}
}
}
}
}
}
替代解决方案:(类似于选择不同的)
作为最后的手段,我能想到的是做一些类似 select distinct based on 的事情fullName + "_" + natCode
。所以基本上你的钥匙是形式的JOHN SMITH_111
。这确实为您提供了准确的结果,除了键将采用这种形式。
POST <your_index_name>/_search
{
"size":0,
"query":{
"bool":{
"filter":{
"term":{
"birthDate":"18-sep-1993"
}
}
}
},
"aggs":{
"name_count":{
"terms":{
"script":{
"inline":"doc['fullName.raw'].value + params.param + doc['natCode'].value",
"lang":"painless",
"params":{
"param":"_"
}
}
},
"aggs":{
"my_filter":{
"bucket_selector":{
"buckets_path":{
"doc_count":"_count"
},
"script":"params.doc_count > 2"
}
}
}
}
}
}
希望能帮助到你。
推荐阅读
- c++ - C++ 标识符未定义
- php - 想要访问网站上最新的 Instagram 订阅源,但没有收到来自 Instagram 的任何回复
- angular - 提供 Angular 组件使用的专用 api 访问时的热与冷 observables
- google-chrome-extension - 用户单击提交按钮后如何打开多个选项卡?
- synchronization - 在不同的时钟(微控制器)上同步两个 8250 uart
- django - 正常的 django_redis 会生成这种会话密钥吗?
- tensorflow - 为什么函数内部的变量没有初始化?
- python - Flask Talisman 内容安全策略
- windows - msiexec 命令行为注册表中的所有用户安装软件,而不仅仅是管理员?
- sql-server - 有效地从子查询中选择多个列(放置在 SELECT 线索中)