Number Facets on nested field does not resect boundaries

Hello,

I have an index setting that seems valid:

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "variants": {
        "fields": {
          "priceVariants": {
            "fields": {
              "default": {
                "fields": {
                  "price": [
                    {
                      "type": "number"
                    },
                    {
                      "type": "numberFacet"
                    }
                  ]
                },
                "type": "document"
              }
            },
            "type": "document"
          }
        },
        "type": "document"
      }
    }
  }
}

and document that match

    "_id" : "63d32105fa861f7db4b57866-en-published",
    "variants" : [
        {
            "sku" : "my-super-red-t-shirt-1674780931920",
            "priceVariants" : {
                "default" : {
                    "price" : NumberInt(123),
                    "currency" : "eur"
                }
        },
        {
            "sku" : "my-super-t-shirt-1674783282149",
            "priceVariants" : {
                "default" : {
                    "price" : NumberInt(123),
                    "currency" : "eur"
                }
            },
        }
    ]
}`

The index is set and active, I double-checked the typo too.
Not all the documents have variants. I don’t know if that can have an impact.

When I try to query the facets, everything goes to “other” all the time
What do I do wrong?

  {
    "$search": {
      "index": "mycollection",
      "facet": {
        "facets": {
          "priceFacet": {
            "type": "number",
            "path": "variants.priceVariants.default.price",
            "boundaries": [0, 50, 100, 150, 200],
            "default": "other"
          }
        }
      }
    }
  },
  {
    "$facet": {
      "docs": [{ "$limit": 25 }],
      "meta": [{ "$replaceWith": "$$SEARCH_META" }, { "$limit": 1 }],
    }
  },
  {
    "$addFields": {
      "score": { "$meta": "searchScore" }
    }
  },
  {
    "$set": {
      "meta": { "$arrayElemAt": ["$meta", 0] }
    }
  }
]`````````
`

Hi @Sebastien_Morel , this is expected behavior because Atlas Search does not support faceting on numeric values in arrays or in a document contained in an array for faceting. We support faceting on this data model for strings, so you could consider storing this field as a string and index it as a stringFacet.

If this is an area of concern for you, I recommend opening an item in the MongoDB Feedback Engine.

ok good to know, thanks, I did not read that anywhere.

The problem if I do that is that I won’t have boundaries and then I will have a facet per price,
95.45, 99.99, 100, 102, etc., etc. which defeats the logic for a facet of the price, I would round them and manage maybe…

Should I assume that the same happens with DateFacet, which will not work with nested stuff?

Ok then maybe as a workaround, maybe a side question on which you have an idea.
Let’s say I want to have the min/max price for the query… I know how to do it without $search aggregation. With plain simple mongodb.
Something like that in the $facet: aggregation

minPrice: [
  {
    $group: {
      _id: null,
      minPrice: { $min: '$variant.price' },
    },
  },
],
maxPrice: [
  {
    $group: {
      _id: null,
      maxPrice: { $max: '$variant.price' },
    },
  },
],

But when I use $search I need to to $limit before the $facets aggregation otherwise the request is crazy slow on a big number of documents, which is logical because I would fetch them all…
But if I $limit then the min/price is within the “limit” which is not correct.

So I would need to have that min/max from the $search aggregation.

Any trick?

Thanks

Hi @Sebastien_Morel sorry for the delayed response here.

Should I assume that the same happens with DateFacet, which will not work with nested stuff?

At this time, dateFacets are also not supported with nested documents, however, this is an area that we hope to improve in the near future.

Is your requirement for number facets to return the number of matching parent documents or nested child documents? ie. If a document has multiple priceVariants.price within the same boundary (e.g. the boundary is 0-50 and there are two nested documents: one with price: 10 and one with price: 20), does this count as one or two entries towards the bucket 0-50?

Hello @amyjian ,

Yes, that’s exactly that. It’s a pretty common e-commerce setup:
Product > Variants > Prices

I want to be able to search: give me the variants that matches a price between 10 and 100: this is filtering and it works well

But I also want to provide a facet in the search UI for them to click on 20-50 (N products/variants) etc.

Are you saying it could be available soon?

Thanks

Hi @Sebastien_Morel , thanks for adding those details. This is a gap that we are aware of and are looking into addressing in the upcoming months. I recommend voting on this item in the Feedback Portal to stay up-to-date on progress.