Hi,
I have been looking for a solution for the following case and tried searching for multiple terms on the Google and here on the forum. I'm not sure what the best solution is for this case so maybe someone here can point me to the right thread. We are working on an e-commerce store and are implementing dynamic facets in the store.
Consider the following mapping.
{
"title": {
"type": "keyword"
},
"specifications": {
"type": "nested",
"properties": {
"header": {
"type": "keyword"
},
"value": {
"type": "keyword"
}
}
}
}
Then we use this aggregation to collect the facet headers and values.
{
"aggs": {
"specification_headers": {
"nested": {
"path": "specifications"
},
"aggs": {
"headers": {
"terms": {
"field": "specifications.header",
"min_doc_count": 5,
"size": 7
},
"aggs": {
"values": {
"terms": {
"field": "specifications.value",
"min_doc_count": 1,
"size": 100
}
}
}
}
}
}
}
}
This will produce the following results
- Color
- [10] Black
- [5] Red
- Height
- [8] 100
- [7] 50
Then when a customer selects one of the values we apply a post_filter
to filter out the selection.
Now the counters of the other filters do not change with the selected option.
{
"post_filter": {
"bool": {
"filter": [
{
"nested": {
"path": "specifications",
"query": {
"bool": {
"filter": [
{
"terms": {
"specifications.header": [
"Color"
]
}
},
{
"terms": {
"specifications.value": [
"Black"
]
}
}
]
}
}
}
}
]
}
},
"aggs": {
"specification_headers": {
"nested": {
"path": "specifications"
},
"aggs": {
"headers": {
"terms": {
"field": "specifications.header",
"min_doc_count": 5,
"size": 7
},
"aggs": {
"values": {
"terms": {
"field": "specifications.value",
"min_doc_count": 1,
"size": 100
}
}
}
}
}
}
}
}
So the other things that I tried
- Not use
post_filter
instead usebool
query
they result is when I choose the black option the red option won't appear. - Using a
global
aggregation and then apply afilter
aggregation of the results which excludes the selected term on a specific aggregation that works for static filters but for dynamic filters like this I don't know the headers beforehand so I don't know what to exclude - Doing the same as the
global
aggregation withpost_filter
andfilter
aggregation this gave the same issue as the second thing i tried
I came to the conclusion to do 2 requests to ElasticSearch in the first ask for the headers and then add different aggregations for al the found headers in a second request.
Maybe there is a solution to solve this in a single call to ElasticSearch?
For anyone responding thanks in advance!