How to Filter Nested Attributes with Facets in Algolia Flutter?

Hi Algolia Community,

I’m facing an issue with filtering nested attributes using facets in Algolia and need some guidance.

I have a product index where each product contains an array of attributes under ProductAttributes. Each attribute has a Name and an array of Values. Here’s an example of my product document:

{
“id”: “1”,
“name”: “Product 1”,
“price”: 100,
“category”: “Category 1”,
“ProductAttributes”: [
{ “Name”: “Color”, “Values”: [“Blue”, “Red”] },
{ “Name”: “Gender”, “Values”: [“Women”] },
{ “Name”: “Size”, “Values”: [“XS”, “S”, “M”, “L”, “XL”] }
]
}
Problem: I want to enable filtering based on ProductAttributes.Name and the corresponding ProductAttributes.Values. For example, if a user selects Color, they should see the values Blue and Red to filter by. Similarly, selecting Size should show XS, S, M, L, and XL.

Currently, my facets show all values under every attribute name, which is not the desired behavior. I need to dynamically fetch and display ProductAttributes.Values based on the selected ProductAttributes.Name.

What I’ve Tried:

  1. Configured ProductAttributes.Name and ProductAttributes.Values as facets in the Algolia dashboard.
  2. Used SearchForFacets to fetch ProductAttributes.Name and then ProductAttributes.Values based on the selected name.

Example Code:

Here’s a snippet of my current implementation in Flutter: Future _fetchNameFacets() async {
// Fetch ProductAttributes.Name facets
final nameRequest = SearchForFacets(
indexName: ‘StoreProducts’,
facet: ‘ProductAttributes.Name’,
facetQuery: ‘’,
);
final nameResponse = await client.searchForFacets(requests: [nameRequest]);

// Initialize facets and fetch corresponding values
for (var facet in nameResponse.first.facetHits) {
    _fetchValueFacets(facet.value);
}

}

Future _fetchValueFacets(String attributeName) async {
// Fetch ProductAttributes.Values based on selected name
final valueRequest = SearchForFacets(
indexName: ‘StoreProducts’,
facet: ‘ProductAttributes.Values’,
facetQuery: ‘’,
filters: [‘ProductAttributes.Name:$attributeName’].join(’ AND '),
);
final valueResponse = await client.searchForFacets(requests: [valueRequest]);

// Update state with fetched values
setState(() {
    _facetsByAttribute[attributeName] = valueResponse.first.facetHits
        .map((facet) => SelectableFacet(facet.value, facet.count))
        .toList();
});

}

Question:

  1. How can I properly structure and query my data to ensure that selecting a ProductAttributes.Name only shows the corresponding ProductAttributes.Values?
  2. Is there a specific configuration or approach I should follow to handle this nested facet scenario in Algolia?

Thank you in advance for your help!