Elasticsearch aggregation order by top hit score with partitions

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"
				}
			}
		}
	}
}
}

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.

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

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.

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