Add normal field filter to nested field aggregation

I have document structure like below in ES:

{
  customer_id: 1,
  is_member: true,
  purchases: [
    {
      pur_id: 1,
      pur_channel_id: 1,
      pur_amount: 100.00,
      pur_date: '2021-08-01'
    },
    {
      pur_id: 2,
      pur_channel_id: 2,
      pur_amount: 100.00,
      pur_date: '2021-08-02'
    }
  ]
},
{
  customer_id: 2,
  is_member: false,
  purchases: [
    {
      pur_id: 3,
      pur_channel_id: 1,
      pur_amount: 200.00,
      pur_date: '2021-07-01'
    },
    {
      pur_id: 4,
      pur_channel_id: 3,
      pur_amount: 300.00,
      pur_date: '2021-07-02'
    }
  ]
}

I want to aggregate sum by purchases.pur_channel_id and also for each sub aggregation I want to add sub sum aggregation on documents that contains "is_member=false", therefore, I composed following query:

{ 
  "size": 0,
  "query": {
     "match_all": {}
   }
 },
"aggs": {
  "purchases": {
    "nested": {
      "path": "purchases"
    },
    "aggs": {
       "pur_channel_id": {
         "terms": {
           "field": "purchases.pur_channel_id",
           "size": 10
         },
         "aggs": {
           "none_member": {
             "filter": {
               "term": {
                 "is_member": false
               }
             },
             "aggs": {
               "none_member_amount": {
                 "sum": {
                   "field": "purchases.pur_amount"
                 }
               }
             }
           },
           "aggs": {
             "pur_channel_amount": {
               "sum": {
                   "field": "purchases.pur_amount"
               }    
             }
           }
         }
       }
    }
  }
}

The query runs success, while I got 0 for all "none_member_amount". I wonder a normal field perhaps can not be added inside of a nested aggregation.

Please help! Thanks.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.