How to return number of occurrence of specific term in a field in the search result

Supposing I perform a search for a random term "cook", then I would like to know how many times that each of the words: "restaurant", "school", "hospital", and other 7 terms occurs in the field "category" in the search result.

I tried the count API, however, it always returns the number of occurrence of the specific term in all documents, rather than that in the search result. (Maybe it is because I perform the count after the search, not sure how to combine search and count together) Also, this way takes very long time to return the result.

I also looked at the "term aggregation" functionality, however, I didn't figure out how to aggregate the specific term in the search result.

Anyone can advise? Thank you.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.