Can Atlas Search perform country-specific faceting? For example, one product can have different distribution/classification characteristics in different countries.
Inherently, Atlas Search or the Facets functionality are not “country aware”. However, Atlas Search can perform faceting based on the classification specified, to produce country specific faceting. For example, if there is a products
collection having a country
field in each document, then the operator
part of the $searchMeta
and facet
query can filter on the item
and produce the facet results based on that, which can be country specific.
Here’s a quick example - given this products collection:
> db.products1234.find()
{ "_id" : 1, "item" : "Tea", "country" : "United States" }
{ "_id" : 2, "item" : "Tea", "country" : "United States" }
{ "_id" : 3, "item" : "Coffee", "country" : "United States" }
{ "_id" : 4, "item" : "Coffee", "country" : "United States" }
{ "_id" : 5, "item" : "Coffee", "country" : "United States" }
{ "_id" : 6, "item" : "Coffee", "country" : "United States" }
{ "_id" : 7, "item" : "Tea", "country" : "United Kingdom" }
{ "_id" : 8, "item" : "Tea", "country" : "United Kingdom" }
{ "_id" : 9, "item" : "Tea", "country" : "United Kingdom" }
{ "_id" : 10, "item" : "Tea", "country" : "United Kingdom" }
{ "_id" : 11, "item" : "Coffee", "country" : "United Kingdom" }
{ "_id" : 12, "item" : "Coffee", "country" : "United Kingdom" }
Using an Atlas Search index that uses the stringFacet
and string
datatype mappings, we can run a query like this to perform country-specific faceting where the product is "Tea"
:
> db.products1234.aggregate([
... {
... "$searchMeta": {
... "facet": {
... "operator": {
... "text": {
... "path": "item",
... "query": "Tea"
... }
... },
... "facets": {
... "countryFacet": {
... "type": "string",
... "path": "country"
... }
... }
... }
... }
... }
... ]).pretty()
{
"count" : {
"lowerBound" : NumberLong(6)
},
"facet" : {
"countryFacet" : {
"buckets" : [
{
"_id" : "United Kingdom",
"count" : NumberLong(4)
},
{
"_id" : "United States",
"count" : NumberLong(2)
}
]
}
}
}
The results returned show that for the product “Tea”, the count is 4 for country “United Kingdom” and the count is 2 for country “United States”.
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.