# Different aggregation result when filtering just one term

**URL:** <https://discuss.elastic.co/t/different-aggregation-result-when-filtering-just-one-term/132317>\
**Category:** Elasticsearch\
**Created:** [May 17, 2018, 11:14am UTC](https://discuss.elastic.co/t/different-aggregation-result-when-filtering-just-one-term/132317 "2018-05-17T11:14:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kohutovic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohutovic/32/31329_2.png) [@kohutovic](https://discuss.elastic.co/u/kohutovic)\
**Post date:** [May 17, 2018, 11:14am UTC](https://discuss.elastic.co/t/different-aggregation-result-when-filtering-just-one-term/132317/1 "2018-05-17T11:14:01Z")

</div>

Hello,  
I have an aggregation query and it sums just one doc for a particular term but if I query one term it sums over 2 docs. I do not understand why I get a different result with and without query which should not make a difference.

Let say the query is

```
{
  "size": 0,
  
  "aggs": {
    "2": {
      "terms": {
        "field": "round_id",
        "size": 1,
     
        "order": {
          "sum1": "desc"
        }
      },
      "aggs": {
        "sum1": {
          "sum": {
            "field": "real_win"
            
          }
        },
        "sum2": {
          "sum": {
            "field": "real_bet"
 
          }
        }
      }
    }
  },
  
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        },
        {
          "range": {
            "created_at": {
              "gte": "2018-05-17T05:20:20",
              "lte": "2018-05-17T05:30:20"
                        }
          }
        }
      ]
     }
  }
}

```

The result is:

```
"buckets": [
        {
          "key": 1176640530,
          "doc_count": 1,
          "sum1": {
            "value": 5488
          },
          "sum2": {
            "value": 0
          }
        }
      ]

```

When I change match\_all to:

```
"term": {
            "round_id": {
              "value": "1176640530"
            }
          } 

```

where 1176640530 key is the only result from previos aggregation I get different results.

```
"buckets": [
        {
          "key": 1176640530,
          "doc_count": 2,
          "sum1": {
            "value": 5488
          },
          "sum2": {
            "value": 9
          }
        }
      ]

```

Thanks, can anybody give me a hint what can cause the difference, please?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 17, 2018, 1:12pm UTC](https://discuss.elastic.co/t/different-aggregation-result-when-filtering-just-one-term/132317/2 "2018-05-17T13:12:53Z")

</div>

See [notes on accuracy](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-approximate-counts) and how to improve it.

---

<div class="post-metadata">

**Author:** ![kohutovic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohutovic/32/31329_2.png) [@kohutovic](https://discuss.elastic.co/u/kohutovic)\
**Post date:** [May 17, 2018, 2:36pm UTC](https://discuss.elastic.co/t/different-aggregation-result-when-filtering-just-one-term/132317/3 "2018-05-17T14:36:02Z")

</div>

I was reading that part but I actually do not care about document count but about the second aggregation sum2. It returns 0 but one of those 2 docs has it bigger than 0. Is also sum aggregation inaccurate? I think it should not be, right?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 17, 2018, 2:42pm UTC](https://discuss.elastic.co/t/different-aggregation-result-when-filtering-just-one-term/132317/4 "2018-05-17T14:42:34Z")

</div>

The doc count is the size of the document set considered.  
The sum aggregation (and all other metric aggs) only operate on the docs in this set. If you're not considering all the docs your metrics will also be off.

---

<div class="post-metadata">

**Author:** ![kohutovic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kohutovic/32/31329_2.png) [@kohutovic](https://discuss.elastic.co/u/kohutovic)\
**Post date:** [May 17, 2018, 3:14pm UTC](https://discuss.elastic.co/t/different-aggregation-result-when-filtering-just-one-term/132317/5 "2018-05-17T15:14:27Z")

</div>

Thank you for your explanation.

---

<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:** [June 14, 2018, 3:14pm UTC](https://discuss.elastic.co/t/different-aggregation-result-when-filtering-just-one-term/132317/6 "2018-06-14T15:14:30Z")

</div>

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