【问题标题】:Sum for Multiple Ranges on GroupBy Aggregations in ElasticsearchElasticsearch 中 GroupBy 聚合的多个范围的总和
【发布时间】:2016-07-24 08:32:45
【问题描述】:

以下映射在一个字段的多个级别上聚合,使用另一个字段对文档进行分组。

映射:

 {
   'predictions': {
      'properties': {
              'Company':{'type':'string'},
              'TxnsId':{'type':'string'},
              'Emp':{'type':'string'},
              'Amount':{'type':'float'},
              'Cash/online':{'type':'string'},
              'items':{'type':'float'},
              'timestamp':{'type':'date'}
             }
          }
      }

我的要求有点复杂,我需要

  1. 对于每个 Emp(获取不同的员工)
  2. 查看是在线交易还是现金交易
  3. 按项目分组,范围为 0-10,11-20,21-30....
  4. 合计金额

最终输出如下:

>Emp-online-range-Amount       
>a-online-(0-10)-1240$    
>a-online-(21-30)-3543$    
>b-online-(0-10)-2345$    
>b-online-(11-20)-3456$ 

【问题讨论】:

  • Cash/online 的浮点值是多少?

标签: python elasticsearch dsl elasticsearch-query


【解决方案1】:

这样的事情应该可以完成:

{
  "size": 0,
  "aggs": {
    "by_emp": {
      "terms": {
        "field": "Emp"
      },
      "aggs": {
        "cash_online": {
          "filters": {
            "filters": {
              "cashed": {
                "term": {
                  "Cash/online": "cached"
                }
              },
              "online": {
                "term": {
                  "Cash/online": "online"
                }
              }
            }
          },
          "aggs": {
            "ranges": {
              "range": {
                "field": "items",
                "ranges": [
                  {
                    "from": 0,
                    "to": 11
                  },
                  {
                    "from": 11,
                    "to": 21
                  },
                  {
                    "from": 21,
                    "to": 31
                  }
                ]
              },
              "aggs": {
                "total": {
                  "sum": {
                    "field": "Amount"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

【讨论】:

  • 这个运气好吗?
猜你喜欢
  • 2018-11-28
  • 2014-12-29
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
相关资源
最近更新 更多