aggregation on path hierarchy tokens
I found a couple of great articles on this issue (here and here), and also experimented a bit. While the two articles reference an older version, a few tweaks got things working with ES 6.
Here's what worked for me, I haven't tested with multiple categories per item (e.g. your array example), but it would likely still work:
{ "settings": { "analysis": { "analyzer": { "path-analyzer": { "tokenizer": "path-tokenizer" } }, "tokenizer": { "path-tokenizer": { "type": "path_hierarchy", "delimiter": "|" } } } }, "mappings": { "item": { "dynamic": "strict", "properties": { "category_path": { "type": "text", "analyzer": "path-analyzer", "search_analyzer": "keyword", "fielddata": true } } } }}
Your aggregations request would look something like this:
{ "aggs": { "category": { "terms": { "field": "category_path" } } }, "size": 0}
And your results:
"buckets": [ { "key": "Men", "doc_count": 11 }, { "key": "Men|Sale", "doc_count": 4 }, { "key": "Men|Tops", "doc_count": 3 }, { "key": "Men|Tops|Shirts", "doc_count": 2 } ]