Basically, what I'm trying to do here is get the second-level-down categories from a hierarchically stored string. The problem is that the level of hierarchy vary and one product category could have six levels and another only four, otherwise I would have just implemented predefined levels.
I have some products with categories like so:
[
{
title: 'product one',
categories: [
'clothing/mens/shoes/boots/steel-toe'
]
},
{
title: 'product two',
categories: [
'clothing/womens/tops/sweaters/open-neck'
]
},
{
title: 'product three',
categories: [
'clothing/kids/shoes/sneakers/light-up'
]
},
{
title: 'product etc.',
categories: [
'clothing/baby/bibs/super-hero'
]
},
... more products
]
I'm trying to get aggregation buckets like so:
buckets: [
{
key: 'clothing/mens',
...
},
{
key: 'clothing/womens',
...
},
{
key: 'clothing/kids',
...
},
{
key: 'clothing/baby',
...
},
]
I've tried looking at filter prefixes, includes and excludes on terms, but I can't find anything that works. Please someone point me in the right direction.
Your
category
field should be analyzed with a custom analyzer. Maybe you have some other plans with thecategory
, so I'll just add a subfield used only for aggregations:Test data:
The query itself:
The results: