Access doc_count in scripted_metric section

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;"
                    }
                }
            }
        }
    }
}

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;"
                    }
                }
            }
        }
    }
}

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

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. Note that from 5.0 you will not be able to specify size: 0 in the terms aggregation

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.

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