# Subtract two doc counts from one another

**URL:** https://discuss.elastic.co/t/subtract-two-doc-counts-from-one-another/52865
**Category:** Elasticsearch
**Created:** [June 15, 2016, 1:10pm UTC](https://discuss.elastic.co/t/subtract-two-doc-counts-from-one-another/52865 "2016-06-15T13:10:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lwintergerst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lwintergerst/32/18164_2.png) [@lwintergerst](https://discuss.elastic.co/u/lwintergerst)
#### Post date: [June 15, 2016, 1:10pm UTC](https://discuss.elastic.co/t/subtract-two-doc-counts-from-one-another/52865/1 "2016-06-15T13:10:30Z")

</div>

Hi,  
I'm currently trying to find out how large the difference between the number of two seperate events is.  
This is the response I get when I search for my events:  
`"aggregations": { "aggname": { "buckets": { "event1": { "doc_count": 121 }, "event2": { "doc_count": 119 } } } }`

I got this result by running  
`"aggs": { "aggname": { "filters": { "filters": { "event1": { "query": { "query_string": { "query": "event1" } } }, "event2": { "query": { "query_string": { "query": "event2" } } } } } } }`

I'm now trying to calculate the difference, but I cant get the bucket path to work...  
I tried doing it like this and a few other ways, but had no success so far:  
`"aggs": { "aggname": { "filters": { "filters": { "event1": { "query": { "query_string": { "query": "event1" } } }, "event2": { "query": { "query_string": { "query": "event2" } } } } }, "aggs": { "aggscript": { "bucket_script": { "buckets_path": { "my_var1": "event1>count", "my_var2": "event2>count" }, "script": "my_var1 - my_var2" } } } } }`

---

<div class="post-metadata">

### Author: ![paulkeogh](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@paulkeogh](https://discuss.elastic.co/u/paulkeogh)
#### Post date: [June 21, 2016, 10:56am UTC](https://discuss.elastic.co/t/subtract-two-doc-counts-from-one-another/52865/2 "2016-06-21T10:56:16Z")

</div>

I think this won't work because the bucket script aggregation is a parent aggregation and your example does not have it structured as such.

I have exactly the same requirement so if anyone else knows how to do this without writing a custom client, please suggest !

---

<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: [July 5, 2017, 10:41pm UTC](https://discuss.elastic.co/t/subtract-two-doc-counts-from-one-another/52865/3 "2017-07-05T22:41:47Z")

</div>


