首页 > 解决方案 > 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 个字段。

如果我可能使用了任何错误的术语,请原谅。希望您了解需要什么帮助。谢谢

标签: elasticsearchelasticsearch-aggregation

解决方案


以下是您的查询应该如何。

必填查询(最终答案)

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"
               }
            }
         }
      }
   }
}

希望能帮助到你。


推荐阅读