0

The following mapping is aggregated on multiple levels on a field grouping documents using another field.

Mapping:

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

My requirement is bit complex, I need to

  1. For each Emp (Getting the distinct employees)
  2. Check whether it is online or cashed transaction
  3. Group by items with the ranges like 0-10,11-20,21-30....
  4. Sum the Amount

Final Output is like:

>Emp-online-range-Amount       
>a-online-(0-10)-1240$    
>a-online-(21-30)-3543$    
>b-online-(0-10)-2345$    
>b-online-(11-20)-3456$ 
Jack Daniel
  • 2,527
  • 3
  • 31
  • 52

1 Answers1

0

Something like this should do the job:

{
  "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"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360