Hello,
(data to play, at the end)
I've been reading about faceted navigation and filtering and how it can be applied to ecommerce apps.
So far I've managed to get the bellow example working.
Facet Group 1
- Facet Option 1 (counts)
- Facet Option 2 (counts)
- ...
- Facet Option Nth (counts)
Facet Group 2
- Facet Option 1 (counts)
- Facet Option 2 (counts)
- ...
- Facet Option Nth (counts)
Facet Group 3
- Facet Option 1 (counts)
- Facet Option 2 (counts)
- ...
- Facet Option Nth (counts)
My problem is that I always want all options to show and have only the counts change depending of what is selected, while also not changing the counts within a group after selecting siblings in that group. I'll throw some visual examples bellow.
Example Initial state without selections
RAM
- 4GB (400)
- 6GB (200)
- 8GB (100)
- >8GB (40)
Storage
- 64GB (400)
- 128GB (300)
- 256GB (30)
- >256GB (10)
Example when selecting RAM
RAM
- 4GB (400)
- 6GB (200)
- 8GB (100) (selected, but notice the rest of the sibling option counts remain unchanged)
- >8GB (40)
Notice now, how other groups should recalculate their counts when there are facet optons selected in sibling groups
Storage
- 64GB (0) (from 400 initially)
- 128GB (60) (from 300 initially)
- 256GB (30)
- >256GB (10)
How can this be achieved?
My idea is that, the selected option should not be filtered within the sub aggregation group it belongs, but applied to the rest. Does this make sense? in pseudo: add filter in agg only if it is from other group
But I haven't been able to get desired results.
Here is a sample index and some data to play with
PUT products
{
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"manufacturer": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"attributes": {
"type": "nested",
"properties": {
"attribute_name": {"type": "keyword"},
"attribute_value": {"type": "keyword"}
}
}
}
}
}
POST _bulk
{"index": {"_index": "products", "_id": 1}}
{"title":"Huawei P Smart Z (64GB) Midnight Black","manufacturer":"Huawei","attributes":[{"attribute_name":"RAM","attribute_value":"4GB"},{"attribute_name":"Storage","attribute_value":"64GB"}]}
{"index": {"_index": "products", "_id": 2}}
{"title":"Apple iPhone 12 Pro (128GB) Gold","manufacturer":"Apple","attributes":[{"attribute_name":"RAM","attribute_value":"6GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 3}}
{"title":"Apple iPhone 11 (64GB) Yellow","manufacturer":"Apple","attributes":[{"attribute_name":"RAM","attribute_value":"4GB"},{"attribute_name":"Storage","attribute_value":"64GB"}]}
{"index": {"_index": "products", "_id": 4}}
{"title":"Samsung Galaxy Note 20 Ultra (256GB) Mystic Bronze","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"12GB"},{"attribute_name":"Storage","attribute_value":"256GB"}]}
{"index": {"_index": "products", "_id": 5}}
{"title":"Samsung Galaxy S20 (128GB) Cosmic Gray","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"8GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 6}}
{"title":"Samsung Galaxy S21+ 5G (128GB) Phantom Violet","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"8GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 7}}
{"title":"Apple iPhone 12 Mini (128GB) Black","manufacturer":"Apple","attributes":[{"attribute_name":"RAM","attribute_value":"4GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 8}}
{"title":"Huawei Nova 5T (6GB/128GB) Black","manufacturer":"Huawei","attributes":[{"attribute_name":"RAM","attribute_value":"6GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 9}}
{"title":"Apple iPhone XR (64GB) White","manufacturer":"Apple","attributes":[{"attribute_name":"RAM","attribute_value":"3GB"},{"attribute_name":"Storage","attribute_value":"64GB"}]}
{"index": {"_index": "products", "_id": 10}}
{"title":"Samsung Galaxy A21s (128GB) Black","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"4GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 11}}
{"title":"Samsung Galaxy S20 5G (128GB) Cloud White","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"12GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 12}}
{"title":"Samsung Galaxy A21s (32GB) Red","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"3GB"},{"attribute_name":"Storage","attribute_value":"32GB"}]}
{"index": {"_index": "products", "_id": 13}}
{"title":"Apple iPhone 12 Mini (256GB) Black","manufacturer":"Apple","attributes":[{"attribute_name":"RAM","attribute_value":"4GB"},{"attribute_name":"Storage","attribute_value":"256GB"}]}
{"index": {"_index": "products", "_id": 14}}
{"title":"Apple iPhone 11 Pro (256GB) Gold","manufacturer":"Apple","attributes":[{"attribute_name":"RAM","attribute_value":"4GB"},{"attribute_name":"Storage","attribute_value":"256GB"}]}
{"index": {"_index": "products", "_id": 15}}
{"title":"Apple iPhone 8 Plus (64GB) Silver","manufacturer":"Apple","attributes":[{"attribute_name":"RAM","attribute_value":"3GB"},{"attribute_name":"Storage","attribute_value":"64GB"}]}
{"index": {"_index": "products", "_id": 16}}
{"title":"Samsung Galaxy Note 8 (64GB) Midnight Black","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"6GB"},{"attribute_name":"Storage","attribute_value":"64GB"}]}
{"index": {"_index": "products", "_id": 17}}
{"title":"Apple iPhone XS Max (256GB) Space Gray","manufacturer":"Apple","attributes":[{"attribute_name":"RAM","attribute_value":"4GB"},{"attribute_name":"Storage","attribute_value":"256GB"}]}
{"index": {"_index": "products", "_id": 18}}
{"title":"Samsung Galaxy S20 FE (128GB) Cloud Lavender","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"6GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 19}}
{"title":"Samsung Galaxy A31 (128GB) Prism Crush Blue","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"4GB"},{"attribute_name":"Storage","attribute_value":"128GB"}]}
{"index": {"_index": "products", "_id": 20}}
{"title":"Samsung Galaxy A21s (32GB) White","manufacturer":"Samsung","attributes":[{"attribute_name":"RAM","attribute_value":"3GB"},{"attribute_name":"Storage","attribute_value":"32GB"}]}
GET products/_search?size=0
{
"aggs": {
"facet": {
"nested": {
"path": "attributes"
},
"aggs": {
"facet_group": {
"terms": {
"field": "attributes.attribute_name"
},
"aggs": {
"values": {
"terms": {
"field": "attributes.attribute_value"
}
}
}
}
}
}
}
}