Structuring indices in Algolia (from laravel)


I’m brand new to Algolia and Laravel and I’m a bit confused how to structure my data when I index it.

I have two models in my local database called ‘Films’ and ‘Categories’ and they are joined by a pivot table to manage the many to many relationship.

I want users to be able to search by Film title and then have categories as a filter, but I also want users to be able to browse a category.

If I create two indices called Films and Categories, how will these relate? How will algolia know what films belong to which categories?

The documentation doesn’t seem clear on this:


In order to filter films by category, each films must have a category attributes. Unlike SQL where you’d probably store ID’s and link table, in Algolia, you add a full-text name. It can be a string or an array of strings. A Film record should look like

    "title": "A movie",
    "category": ["Comedy", "Something else"],
    "description": "..."

To achieve this in Laravel, you will override the toSearchableArray method in your Film model.

public function toSearchableArray()
    $film = $this->toArray();

    $film['category'] = $this->categories->pluck('name')->all(),
    // ^ You'll have to adapt this surely

    return $film;

About browsing Categories, do you mean browsing the film inside the category? Is so, you don’t even need a Category index. Browsing is like filtering, but with an empty query.

Here is the concept page about filtering:

Please let me know if it’s clear.

Sorry I never replied! That was perfect, thank you.

1 Like