GHIJK
<<< Back to the blog

Push related Craft CMS entry data into Algolia index

it me
Published on
Estimated Reading time
4 mins

Let's take a look at how we can add some good related Craft CMS entry data to an Algolia index and improve the user search experience.

Craft CMS doesn't come with Algolia integration out of the box. We have 2 options at the time of writing: Scout by Rias and the official Algolia plugin.

On the project I was working with, the Algolia plugin didn't exist, so we'll use Scout in this example.

I'm going to assume that you know how to install a plugin to your Craft instance.

With the plugin installed, we need to create a configuration file config/scout.php

<?php

return [
    /*
     * Scout listens to numerous Element events to keep them updated in
     * their respective indices. You can disable these and update
     * your indices manually using the commands.
     */
    'sync' => true,

    /*
     * By default Scout handles all indexing in a queued job, you can disable
     * this so the indices are updated as soon as the elements are updated
     */
    'queue' => true,

    /*
     * The connection timeout (in seconds), increase this only if necessary
     */
    'connect_timeout' => 1,

    /*
     * The batch size Scout uses when importing a large amount of elements
     */
    'batch_size' => 1000,

    /*
     * The Algolia Application ID, this id can be found in your Algolia Account
     * https://www.algolia.com/api-keys. This id is used to update records.
     */
    'application_id' => '$ALGOLIA_APPLICATION_ID',

    /*
     * The Algolia Admin API key, this key can be found in your Algolia Account
     * https://www.algolia.com/api-keys. This key is used to update records.
     */
    'admin_api_key'  => '$ALGOLIA_ADMIN_API_KEY',

    /*
     * The Algolia search API key, this key can be found in your Algolia Account
     * https://www.algolia.com/api-keys. This search key is not used in Scout
     * but can be used through the Scout variable in your template files.
     */
    'search_api_key' => '$ALGOLIA_SEARCH_API_KEY', //optional
    
    /*
     * A collection of indices that Scout should sync to, these can be configured
     * by using the \rias\scout\ScoutIndex::create('IndexName') command. Each
     * index should define an ElementType, criteria and a transformer.
     */
    'indices'       => [],
];

We then need to replace the ALGOLIA_APPLICATION_ID, ALGOLIA_ADMIN_API_KEY and ALGOLIA_SEARCH_API_KEY with the required keys from the Algolia dashboard.

With that config file set, let us consider what we want to achieve. Let's say we have a Craft channel of posts and each post has a single Craft category. We want to have the post info and related category indexed by Algolia.

So, within our indices array in config/scout.php you'd be tempted to do something like

'indices' => [
ScoutIndex::create('posts')
            ->elementType(Entry::class)
            ->criteria(function (EntryQueryAlias $query) {
                return $query->section('posts');
            })
            ->transformer(function (Entry $entry) {
                return [
                    'title' => $entry->title,
                    'category' => $entry->category
                ];
            }),
],

However, if we run ./craft scout/index/refresh posts and take a look at our Algolia index - we can see the category has a lot of irrelevant search data.

Fat Algolia category

So, let's get something a little leaner and more useful for our search experience.

We can change the category to read like this

'category' => $entry->category ? $entry->category->one()->title ?? "" : "",

Now if we refresh the Algolia index again ./craft/scout/index/refresh blog we can see that we have the category title as a string

Skinny Algolia related category

And there we have it, you can now configure Algolia to allow category to be searchable and everything else that Algolia does. Of course you might have multiple categories and perhaps meta data on the category itself, but this should give you a starter for 10.

Need helping hooking up your Craft CMS content to Algolia? Get in touch and we can talk about working together on it 🤓
GHIJK