Trouble using new .order() on datehistogram after upgrading

I recently upgraded to Spring 3 which means there were small adjustments needed for some of my queries/aggregations written in elasticsearch java. I cannot figure out how to get the .order(SortOrder.Asc) to work again in the current version. This is what my Aggregation used to look like:

new Aggregation.Builder().dateHistogram(
         d -> d.field("timestamp")
            .calendarInterval(...)
            .format(...)
            .order(HistogramOrder.of(h -> h.key(SortOrder.Asc)))
            .minDocCount(0)
            .extendedBounds(
               e -> e.max(...)
                  .min(...)
            )
      );

now the .order(...) seems to take a NamedValue, so I tried

.order(NamedValue.of("ascending", SortOrder.Asc))

this however, only gives me an error when trying to execute the query:
"type":"aggregation_execution_exception","reason":"Invalid aggregation order path [ascending]. The provided aggregation [ascending] either does not exist, or is a pipeline aggregation and cannot be used to sort the buckets."

I can't say I understand the error completely and how to fix it! Is there a different way of ordering these buckets now? Any help appreciated!

Hello!

order works the same in any aggregation, and in Java it's a NamedValue because you have to choose:

  1. what you need to order by (keyword? aggregation?)
  2. how you want to order it. (asc? desc?)

In the previous version of the client we'd hardcoded a few choices, like key in the example you showed which is equivalent to:

...
"order": { "_key": "asc" }
...

While in the new client we the parameter as a string, because you can also order by sub aggregation names - so in the new client all you need to do is replace ascending with _key, or whatever parameter you need to order the buckets by!

You can find more information on how to use order in the documentation (yes it's term aggregation, because as I said it works the same for every aggregation).