# Elasticsearch aggregation order by top hit score with partitions

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregation-order-by-top-hit-score-with-partitions/102228>\
**Category:** Elasticsearch\
**Created:** [September 29, 2017, 8:33am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-order-by-top-hit-score-with-partitions/102228 "2017-09-29T08:33:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wu\_Chun\_Wa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wu_chun_wa/32/22563_2.png) [@Wu\_Chun\_Wa](https://discuss.elastic.co/u/Wu_Chun_Wa)\
**Post date:** [September 29, 2017, 8:33am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-order-by-top-hit-score-with-partitions/102228/1 "2017-09-29T08:33:14Z")

</div>

I read (Elasticsearch aggregation order by top hit score) and follow it with partitions. The output is very strange. The last partition has the large score. Also, each partition sort the score individually.

Current result

Partition (1) [{score:10}, {score:9}, {score:8}]

Partition (2) [{score:12}, {score:10}, {score:9}]

Partition (3) [{score:20}, {score:15}, {score:10}]

Expected Resultt

Partition (1) [{score:20}, {score:15}, {score:12}]

Partition (2) [{score:10}, {score:10}, {score:10}]

Partition (3) [{score:9}, {score:9}, {score:8}]

```
{
"size": 0,
"query": {
	"bool": {
		"must": {
			"multi_match": {
				"query": "place",
				"fields": ["name", "name.stemmed"],
				"type": "best_fields"
			}
		}
	}
},
"aggs": {
	"unique_id": {
		"terms": {
			"field": "id",
			"size": 10000,
			"order": {
				"max_score": "desc"
			},
			"include": {
				"partition": 0,
				"num_partitions": 3
			}
		},
		"aggs": {
			"top_places": {
				"top_hits": {
					"size": 1
				}
			},
			"max_score": {
				"max": {
					"script": "_score"
				}
			}
		}
	}
}
}
```

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [October 6, 2017, 6:57am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-order-by-top-hit-score-with-partitions/102228/2 "2017-10-06T06:57:30Z")

</div>

The partitions work on a subset of terms, there is no guarantee that the order is preserved among the partitions.  
Each partition selects the terms that should be part of the response and sort them accordingly. If you want the global ordering you'll have to merge sort the responses from all partitions.

---

<div class="post-metadata">

**Author:** ![Wu\_Chun\_Wa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wu_chun_wa/32/22563_2.png) [@Wu\_Chun\_Wa](https://discuss.elastic.co/u/Wu_Chun_Wa)\
**Post date:** [October 18, 2017, 7:51am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-order-by-top-hit-score-with-partitions/102228/3 "2017-10-18T07:51:41Z")

</div>

@jimczi How to handle global ordering and merge sort the responses from all partitions?

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [October 20, 2017, 8:07pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-order-by-top-hit-score-with-partitions/102228/4 "2017-10-20T20:07:44Z")

</div>

You'll have to merge sort the results coming from all partitions on your side. You can build a priority queue that will keep the top N global results and populate it with the results coming from all partitions. All partitions are independent and we don't provide a way to do the merge sort on es side.

---

<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 17, 2017, 8:07pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-order-by-top-hit-score-with-partitions/102228/5 "2017-11-17T20:07:45Z")

</div>

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