Skip to content

Commerce Foundations is our pre-built Craft Commerce store. See what it includes

Craft CMS plugins

Meilisearch Connect

Meilisearch Connect enables seamless data synchronization between Craft CMS and Meilisearch.

Recipe: reindex entries when an author changes

An article document includes its author’s name and bio. Register the author as a dependency so a saved author queues each related article for reindexing.

<?php

use craft\elements\Entry;
use craft\elements\db\EntryQuery;
use craft\elements\User;
use fostercommerce\meilisearch\builders\IndexBuilder;
use fostercommerce\meilisearch\builders\IndexSettingsBuilder;

return [
    'meiliHostUrl' => 'http://localhost:7700',
    'meiliAdminApiKey' => '<Meilisearch admin key>',
    'meiliSearchApiKey' => '<Meilisearch search key>',
    'indices' => [
        'articles' => IndexBuilder::fromSettings(
            IndexSettingsBuilder::create()
                ->withSearchableAttributes(['title', 'body', 'authorName', 'authorBio'])
                ->build(),
        )
            ->withElementQuery(
                static fn (): EntryQuery => Entry::find()->section('articles'),
                static function (Entry $entry, callable $registerDependency): array {
                    $author = $entry->author;

                    $registerDependency($author);

                    return [
                        'id' => $entry->id,
                        'title' => $entry->title,
                        'body' => $entry->body,
                        'authorName' => $author->fullName,
                        'authorBio' => $author->bio,
                    ];
                },
            )
            ->build(),
    ],
];

The plugin records the article as the parent source and the author as its dependency.

When the author is saved, the plugin queues a sync for each tracked article that uses that author. When the author is deleted, it does the same before removing the dependency.

This only works after the articles have been indexed at least once.

We can take it from here.

We take on the Craft and Craft Commerce sites you built, so you can get back to building. Introduce a client who signs a management contract and you get a $3,000 partnership fee.

How the Dev Partnership Program works
See if we’re a fit