Aggregate for each returned result

I read the section on aggregation scoping and did a lot of searching but can't find any examples. I want to create an aggregation within each returned result instead of aggregating over the entire returned set.

My use case:

Starting with a simplified document like this:

"document": {
    "id": "The ID",
    "title": "The title",
    "references": [
            "id": "Another ID",
            "docType": "book"
            "id": "More IDs",
            "docType": "magazine"

When documents are returned from some query, for each document I want to count the number of references by docType for each result.

I've tried putting the aggregation everywhere and can't seem to get it in the right place. Tried nesting within the query, etc. I always seem to get some variation on QueryParsingException[ No query registered for [aggregations]];

Is what I'm trying to do possible? Am I just misunderstanding? Thanks!