This is exactly what I was looking for!
However, when I implement it verbatim into my codebase, I get this error:
Cannot infer type argument(s) for <T> aggregation(AggregationKey<T>, Function<? super AF,? extends AggregationFinalStep<T>>)
Here’s the relevant code.
AggregationKey<Map<Range<String>, Long>> countsByTitleKey = AggregationKey.of("countsByTitle");
SearchResult<String> result = searchSession.search(Entity.class)
.where(entity -> entity.match().field("title").matching(title))
.aggregation(countsByTitleKey, f -> f.terms().field("title", String.class).maxTermCount(5))
.fetch(5);