Pipeline aggregations: Sorting/Filtering after bucket_script execution

Hey,

I am running an aggregation that groups by a terms aggregation and then runs the sum on a field for each bucket. Works of course.

Now on top of that I have a bucket script aggregation running, that is doing a calculation with the doc_count of each bucket and the result of the sum agg.

After this calculation I would like to filter (a minimum threshold of that calculated value) and sort based on that calculated bucket script output.

I do not see how I can sort on pipeline bucket outputs, as the documentation explicitely states, that sorting on the terms bucket agg is only for multi bucket aggs.

Maybe I misread the docs and this is somehow possible (or any other alternative that I am not seeing)? This is on Elasticsearch 8.14.

Thanks in advance!

--Alex

Hey,

quick update, I managed to solve this with ESQL, even though I now have to run a second query before due to not being able to run full text search within ESQL, but keyword only.

It's roughly like this, using the SUM grouped by a field and dividing it.

FROM idx |
WHERE (term LIKE "a" OR term LIKE "b") |
STATS final_score=SUM(score)/COUNT(field) BY field |
SORT final_score DESC

--Alex

P.S. Full text search matching in ESQL wanted :slight_smile:

1 Like