How to structure product data for nested catalogue facets?

I saw on an Algolia demo of instant search with faceting that you can have a catalogue facet with nested sub-catalogues and even subs within them. But I could never find out how to structure the data I was pushing to actually give the correctly nested catalogues.

Can anyone give me an example of how the data I push to algolia should be formatted to enable nested catalogue facets as in the demos below?

https://community.algolia.com/instantsearch.js/documentation/#hierarchicalmenu

https://demo.algolia.com/instant-search-demo/

Hi there!

Thanks for your question :slight_smile:
The documentation about this is indeed a bit hidden.

You’ll find the details in our documentation of instantsearch.js for the hierarchicalMenu widget:
https://community.algolia.com/instantsearch.js/documentation/#hierarchicalmenu

There, click on the Requirements tab and you’ll find the following:

The attribute used for faceting must be an object that follows a specific convention.

For example, to build the example menu, our objects are defined like this:

{
  "objectID": 4815162342,
  "hierarchicalCategories": {
    "lvl0": "Appliances"
    "lvl1": "Appliances > Air Conditioners"
    "lvl2": "Appliances > Air Conditioners > Portable Air Conditioners"
  }
}

All attributes (hierarchicalCategories.lvl0/1/2) should be defined as attributesForFaceting in your index configuration.

Each level must repeat the parent breadcrumb.

Let us know if you have further questions!

Hi olance, If one object can have a multiple categories how to design a structure of hierarchical.

By the way I create hierarchicalCategories with multiple categories like this

 "hierarchicalCategories": [
{
    "lvl0": "Appliances"
    "lvl1": "Appliances > Air Conditioners"
    "lvl2": "Appliances > Air Conditioners > Portable Air Conditioners"
  },
{
    "lvl0": "Appliances 2"
    "lvl1": "Appliances 2 > Air Conditioners 2"
    "lvl2": "Appliances 2 > Air Conditioners 2 > Portable Air Conditioners 2"
  },
]

I saw this doc talking about multiple value but i am not sure it’s the best practice or not

{
  "objectID": "123",
  "name": "orange",
  "categories": {
    "lvl0": ["fruits", "color"],
    "lvl1": ["fruits > citrus", "color > orange"]
  }
},
{
  "objectID": "456",
  "name": "grapefruit",
  "categories": {
    "lvl0": ["fruits", "color", "new"],
    "lvl1": ["fruits > citrus", "color > yellow", "new > citrus"]
  }
}

Hi there,

The doc you’ve found (also here in a friendlier version) is indeed the best practice for multiple values per category level.

Using arrays at each level of the hierarchy, the engine will automatically provide the different possible values for each level and instantsearch.js will seamlessly display a product in any of the categories it belongs to when declared this way.

So I really encourage you to merge your two hierarchies to follow the documented pattern :slight_smile:

Sorry I have a question

if my 1st hierarchies is

“categories”: {
“lvl0”: “fruits”,
“lvl1”: “fruits > citrus”,
“lvl2”: “fruits > citrus > orange”
}

and 2nd is

“categories”: {
“lvl0”: “fruits”,
“lvl1”: “fruits > lemon”
}

when i merge 2 hierarchies it would be

“categories”: {
“lvl0”: [“fruits”, “fruits”],
“lvl1”: [“fruits > citrus”, “fruits > lemon”]
“lvl2”: ["fruits > citrus > orange "]
}

or

"categories": {
    "lvl0": ["fruits"],
    "lvl1": ["fruits > citrus", "fruits > lemon"],
     lvl2": ["fruits > citrus > orange"]
}

Thanks for your advice

You don’t need to repeat the common root, so your second solution is the one to go with!

Thanks for you advise

I test indexing my data with old structure

“categories”: [{
“lvl0”: “fruits”,
“lvl1”: “fruits > citrus”,
“lvl2”: “fruits > citrus > orange”
},
{
“lvl0”: “fruits”,
“lvl1”: “fruits > lemon”
}
]

and a new one

“categories”: {
“lvl0”: [“fruits”],
“lvl1”: [“fruits > citrus”, “fruits > lemon”],
lvl2": [“fruits > citrus > orange”]
}

I dinn’t see the difference when i get facet data :frowning:

Just want to know why

It’s true that it actually kinda is the same because the engine smartly accesses the objects in your array in the first version, however it is less optimized in terms of data usage.

It’s been a while since the question was asked but is it possible to use the same logic for refinements?

So, instead of hierarchicalMenu I’d need a hierarchicalRefinementList.

2 Likes