# Percentile Rank query giving unexpected results

**URL:** <https://discuss.elastic.co/t/percentile-rank-query-giving-unexpected-results/265260>\
**Category:** Elasticsearch\
**Created:** [February 23, 2021, 10:08pm UTC](https://discuss.elastic.co/t/percentile-rank-query-giving-unexpected-results/265260 "2021-02-23T22:08:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Tan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_tan/32/83546_2.png) [@Jonathan\_Tan](https://discuss.elastic.co/u/Jonathan_Tan)\
**Post date:** [February 23, 2021, 10:08pm UTC](https://discuss.elastic.co/t/percentile-rank-query-giving-unexpected-results/265260/1 "2021-02-23T22:08:02Z")

</div>

Hi,

I'm getting unexpected results for the `percentile_rank` aggregation, and was looking for some help/clarification.

I've got 9 documents, all of which have a `position` field.  
The `position` values for the 9 documents are:

`[1, 1, 1, 1, 2, 2, 3, 4, 8]`

Now when I do the following `percentile_rank` aggregate

```auto
{
  "size": 0,
  "aggs": {
    "percentile_test": {
      "percentile_ranks": {
        "field": "position",
        "values": [
          1,
          3
        ]
      }
    }
  }
}

```

The documentation at [Percentile ranks aggregation | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-rank-aggregation.html) says that

> Percentile rank shows the percentage of observed values which are below certain value

implying that for my value of `1`, I should actually get a value of `0`, since I have no documents with a position of less than 1.

Instead, I get

```auto
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 9,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "percentile_test": {
      "values": {
        "1.0": 33.33333333333333,
        "3.0": 72.22222222222221
      }
    }
  }
}

```

which states that my percentile rank for documents having a position of less than 1, is `33.33%`. i.e., a third of my documents.  
Similarly, with 6 out of 9 documents having a value of less than 3, I'd expect the percentile rank for `3` to be 66.66%. Instead, It's coming back as `72.22%`.

I'm aware of the caveat in [Percentiles aggregation | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html#search-aggregations-metrics-percentile-aggregation-approximation) talking about how percentiles are approximate, however this seems a bit weird. I mean, in this case, I'm asking for the percent of values under the smallest value:

1. there's nothing that is smaller, so why is that ever coming up with a value \> 0?
2. the same link says that for small document sets, it is highly accurate, potentially even being 100% accurate. And I think 9 documents is a very very small data set...

Can anybody provide some help/answers/opinions/perspective please?

I also note that similar questions about percentile rank have been asked in the past, without any answers, so clearly this is something that others have found puzzling ☹

> [@Percentile ranking question](https://discuss.elastic.co/t/percentile-ranking-question/62154):
>
> Hi, I have the following list of data: [2.625, 3.0, 3.5, 3.5, 3.5, 3.875, 4.375, 4.75, 4.875, 4.875, 5.0, 5.125, 5.125, 5.375, 5.375, 5.5, 5.625, 5.625] when I use the following query to get the percentile ranks for the given values { "aggregations" : { "agg" : { "percentile\_ranks" : { "field" : "score", "values" : [2.55, 2.625, 3.0, 3.5, 3.875, 4.375, 4.75, 4.875, 5.0, 5.125, 5.375, 5.5, 5.625] } } } } I get the following: { "took" : 5, "timed\_out" : false, "\_shards" : { …

> [@Question about percentile rank query](https://discuss.elastic.co/t/question-about-percentile-rank-query/198323):
>
> I had this query, with which I would expect to have value '0.000005' below the value returned from the record "system" : { "pct" : 0.016} The question is: why it still shows the percentile as 100.0? GET /metricbeat-7.3.1/\_search { "aggs": { "test\_percentile": { "percentile\_ranks": { "field": "system.cpu.system.pct", "values": [1, 0.5, 0.000005] } } }, "query": { "bool": { …

> [@Percentile ranks return wrong value](https://discuss.elastic.co/t/percentile-ranks-return-wrong-value/223755):
>
> I read about "Percentiles Aggregation" and know already about "relative error". In my case, I have to calculate correctly. I have 45 values greater than 0.4 and 1 values smaller than 0.4 So correct return percentile have to be (1\45\*100) =2.2222, but I get "0.4": 1.6760828741290288. How is it possible to calculate the correct values? My Current request lool like I read [here](https://github.com/elastic/elasticsearch/issues/26156) that max aggregation can solve the issue but I can not apply it. { "size": 0, "query": { "bool": {…

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Jonathan\_Tan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonathan_tan/32/83546_2.png) [@Jonathan\_Tan](https://discuss.elastic.co/u/Jonathan_Tan)\
**Post date:** [March 18, 2021, 12:17am UTC](https://discuss.elastic.co/t/percentile-rank-query-giving-unexpected-results/265260/2 "2021-03-18T00:17:56Z")

</div>

Bump?  
Nobody else able to respond to this with some experience?

---

<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:** [April 15, 2021, 12:18am UTC](https://discuss.elastic.co/t/percentile-rank-query-giving-unexpected-results/265260/3 "2021-04-15T00:18:25Z")

</div>

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