# Query to display terms associated to a filter aggregation on particular event

**URL:** <https://discuss.elastic.co/t/query-to-display-terms-associated-to-a-filter-aggregation-on-particular-event/157183>\
**Category:** Elasticsearch\
**Created:** [November 17, 2018, 3:57pm UTC](https://discuss.elastic.co/t/query-to-display-terms-associated-to-a-filter-aggregation-on-particular-event/157183 "2018-11-17T15:57:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikhil\_Nambiar](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Nikhil\_Nambiar](https://discuss.elastic.co/u/Nikhil_Nambiar)\
**Post date:** [November 17, 2018, 3:57pm UTC](https://discuss.elastic.co/t/query-to-display-terms-associated-to-a-filter-aggregation-on-particular-event/157183/1 "2018-11-17T15:57:54Z")

</div>

Hi,

I need a help for an elastic search query,

My query below is as follows

```
    {

  "size":0,

"aggs":{

      "group-by-customerid":{

        "terms":{

          "field":"customer_id.keyword"

        },

        "aggs":{

          "card-load":{

            "filter":{

              "term":{

                "Event":2003

              }

            },

            "aggs":{

              "load-balance":{

                "terms":{

                  "field":"balance"

                }

              }

            }

          },

          "reset-cycle":{

            "filter":{

              "term":{

                "Event":4012

              }

            },

            "aggs":{

              "reset-balance":{

                "terms":{

                  "field":"balance"

                }

              }

            }

          }

        }

      }

    }

}

```

This is a working query this query help me to get every customer\_id in my doc who has event 2003 and 4012 with its respective balance associate to the customer\_id.

Now I want to get only those customer\_id's whose balance of card-load event(Event:2003) + balance of reset-cycle event(Event:4012) to be equal to 0.

I used bucket\_selector as below

```
{

  "size":0,

"aggs":{

      "group-by-customerid":{

        "terms":{

          "field":"customer_id.keyword"

        },

        "aggs":{

          "card-load":{

            "filter":{

              "term":{

                "Event":2003

              }

            },

            "aggs":{

              "load-balance":{

                "terms":{

                  "field":"balance"

                }

              },

              "loadBalance1":{

                "bucket_selector":{

                  "buckets_path":{

                    "load-Balance":"load-balance"

                  }

                }

              }

            },

            "loadBalance2":{

              "bucket_selector":{

                "buckets_path":{

                  "load-Balance2":"loadBalance1"

                }

              }

            }

          },

          "reset-cycle":{

            "filter":{

              "term":{

                "Event":4012

              }

            },

            "aggs":{

              "reset-balance":{

                "terms":{

                  "field":"balance"

                }

              },

              "resetBalance1":{

                "bucket_selector":{

                  "buckets_path":{

                    "reset-Balance":"reset-balance"

                  }

                }

              }

            },

            "resetBalance2":{

              "bucket_selector":{

                "buckets_path":{

                  "reset-Balance2":"resetBalance1"

                }

              }

            }

          },

          "ResultBucket":{

            "bucket_selector":{

              "buckets_path":{

                "loadBucket":"loadBalance2",

                "resetBucket":"resetBalance2"

              },

              "script":"params.loadBucket+params.resetBucket==0"

            }

          }

        }

      }

    }

} 

```

And query fails.

I am not getting where I am going wrong

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 15, 2018, 3:58pm UTC](https://discuss.elastic.co/t/query-to-display-terms-associated-to-a-filter-aggregation-on-particular-event/157183/2 "2018-12-15T15:58:06Z")

</div>

Closed on December 15, 2018, 3:58pm UTC

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