How to limit the no. of top values when aggregating in a Function?

371 Views Asked by At

I am writing a function which just groupby a string property. For this purpose, I need to select topValues() as the aggregation type. However, I get all possible buckets, while I just need the first 10 ones. Is it possible to set the max. number of buckets to retrieve?

  • .topValues(10) is not working. No configuration possibility available.
  • I wrote a function to return 1000 values and then, keep just the first 10. This is however really unefficient.
1

There are 1 best solutions below

0
On BEST ANSWER

Currently the topValues() aggregation does not support a pre-defined bucket count. Your approach of calculating the top 1000 and then post-filtering is the best option.

For others looking, here's a code snippet for reference:

const x = Object.search().myObjectType().groupBy(obj => obj.myStringProperty.topValues()).sum();
return {buckets: x.buckets.sort((x, y) => x.value - y.value).slice(0, 5)};

There is a feature request tracking an enhancement to add this configuration, though I would expect this to be more of a convenience rather than a performance improvement, since to calculate the top N buckets you roughly have to know the sizes of the other buckets (with some obvious room for optimization).