# Getting doc\_count in percentiles aggregation

**URL:** <https://discuss.elastic.co/t/getting-doc-count-in-percentiles-aggregation/113036>\
**Category:** Elasticsearch\
**Created:** [December 22, 2017, 4:40pm UTC](https://discuss.elastic.co/t/getting-doc-count-in-percentiles-aggregation/113036 "2017-12-22T16:40:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amyc](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@amyc](https://discuss.elastic.co/u/amyc)\
**Post date:** [December 22, 2017, 4:40pm UTC](https://discuss.elastic.co/t/getting-doc-count-in-percentiles-aggregation/113036/1 "2017-12-22T16:40:28Z")

</div>

Hi,  
I would like to get the doc\_count value in percentiles aggregation, e.g.:  
GET /my\_index/\_search  
{  
"query": {  
...  
},  
"aggregations" : {  
"my\_agg" : {  
"percentiles" : {  
"script" : "\_score",  
"percents" : [85],  
"keyed" : "false"  
}  
}  
}  
}  
would return the following response:  
...  
"aggregations": {  
"my\_agg": {  
"values": [  
{  
"key": 85,  
"value": 2.674367666244506  
}  
]  
}  
}

that shows the score of 85 percentiles point = 2.674367666244506. If it returns the "doc\_count" together with the value, I could get the number of my search hits which is above the 85 percentiles.  
I tried to do some pipe aggreation or sub-aggregation to get the number and all ended in the error.  
Any idea if this is achievable?  
Thanks!

Yifeng

---

<div class="post-metadata">

**Author:** ![luiz.santos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luiz.santos/32/24664_2.png) [@luiz.santos](https://discuss.elastic.co/u/luiz.santos)\
**Post date:** [December 28, 2017, 12:00pm UTC](https://discuss.elastic.co/t/getting-doc-count-in-percentiles-aggregation/113036/2 "2017-12-28T12:00:06Z")

</div>

Hi @amyc,

The document count in percentile aggregation is not available. It's not clear why is it necessary to know the number of documents as 80th percentile, for example, is the value which is greater than 80% of the observed values.

If you really need a number you could guess it doing some math using the total hits of the search result, but please be aware that [percentile are approximate](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html#search-aggregations-metrics-percentile-aggregation-approximation).

Cheers,  
LG

---

<div class="post-metadata">

**Author:** ![amyc](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@amyc](https://discuss.elastic.co/u/amyc)\
**Post date:** [January 2, 2018, 2:27pm UTC](https://discuss.elastic.co/t/getting-doc-count-in-percentiles-aggregation/113036/3 "2018-01-02T14:27:10Z")

</div>

@luiz.santos:  
Thanks for you reply.  
My use case is to use percentiles aggregation for filtering out documents based on percentiles of the max score (not the \_min\_score), e.g. filtering out documents having score below 80%. (See comment on issue #719). If I have the document count together with the 80% percentiles score value, I am able to achieve this by returning the correct total hits (using this document count), and looping the returned hits, removing document with the score below the percentile value. This allows me to do it with one pass. Without it, I have to run a second pass.

Yifeng

---

<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 2, 2018, 2:58pm UTC](https://discuss.elastic.co/t/getting-doc-count-in-percentiles-aggregation/113036/4 "2018-01-02T14:58:13Z")

</div>

> [@amyc](#):
>
> Without it, I have to run a second pass.

Search filters such as `min_score` are applied locally to a shard in phase 1 of a search. The global range of scores produced using percentiles is not known until phase 1 results are merged from all shards. Therefore you will need 2 passes to do this sort of logic.

---

<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:** [January 30, 2018, 2:58pm UTC](https://discuss.elastic.co/t/getting-doc-count-in-percentiles-aggregation/113036/5 "2018-01-30T14:58:20Z")

</div>

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