palantir-foundryfoundry-functions

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


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?


Solution

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