# Sorting issue

**URL:** <https://discuss.elastic.co/t/sorting-issue/155368>\
**Category:** Elasticsearch\
**Created:** [November 5, 2018, 9:00am UTC](https://discuss.elastic.co/t/sorting-issue/155368 "2018-11-05T09:00:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vigilrapheal](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@vigilrapheal](https://discuss.elastic.co/u/vigilrapheal)\
**Post date:** [November 5, 2018, 9:00am UTC](https://discuss.elastic.co/t/sorting-issue/155368/1 "2018-11-05T09:00:49Z")

</div>

I have a requirement where in I would need to sort the parent bucket ('d1' here) based on the values calculated in an innermost bucket ('percentage' key within udate[0]). Please find my Elasticsearch query below. Thank you in advance.!

{  
"query": {  
"bool": {  
"must": [{  
"range": {  
"date": {  
"gte": "27/10/2018 00:00:00"

```
				}
			}
		}]
	}
},
"aggs": {
	"d1": {
		"terms": {
			"field": "d.keyword",
			"size": 100
		},
		"aggs": {
			"udate": {
				"terms": {
					"field": "date",
					"size": 10
				},
				"aggs": {
					"value_count": {
						"value_count": {
							"field": "status.keyword"
						}
					},
					"filterStatus": {
						"filter": {
							"prefix": {
								"status": "4"
							}
						},
						"aggs": {
							"error_count": {
								"value_count": {
									"field": "status.keyword"
								}
							}
						}
					},
					"percentage": {
						"bucket_script": {
							"buckets_path": {
								"errorCount": "filterStatus>error_count",
								"totalCount": "value_count"
							},
							"script": "params.errorCount/params.totalCount * 100"
						}
					}
				}
			}
		}
	}
},
"size": 0

```

}

---

<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:** [December 3, 2018, 9:00am UTC](https://discuss.elastic.co/t/sorting-issue/155368/2 "2018-12-03T09:00:50Z")

</div>

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