How to structure category hierarchy with many categories?

I’m struggling to understand how I need to format the algolia-record before uploading it. I want to use the filtering and Faceting on the categories.

I have a database containing companies. Each company is categorized by several categories.

An example of many categories.

{
    "id": "1",
    "name": "first record",
    "categories": [
        {
            "categoryName": "sdg",
            "answers": ["8","9","11","12"]
        },
        {
            "categoryName": "sector",
            "answers": [ "private"]
        },
        {
            "categoryName": "industry",
            "answers": [ "mobility", "telecom", "consulting"]
        }
    ]

}

How to format the algolia-category structure for the example structure above?

The example here seems to just have one category.

The example above has just three categories. I need to be able to add many more.

You are correct that on that page, the example only has a single facet called category, but that’s just the name of an attribute. In your case, you’ll want to take each of things you’ve classified as a “category” and make them into attributes in your record. That should look something like:

{
    "objectID": "1",
    "name": "first record",
    "sdg": ["8","9","11","12"],
    "sector": ["private"],
    "industry": [ "mobility", "telecom", "consulting"]
   }
}

Then all of the “categories” become facets in your index configuration with something like:

index.setSettings({
  attributesForFaceting: ['filterOnly(sdg)', 'sector', 'industry']
});

You’ll need to add all possible facets across all records to your configuration. If you’re using InstantSearch for your UI, you can ensure that only the facets that are relevant to the current record set will display.

Thanks Chuck

I have another challenge that I see that I did not explain properly. My records (companies) can be categorized by many categories.
Most of them share the same categories, but one group of companies might have a set of categories that is just used by that group.
So I need to be able to add categories dynamically.

Is it possible to model it like this:

{
    {
        "objectID": "1",
        "name": "first record",
        "categories": {
            "sdg": ["8","9","11","12"],
            "sector": ["private"],
            "industry": [ "mobility", "telecom", "consulting"]
        }
    },
    {
        "objectID": "2",
        "name": "second record",
        "categories:" {
            "sdg": ["8","9","11","12"],
            "sector": ["private"],
            "challenge": [ "Enhanced data collection", "Improving travel safety"]
        }
    }
}

In the above example there are two records. They both have the categories “sdg” and “sector”. But one has the category “industry” and the other “challenge”.

If this is possible. How do I set attributesForFaceting ?
I cannot name them like in your example because I do not know their names. Is it possible to have attributesForFaceting point to all items in the “categories” ?

You will need to know the names of the categories ahead of time to use them for faceting. Facets are reductive rather than additive – the engine needs to know all of the facets across an index ahead of time to create the taxonomy for each facet. If no records for a particular facet are in the results (for instance industry in a search that only contains the second record) then that facet is hidden.

But you need that initial list to be comprehensive across an index. If a record has a new attribute you want to use as a facet, you need to update the list of facets using attributesForFaceting for the engine to calculate facet counts, etc. before you can use it in search.

Do you need to filter based on all of the categories as facets? Are there enough distinct values in these categories to warrant faceting instead of just searching?