title: Elastic Query with `aggs` inside `must`
body: <p>Is there any way where we can <code>aggs</code> inside the inside the <code>must</code> .??
  I have a query which hold multiple conditions and one aggregation, and aggregation must be part of <code>AND</code> clause (i mean <code>must</code>)</p>
  <p>query look something like this ...</p>
  <pre><code>{
    &quot;query&quot;: {
      &quot;bool&quot;: {
        &quot;must&quot;: [
          {
            &quot;has_child&quot;: {
              &quot;type&quot;: &quot;actualIndex&quot;,
              &quot;min_children&quot;: 1,
              &quot;query&quot;: {
                &quot;range&quot;: {
                  &quot;actualIndex.datetime&quot;: {
                    &quot;gte&quot;: &quot;now-364d/d&quot;,
                    &quot;lt&quot;: &quot;now/d&quot;
                  }
                }
              }
            }
          },
          {
            &quot;bool&quot;: {
              &quot;must&quot;: [
                {
                  &quot;has_child&quot;: {
                    &quot;type&quot;: &quot;actualIndex&quot;,
                    &quot;min_children&quot;: 3,
                    &quot;query&quot;: {
                      &quot;bool&quot;: {
                        &quot;must&quot;: [
                          {
                            &quot;term&quot;: {
                              &quot;actualIndex.array1.cloumA&quot;: &quot;abc&quot;
                            }
                          },
                          {
                            &quot;term&quot;: {
                              &quot;actualIndex.array1.cloumB&quot;: &quot;xyz&quot;
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          },
          {
            &quot;bool&quot;: {
              &quot;must&quot;: [
                {
                  &quot;has_child&quot;: {
                    &quot;type&quot;: &quot;actualIndex&quot;,
                    &quot;min_children&quot;: 2,
                    &quot;query&quot;: {
                      &quot;terms&quot;: {
                        &quot;actualIndex.array1.mno&quot;: [
                          &quot;value1&quot;,
                          &quot;value2&quot;,
                          &quot;value3&quot;
                        ]
                      }
                    }
                  }
                },
                {
                  &quot;query&quot;: {
                    &quot;range&quot;: {
                      &quot;actualIndex.datetime&quot;: {
                        &quot;gte&quot;: &quot;now-364d/d&quot;,
                        &quot;lt&quot;: &quot;now/d&quot;
                      }
                    }
                  },
                  &quot;aggs&quot;: {
                    &quot;groupby&quot;: {
                      &quot;composite&quot;: {
                        &quot;sources&quot;: [
                          {
                            &quot;ids&quot;: {
                              &quot;terms&quot;: {
                                &quot;field&quot;: &quot;ids&quot;
                              }
                            }
                          }
                        ]
                      },
                      &quot;aggs&quot;: {
                        &quot;total&quot;: {
                          &quot;children&quot;: {
                            &quot;type&quot;: &quot;actualIndex&quot;
                          },
                          &quot;aggs&quot;: {
                            &quot;range_bucket&quot;: {
                              &quot;date_range&quot;: {
                                &quot;field&quot;: &quot;actualIndex.datetime&quot;,
                                &quot;keyed&quot;: true,
                                &quot;ranges&quot;: {
                                  &quot;from&quot;: &quot;now-364d/d&quot;,
                                  &quot;to&quot;: &quot;now&quot;,
                                  &quot;key&quot;: &quot;bucket&quot;
                                }
                              },
                              &quot;aggs&quot;: {
                                &quot;BAS_TOTAL&quot;: {
                                  &quot;sum&quot;: {
                                    &quot;field&quot;: &quot;actualIndex.amountField&quot;
                                  }
                                }
                              }
                            },
                            &quot;sum_intermediate&quot;: {
                              &quot;max_bucket&quot;: {
                                &quot;buckets_path&quot;: &quot;range_bucket&gt;BAS_TOTAL&quot;
                              }
                            }
                          }
                        },
                        &quot;inventory_selector&quot;: {
                          &quot;bucket_selector&quot;: {
                            &quot;buckets_path&quot;: {
                              &quot;total&quot;: &quot;total &gt; sum_intermediate&quot;
                            },
                            &quot;script&quot;: &quot;params.total &gt; 1000&quot;
                          }
                        }
                      }
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
  </code></pre>
  <p>I just need the result of <code>aggs</code> should be the part of <code>must</code> too</p>
  <p>can anybody look help here.</p>
  <p>P.S Score is not important.</p>