1

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.
fmsf
  • 36,317
  • 49
  • 147
  • 195
  • Afaik it's indeed not possible, we'll drop an answer if it becomes available as a feature – fmsf Apr 04 '22 at 11:06

1 Answers1

2

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).

Logan Rhyne
  • 581
  • 2
  • 5