# Use dfs\_query\_then\_fetch with aggregation

**URL:** <https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210>\
**Category:** Elasticsearch\
**Created:** [January 12, 2018, 5:27am UTC](https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210 "2018-01-12T05:27:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [January 12, 2018, 5:27am UTC](https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210/1 "2018-01-12T05:27:31Z")

</div>

I am trying to use **term aggregation** with **dfs\_query\_then\_fetch** in order to get accurate values but still i am not getting accurate count.

**QUERY:**

```
GET demo/_search?pretty=true&search_type=dfs_query_then_fetch
{
    "aggs" : {
        "products" : {
            "terms" : {
                "field" : "demo1.keyword",
                "size" : 5
            }
        }
    }
}

```

**Result** :

```
"aggregations": {
    "products": {
      "doc_count_error_upper_bound": 10959,
      "sum_other_doc_count": 10454016,
      "buckets": [
        {
          "key": "SK148",
          "doc_count": 4442
        },
        {
          "key": "SK67",
          "doc_count": 4432
        },
        {
          "key": "SK489",
          "doc_count": 4420
        },
        {
          "key": "SK592",
          "doc_count": 2245
        },
        {
          "key": "SK88",
          "doc_count": 2245
        }
      ]
    }
  }

```

I believe while using **dfs\_query\_then\_fetch** with **term aggregation** i will get **doc\_count\_error\_upper\_bound** with **zero** value.

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [January 19, 2018, 1:45pm UTC](https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210/2 "2018-01-19T13:45:23Z")

</div>

no term\_aggregations won't change it's accuracy if you use DFS this only applies to term statistics for fulltext search.

---

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [January 23, 2018, 5:05pm UTC](https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210/3 "2018-01-23T17:05:16Z")

</div>

> [@s1monw](#):
>
> use

Is there any way to get accurate term\_aggregations?

---

<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:** [January 23, 2018, 5:19pm UTC](https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210/4 "2018-01-23T17:19:58Z")

</div>

> [@ankur\_singla](#):
>
> Is there any way to get accurate term\_aggregations?

Your request is for the top 5 terms. I'm not clear how many shards you have (that bit is missing from the results you shared).

Each shard returns a multiple of 5 for the number of top-results with the hope that this provides sufficient results to arrive at an overall accurate top 5. It is possible however that the default selection is not large enough to deliver a fully accurate response and this is reflected in the `doc_count_error_upper_bound` response . If you manually increase the selection size using the [shard\_size](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_shard_size_3) setting you can reduce the error bounds to a desirable level (eg zero error).

---

<div class="post-metadata">

**Author:** ![ankur\_singla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankur_singla/32/45612_2.png) [@ankur\_singla](https://discuss.elastic.co/u/ankur_singla)\
**Post date:** [January 23, 2018, 6:16pm UTC](https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210/5 "2018-01-23T18:16:59Z")

</div>

I have 6 shards but that is just for the example my actual purpose is to get accurate term\_aggregation. **Top 5** is a **variable** it can be top 10, top 2 like that.

---

<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:** [January 23, 2018, 6:41pm UTC](https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210/6 "2018-01-23T18:41:00Z")

</div>

> [@ankur\_singla](#):
>
> Top 5 is a variable it can be top 10, top 2 like that

My advice on shard\_size still stands for any given choice of size

---

<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:** [February 20, 2018, 6:41pm UTC](https://discuss.elastic.co/t/use-dfs-query-then-fetch-with-aggregation/115210/7 "2018-02-20T18:41:03Z")

</div>

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