# Access doc\_count in scripted\_metric section

**URL:** <https://discuss.elastic.co/t/access-doc-count-in-scripted-metric-section/56371>\
**Category:** Elasticsearch\
**Created:** [July 26, 2016, 9:08am UTC](https://discuss.elastic.co/t/access-doc-count-in-scripted-metric-section/56371 "2016-07-26T09:08:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashi-GS](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shashi-GS](https://discuss.elastic.co/u/Shashi-GS)\
**Post date:** [July 26, 2016, 9:08am UTC](https://discuss.elastic.co/t/access-doc-count-in-scripted-metric-section/56371/1 "2016-07-26T09:08:26Z")

</div>

Could you please help me on accessing the doc\_count or the value\_count aggregation result in the scripted metric section?

here is the example I am trying to access.

```
{
    "size": 0,
    "_source": false,
     "aggs": {
        "group_by_sales_ACCNO": {
            "terms": {
                "field": "sales_ACCNO",
                "size": 0,
                "order": [
                    {
                        "_term": "asc"
                    }
                ]
            },
            "aggs": {
                "repeatCountAggs": {
                    "scripted_metric": {
                        "map_script": "_agg.value = doc['count'].value;",
                       "reduce_script": "value = 0; for (a in _aggs) { if(a.value > 5) value = a.value;}; return value;"
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![Shashi-GS](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shashi-GS](https://discuss.elastic.co/u/Shashi-GS)\
**Post date:** [July 26, 2016, 9:22am UTC](https://discuss.elastic.co/t/access-doc-count-in-scripted-metric-section/56371/2 "2016-07-26T09:22:44Z")

</div>

In Addition to the above example, I am trying to achieve this.

```
{
    "size": 0,
    "_source": false,
    "aggs": {
        "group_by_customersales_ACCNO": {
            "terms": {
                "field": "customersales_ACCNO",
                "size": 0,
                "order": [
                    {
                        "_term": "asc"
                    }
                ]
            },
            "aggs": {
                "repeatCount": {
                    "value_count": {
                        "field": "customersales_ACCNO"
                    }
                },
                "repeatCountAggs": {
                    "scripted_metric": {
                        "map_script": "_agg.value = doc['count'].value;",
"reduce_script": "value = 0; for (a in _aggs) { if(a.value > 5) value = a.value;}; return value;"
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 26, 2016, 9:24am UTC](https://discuss.elastic.co/t/access-doc-count-in-scripted-metric-section/56371/3 "2016-07-26T09:24:31Z")

</div>

You can't access the result of the terms aggregation from the scripted metric aggregation. It looks like from the scripts you have written that you are trying to filter the buckets in the terms aggregation to only buckets which have a doc\_count greater than 5. is this correct? If so, you can use the `min_doc_count` option int he terms aggregation itself to filter the buckets in this way: [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-bucket-terms-aggregation.html#\_minimum\_document\_count\_3](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/search-aggregations-bucket-terms-aggregation.html#_minimum_document_count_3)

Also you should not use `"size": 0` in the terms aggregation for the reasons described in this issue: [https://github.com/elastic/elasticsearch/issues/18838](https://github.com/elastic/elasticsearch/issues/18838). Note that from 5.0 you will not be able to specify `size: 0` in the terms aggregation

---

<div class="post-metadata">

**Author:** ![Shashi-GS](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shashi-GS](https://discuss.elastic.co/u/Shashi-GS)\
**Post date:** [July 26, 2016, 9:31am UTC](https://discuss.elastic.co/t/access-doc-count-in-scripted-metric-section/56371/4 "2016-07-26T09:31:49Z")

</div>

Yes your understanding is correct. I can't achieve my requirement using the min\_doc\_count. Because, I want to check the resulted bucket values will come between 5 to 10, 10 to 20 etc. Hence I am trying to access through scripted matrics. is there any other alternatives to achieve this ?

regarding size suggestion. Thank you will take care going forward.

---

<div class="post-metadata">

**Author:** ![Shashi-GS](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@Shashi-GS](https://discuss.elastic.co/u/Shashi-GS)\
**Post date:** [July 27, 2016, 9:23am UTC](https://discuss.elastic.co/t/access-doc-count-in-scripted-metric-section/56371/5 "2016-07-27T09:23:46Z")

</div>

I have solved this problem. This problem can be solved by using the "bucket\_selector".

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html)

---

<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:32pm UTC](https://discuss.elastic.co/t/access-doc-count-in-scripted-metric-section/56371/6 "2017-07-05T22:32:38Z")

</div>


