Is it better to use a specific index for facets?

Hello,

In the WhatsCooking project, there are a default index to performe $search stage and another index (facetIndex) to performe $searchMeta stage. Is this a good practice and why? Can I have a single index for perform $search and $searchMeta stages?

Hi @Matheus_Souza and welcome to the MongoDB community forum!!

The $searchMeta stage of the aggregation pipeline, having the same syntax as $search, returns the Metadata information related to the search query.

To answer your question:

The same index can be used for both the operators, keeping in mind the output for both the stages would be different.

Let us know if you have any further questions.

Best Regards
Aasawari

@Aasawari but we would need to have two fields in the mapping, one for “string” and one for “facetString”? if we wanted to have a filter for jobField for example?

"jobField": [
            {
              "type": "stringFacet"
            },
            {
              "type": "string"
            }

It is not mandatory to have stringFacet. You must have facets if you want to use this feature.

Right, but if I want to use facets I have to have both defined, one as String and one as StringFacet

            {
              "type": "stringFacet"
            },
            {
              "type": "string"
            }
```

Hi @Matheus_Souza !

Just seeing this and hope your project is coming along well. You could indeed use the facetIndex for both $search and $searchMeta, and it would probably save space, as well. I used 2 indexes for the sake of teaching explicitly the concept of indexes.

You find our explicit recommendations here if you are querying for facets: https://www.mongodb.com/docs/atlas/atlas-search/facet/#search_meta-aggregation-variable

Happy coding!
Karen

3 Likes

Yes, that’s right. You must to explicit that you want to group and count values for that field

Hi @Karen_Huaulme!
Thanks for reply and tutorial. Our project is stable now and we are moving forward with a single index.
Here is our project in action! Bares e Restaurantes em Sao Paulo, SP - Apontador

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