# Sorting percentiles aggregation with NaN values

**URL:** <https://discuss.elastic.co/t/sorting-percentiles-aggregation-with-nan-values/154462>\
**Category:** Elasticsearch\
**Created:** [October 29, 2018, 3:35pm UTC](https://discuss.elastic.co/t/sorting-percentiles-aggregation-with-nan-values/154462 "2018-10-29T15:35:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![itaydvir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itaydvir/32/10143_2.png) [@itaydvir](https://discuss.elastic.co/u/itaydvir)\
**Post date:** [October 29, 2018, 3:35pm UTC](https://discuss.elastic.co/t/sorting-percentiles-aggregation-with-nan-values/154462/1 "2018-10-29T15:35:21Z")

</div>

I'm using ElasticSearch 2.3.3 and I have the following aggregation:

```
"aggregations": {
		"mainBreakdown": {
			"terms": {
				"field": "location_i",
				"size": 10,
				"order": [
					{
						"comments>medianTime.50": "asc"
					}
				]
			},
			"aggregations": {
				"comments": {
					"filter": {
						"term": {
							"type_i": 120
						}
					},
					"aggregations": {
						"medianTime": {
							"percentiles": {
								"field": "time_l",
								"percents": [
									50.0
								]
							}
						}
					}
				}
			}
		}
	}

```

for better understanding I've added to field names a postfix which tells the field mapping:

- \_i = integer
- \_l = long (timestamp)

And aggregation response is:

```
"aggregations": {
    "mainBreakdown": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 100,
          "doc_count": 2,
          "comments": {
            "doc_count": 1,
            "medianTime": {
              "values": {
                "50.0": 20113
              }
            }
          }
        },
        {
          "key": 121,
          "doc_count": 14,
          "comments": {
            "doc_count": 0,
            "medianTime": {
              "values": {
                "50.0": "NaN"
              }
            }
          }
        }
      ]
    }
}

```

My problem is that the `medianTime` aggregation, sometimes has value of `NaN` because the parent aggregation `"comments"` has 0 matched documents, and then the result with the `NaN` will always be last on both "asc" and "desc" order.  
  
I've tried adding `"missing": 0` inside `percentiles` aggregation but it still returns a `NaN`.

Can you please help me sorting my buckets by medianTime, so when it's "asc" ordering the `NaN` values will be first and when its "desc" they will be last?

---

<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:** [November 26, 2018, 3:35pm UTC](https://discuss.elastic.co/t/sorting-percentiles-aggregation-with-nan-values/154462/2 "2018-11-26T15:35:23Z")

</div>

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