Chapter 31. Sorting Multivalue Buckets
date_histogram—dynamically produce many buckets. How does Elasticsearch decide the order that
these buckets are presented to the user?
By default, buckets are ordered by
doc_count in descending order. This is a
good default because often we want to find the documents that maximize some
criteria: price, population, frequency. But sometimes you’ll want to modify this sort order, and there are a few ways to
do it, depending on the bucket.
These sort modes are intrinsic to the bucket: they operate on data that bucket
generates, such as
doc_count. They share the same syntax but differ slightly
depending on the bucket being used.
Let’s perform a
terms aggregation but sort by
doc_count, in ascending order:
We introduce an
order object into the aggregation, which allows us to sort on
one of several values:
Sort by document count. Works with
Sort by the string value of a term alphabetically. Works only with