Get correct atlas search facet results

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.

1 Like

Hi @Mathias_Mahlknecht , welcome to our forums! I believe what you’re experiencing is the current, intended behavior. Facets return counts over the search results returned from the given operator. Since the operator specifies that mainColor and seats must match specific values, the facets only return buckets for those specified values.

1 Like

Hi @Elle_Shwer, that’s what I thought. But is there currently a way how I can achieve what I want, without querying each facet individually? And if not, is there any feature in the pipeline, that could help me with this in a future release?

Not that I am aware of, will pose this question to the team though. Feel free to also channel feature request here: https://feedback.mongodb.com/
We heavily monitor this site for new ideas.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.