即使是不成熟的尝试,也胜于胎死腹中的策略。

使用filter + composite aggregation的问题

Elasticsearch | 作者 sailershen | 发布于2019年07月23日 | 阅读数:4385

需求:先得到最近1小时的数据,然后用composite aggregation的方法实现ouyu-number字段的分页输出功能:
GET /bj-sip_register/_search
{
"size": 0,
"aggs": {
"today": {
"filter": {
"range": {
"@timestamp" :{
"gte":"now-1h",
"lt":"now"
}
}
},
"aggs": {
"result": {
"composite" : {
"sources": [
{
"ouyu-number": {
"terms": {
"field": "ouyu-number.keyword"
}
}
}
]
}
}
}
}
}
}
输出提示如下,我的理解是filter aggs后面不能跟composite aggs,那么用桶的前提下这个需求该如何实现?谢谢!
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "[composite] aggregation cannot be used with a parent aggregation of type: [FilterAggregatorFactory]"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "bj-sip_register",
"node": "Z7tobMU1RgSwndaSKhdJww",
"reason": {
"type": "illegal_argument_exception",
"reason": "[composite] aggregation cannot be used with a parent aggregation of type: [FilterAggregatorFactory]"
}
}
],
"caused_by": {
"type": "illegal_argument_exception",
"reason": "[composite] aggregation cannot be used with a parent aggregation of type: [FilterAggregatorFactory]",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "[composite] aggregation cannot be used with a parent aggregation of type: [FilterAggregatorFactory]"
}
}
},
"status": 400
}

 
已邀请:

bellengao - 博客: https://www.jianshu.com/u/e0088e3e2127

赞同来自: sailershen

https://www.elastic.co/guide/e ... .html 使用bucket sort 
{
  "size": 0,
  "aggs": {
    "number": {
      "filter": {
        "range": {
         "@timestamp" :{ "gte":"now-1h", "lt":"now" }
        }
      },
      "aggs": {
        "y": {
          "terms": {
            "field": "ouyu-number.keyword"
          },
          "aggs": {
            "sales_bucket_sort": {
              "bucket_sort": {
                "sort": [
                  {
                    "_key": {
                      "order": "asc"
                    }
                  }
                ],
                "from": 1,
                "size": 2
              }
            }
          }
        }
      }
    }
  }
}

sailershen

赞同来自:

bellengao老师,您写的语句里,size值实际只能小于等于10,大于20也只输出10条记录。我看了官网上Bucket Sort Aggreation文档,把size语句放在第2个aggs里,就可以控制每页的实际输出量,比如我写成size:20,输出结果里就有20条记录。
GET /bj-sip_register/_search
{
"size": 0,
"aggs": {
"number": {
"filter": {
"range": {
"@timestamp" :{
"gte":"now-1h",
"lt":"now"
}
}
},
"aggs": {
"y": {
"terms": {
"field": "ouyu-number.keyword",
"size": 20
},
"aggs": {
"sales_bucket_sort": {
"bucket_sort": {
"sort": [
{
"_key": {
"order": "asc"
}
}
],
"from": 0
}
}
}
}
}
}
}
}

要回复问题请先登录注册