We have a product catalog and would like to build a facet search functionality for it, similar to the one in most web shops.
Take the following simplified search index:
{
"mappings": {
"dynamic": false,
"fields": {
"mainColor": [
{
"type": "stringFacet"
},
{
"store": false,
"type": "string"
}
],
"seats": [
{
"type": "stringFacet"
},
{
"store": false,
"type": "string"
}
]
}
}
}
Now I would like to build a query that returns the result set, as well as the total count and the facet buckets.
This is my best attempt so far:
[
{
"$search": {
"facet": {
"operator": {
"compound": {
"must": [
{
"text": {
"path": "mainColor",
"query": "Beige"
}
},
{
"text": {
"path": "seats",
"query": "2"
}
}
]
}
},
"facets": {
"mainColor": {
"type": "string",
"path": "mainColor"
},
"seats": {
"type": "string",
"path": "seats"
}
}
},
"count": {
"type": "total"
}
}
},
{
"$facet": {
"data": [
{
"$project": {
"mainColor": 1,
"seats": 1
}
}
],
"meta": [
{
"$replaceWith": "$$SEARCH_META"
},
{
"$limit": 1
}
]
}
},
{
"$set": {
"meta": {
"$arrayElemAt": [
"$meta",
0
]
}
}
}
]
In the result now the buckets contain only one entry.
But what I need is that each facet is not effected by its own filter field, but only by the other filter fields.
Otherwise, the user has never the possibility to switch the filter to another color, for the same seats.
Is it possible to achieve this?
It would be fine if I have to split it in one query for the data and one query for the facets.
But the only way I see now is to make one query for each facet, which is not feasible, especially if there are 10+ facets.