Elasticsearch中GroupBy聚合的多个范围的总和

时间:2016-07-24 08:32:45

标签: python elasticsearch dsl elasticsearch-query

使用其他字段在字段分组文档的多个级别上聚合以下映射。

映射:

 {
   '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. 总和金额
  5. 最终输出如下:

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

1 个答案:

答案 0 :(得分:0)

这样的事情应该可以胜任:

{
  "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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
相关问题