title: Elasticsearch composite query and aggregation results different doc_count value
body: <p>I am trying to query on my data set with composite query.
  Here is my</p>
  <p><strong>Query 1:</strong></p>
  <pre class="lang-sh prettyprint-override"><code>curl -X POST &quot;localhost:9200/index1-202103/_search?size=0&amp;pretty&quot; -H 'Content-Type: application/json' -d'
  {
    &quot;query&quot;:{
      &quot;bool&quot;:{
        &quot;filter&quot;:[
          {
            &quot;range&quot;:{
              &quot;date&quot;:{
                &quot;gte&quot;:&quot;20210330&quot;,
                &quot;lte&quot;:&quot;20210330&quot;
              }
            }
          },
          {
            &quot;term&quot;:{
              &quot;userid&quot;:&quot;16114&quot;
            }
          },
          {
            &quot;exists&quot;:{
              &quot;field&quot;:&quot;opens&quot;
            }
          },
          {
            &quot;exists&quot;:{
              &quot;field&quot;:&quot;tags&quot;
            }
          }
        ]
      }
    },
    &quot;aggs&quot;:{
      &quot;my_buckets&quot;:{
        &quot;composite&quot;:{
          &quot;sources&quot;:[
            {
              &quot;from_domain_wise&quot;:{
                &quot;terms&quot;:{
                  &quot;field&quot;:&quot;domain&quot;
                }
              }
            },
            {
              &quot;msp_wise&quot;:{
                &quot;terms&quot;:{
                  &quot;field&quot;:&quot;msp&quot;
                }
              }
            },
            {
              &quot;fromaddress_wise&quot;:{
                &quot;terms&quot;:{
                  &quot;field&quot;:&quot;fromaddress&quot;
                }
              }
            },
            {
              &quot;tag_wise&quot;:{
                &quot;terms&quot;:{
                  &quot;field&quot;:&quot;tags&quot;
                }
              }
            },
            {
              &quot;rate_over_time&quot;:{
                &quot;date_histogram&quot;:{
                  &quot;field&quot;:&quot;opens.time&quot;,
                  &quot;interval&quot;:&quot;1h&quot;
                }
              }
            }
          ]
        }
      }
    }
  }'
  </code></pre>
  <p><strong>Query 2</strong></p>
  <pre class="lang-sh prettyprint-override"><code>curl -X POST &quot;localhost:9200/index1-202103/_search?size=0&amp;pretty&quot; -H 'Content-Type: application/json' -d'
  {
    &quot;query&quot;:{
      &quot;bool&quot;:{
        &quot;filter&quot;:[
          {
            &quot;range&quot;:{
              &quot;date&quot;:{
                &quot;gte&quot;:&quot;20210330&quot;,
                &quot;lte&quot;:&quot;20210330&quot;
              }
            }
          },
          {
            &quot;term&quot;:{
              &quot;userid&quot;:&quot;16114&quot;
            }
          },
          {
            &quot;exists&quot;:{
              &quot;field&quot;:&quot;opens&quot;
            }
          },
          {
            &quot;exists&quot;:{
              &quot;field&quot;:&quot;tags&quot;
            }
          }
        ]
      }
    },
    &quot;aggs&quot;:{
      &quot;my_buckets&quot;:{
        &quot;composite&quot;:{
          &quot;sources&quot;:[
            {
              &quot;from_domain_wise&quot;:{
                &quot;terms&quot;:{
                  &quot;field&quot;:&quot;domain&quot;
                }
              }
            },
            {
              &quot;msp_wise&quot;:{
                &quot;terms&quot;:{
                  &quot;field&quot;:&quot;msp&quot;
                }
              }
            },
            {
              &quot;fromaddress_wise&quot;:{
                &quot;terms&quot;:{
                  &quot;field&quot;:&quot;fromaddress&quot;
                }
              }
            },
            {
              &quot;tag_wise&quot;:{
                &quot;terms&quot;:{
                  &quot;field&quot;:&quot;tags&quot;
                }
              }
            }
          ]
        },
        &quot;aggs&quot;:{
          &quot;rate_over_time&quot;:{
            &quot;date_histogram&quot;:{
              &quot;field&quot;:&quot;opens.time&quot;,
              &quot;interval&quot;:&quot;1h&quot;
            }
          }
        }
      }
    }
  }'
  </code></pre>
  <p>Both the results gives output for date histogram with different counts. When I checked, my findings were that Query1 is counting opens.time (FORMAT: 2021-03-30 15:15:45) fields duplicate values also whereas Query2 is counting opens.time only once if hour is same in single doc.</p>
  <p>For example: if doc contains opens: <code>[{ &quot;time&quot;: &quot;2021-03-30 15:20:25&quot; }, { &quot;time&quot;: &quot;2021-03-30 15:45:30&quot; }]</code> then Query1 return <code>doc_count</code> as 2 where as Query2 returns <code>doc_count</code> as 1.</p>
  <p>Can anyone please explain why my query is behaving like this in spite of both the queries having the same goal. I want result which Query2 gives using Query1.</p>
  <p><strong>PS:</strong> Elasticsearch version is 7.10</p>