$search in $facet aggregation

Hi !

I want to know why we cannot use $search in $facet ?

Can we use it in near future ?

Thanks

@Asya_Kamsky maybe you have an insight here?

It is largely a performance concern. We need to implement faceting capabilities for the full text search use case and performance expectation. The $facet implementation targets a different use case and has a different implementation as a result.

Hi Jonathan! $search is always the 1st stage in an aggregation pipeline, and it calls an entirely different process (the mongot) before sending documents back to the mongod to finish the aggregation pipeline. One of the reasons for this is because search uses a different type of index -inverted indexes - before sending to mongod which uses b tree indexes. You can still do a “faceted search” inside the $search stage by combining other operators with a compound operator. There are some examples here:
https://docs.atlas.mongodb.com/reference/atlas-search/compound/

Hope this helps. If not, please let me know your use case for $facet and I can provide further info. Thanks!

2 Likes

I want to make facets like Elastisearch. But for now i think is not possible or very difficult to make many counts in $search or $compound

Query:

“facets”:{
“ratemin”:{
“type”:“range”,
“ranges”:[
{
“from”:0,
“to”:10000,
“name”:“0 - 10000”
},
{
“from”:10001,
“to”:100000,
“name”:“10001 - 100000”
},
{
“from”:100001,
“to”:500000,
“name”:“100001 - 500000”
},
{
“from”:500001,
“to”:1000000,
“name”:“500001 - 1000000”
},
{
“from”:1000001,
“to”:5000000,
“name”:“1000001 - 5000000”
},
{
“from”:5000001,
“to”:10000000,
“name”:“5000001 - 10000000”
},
{
“from”:10000001,
“name”:“10000001+”
}
]
},
“date”:{
“type”:“range”,
“ranges”:[
{
“from”:“2020-09-15T16:00:15.810Z”,
“to”:“2020-09-16T16:00:15.810Z”,
“name”:“Today”
},
{
“from”:“2020-09-15T16:00:15.810Z”,
“to”:“2020-09-22T16:00:15.810Z”,
“name”:“Next 7 Days”
},
{
“from”:“2020-09-22T16:00:15.810Z”,
“to”:“2020-09-29T16:00:15.810Z”,
“name”:“22/09/2020 to 29/09/2020”
},
{
“from”:“2020-09-29T16:00:15.819Z”,
“name”:“After 14 days”
}
]
},
“currency”:{
“type”:“value”
}
},

Response:

“facets”:{
“currency”:[
{
“type”:“value”,
“data”:[
{
“value”:“USD”,
“count”:483046
},
{
“value”:“EUR”,
“count”:195327
},
{
“value”:“GBP”,
“count”:37399
},
{
“value”:“CHF”,
“count”:14690
},
{
“value”:“AUD”,
“count”:2024
},
{
“value”:“CAD”,
“count”:812
},
{
“value”:“HKD”,
“count”:789
},
{
“value”:“ZAR”,
“count”:85
},
{
“value”:“CZK”,
“count”:34
},
{
“value”:“MXD”,
“count”:27
}
]
}
],
“ratemin”:[
{
“type”:“range”,
“data”:[
{
“to”:10000.0,
“from”:0.0,
“name”:“0 - 10000”,
“count”:734339
},
{
“to”:100000.0,
“from”:10001.0,
“name”:“10001 - 100000”,
“count”:0
},
{
“to”:500000.0,
“from”:100001.0,
“name”:“100001 - 500000”,
“count”:0
},
{
“to”:1000000.0,
“from”:500001.0,
“name”:“500001 - 1000000”,
“count”:0
},
{
“to”:5000000.0,
“from”:1000001.0,
“name”:“1000001 - 5000000”,
“count”:0
},
{
“to”:1.0E7,
“from”:5000001.0,
“name”:“5000001 - 10000000”,
“count”:0
},
{
“from”:1.0000001E7,
“name”:“10000001+”,
“count”:0
}
]
}
],
“date”:[
{
“type”:“range”,
“data”:[
{
“to”:“2020-09-16T16:00:15.810Z”,
“from”:“2020-09-15T16:00:15.810Z”,
“name”:“Today”,
“count”:197
},
{
“to”:“2020-09-22T16:00:15.810Z”,
“from”:“2020-09-15T16:00:15.810Z”,
“name”:“Next 7 Days”,
“count”:573
},
{
“to”:“2020-09-29T16:00:15.810Z”,
“from”:“2020-09-22T16:00:15.810Z”,
“name”:“22/09/2020 to 29/09/2020”,
“count”:0
},
{
“from”:“2020-09-29T16:00:15.819Z”,
“name”:“After 14 days”,
“count”:75
}
]
}
]
}

@Jonathan_Gautier to get counts in a $search pipeline the simplest way to get that information today is to add a second stage that is $count for each of the corresponding pipelines beginning with a $search stage. Have you tried that?

1 Like

Unfortunately, we do not support an implementation very similar to the way you would implement it in Mongo or Elasticsearch today.

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