Ah, there's queries for each of these scenarios.
I want to display my docs with the most attachments
For this, if you have attachments stored as a number, you can use the search API and sort by that field in a descending order Search API | Elasticsearch Guide [8.11] | Elastic
number of items grouped by category
For this, you can add an aggregation to your search query Terms aggregation | Elasticsearch Guide [8.11] | Elastic.
The link below will show you all the documentation that there is for Elasticsearch including different types of queries you can do, different types of stats you can do and such Elasticsearch Guide [8.11] | Elastic.