How To Get Content From Craft CMS Into Meilisearch

What Is Meilisearch?

Previously I posted an overview of Meilisearch which should get you started with some of the broad strokes. But briefly here, Meilisearch is a search utility that you can add to your website to greatly speed up and enhance search. Meilisearch is compatible with any website. Your developer just uses normal Javascript to include Meilisearch functionality on your web pages.

How Does Meilisearch Work?

You can run your own Meilisearch server or you can use the hosted service provided by Meilisearch. At Solspace, we like the sense of control we have by running Meilisearch servers for our clients. This is a big improvement from what you get from Algolia, Meilisearch's main rival.

So you run a Meilisearch server. The software is open source. You set up an instance and then you build search indexes that are filled with documents. Each document within an index is structurally identical. A document represents an item of content on your website like a product or an aspect of your service offering.

Your customers or clients use the search interface your developer builds into your website to search with keywords or with filtering checkboxes and pulldowns. This is our favorite capability. Your customers are sorting through your products. They can slice and dice the list based on detailed criteria specific to their needs. And all of it happens faster than your customer can blink their eyes.

How Do You Build A Meilisearch Index?

The two most important things for you to think about with Meilisearch are the search interface mentioned above and the method for populating the search indexes. This guide is about Meilisearch and Craft CMS, so we'll talk about how a Craft plugin can be built to populate Craft content into Meilisearch with no friction at all.

How Do You Get Content From Craft CMS Into Meilisearch?

Ideally each time your content or product teams publish or update content, that information will also be sent into your Meilisearch instance. Craft CMS is so great at being extensible for purposes like this that creating a plugin to support the work takes very little time.

Build A Craft CMS Plugin

There is a thriving ecosystem of Craft plugins, both free and commercial. There's already an open source and well-supported plugin to connect Craft to Algolia called Scout. Your web developer can build a similar plugin to connect Craft with Meilisearch.

To build a new Craft plugin we always like to start with the Plugin Factory provided by the good people at nystudio107. This handy tool gives your web developer the scaffolding of a proper Craft CMS plugin. They then customize your needed functionality into that scaffolding. This approach saves a ton of time.

Your web developer will then fold the PHP SDK provided by Meilisearch into their plugin. This SDK makes the necessary API calls to your Meilisearch instance much easier to manage. In fact, most of that API connection work is done for you already.

Make The Plugin Listen For Events

Once your developer has the plugin scaffolding in place and they have pulled in the Meilisearch SDK, they can then add code to have the plugin listen for specific events in Craft. The main one of interest is the event that fires when someone updates an entry in the Craft control panel. It looks like this in your developers' plugin code:

    function (ModelEvent $event)
        // Add your code here to process and send entry data to your Meilisearch index.

Since Craft makes it so easy to write custom code that can listen for system events like the one above, your developer can build on that and send data to Meilisearch every time one of your content editors publishes or edits content. This would be the main way that Meilisearch stays up to date on searchable content.

Make The Plugin Configurable

Your developer will have had years of experience integrating systems together such as will be needed with Craft and Meilisearch. They'll therefore know that there needs to be an architecture in place to support customizations and changes over time. Your plugin needs to be configurable.

The recent Meilisearch plugin I wrote has extensive capabilities in this regard. For every individual Meilisearch index, there can be a PHP file that handles a virtually infinite array of ways to customize and manipulate the data that gets sent from Craft to Meilisearch. There's also a concept of manipulating individual field values before they go to Meilisearch. As well, multiple fields can be combined into single Meilisearch attributes.

Build A Batching Routine Into The Plugin

Sometimes you will want to make a global change to how one of your Meilisearch indexes is set up. This change will need to affect all documents within the index. It would be ridiculous to go into Craft and hit 'save' on every entry. There's a better way.

Craft CMS supports a queueing concept. Your developer can create 'jobs' within your plugin. These jobs can be placed into Craft's queue and processed in batches over time. Our Meilisearch plugin uses this method to update all documents in a given Meilisearch index. You just set the job in motion and check back in a few minutes. Normally it takes about 30 minutes to process 50,000 records in batches of about 100 each.

Add Controls For Index Settings

Meilisearch has a concept of settings for each index. These settings can be updated over time as needed. The only way to make these changes is through the Meilisearch API. So your plugin needs to have a built-in utility for applying index setting changes. Craft plugins have a utility page concept that is perfect for this. Your developer basically creates a page in the Craft control panel for your plugin. The page will have a variety of links for performing various plugin tasks. Among these is the index settings update utility. Pretty easy.

Let It Run

Once your Meilisearch plugin has been written, you can test it carefully by running a development version of your Craft site as well as by running a staging instance of Meilisearch. Your developer can easily set this up. In fact, they will most likely already have a local version of your Craft site running on their laptop. They can also set up a local Meilisearch server on their laptop. Most of the intricate testing and tuning tasks can take place on these local installs as well as on the development systems.

Once everything is ready, your developer will install your plugin on your production environment and launch it. You'll build up your Meilisearch index and your developer can build out the front-end search interface on your site.

From there, you can let the system run smoothly while you sleep!

Sign up for MORE Solspace

No nonsense. No spam. Just useful free tips, insights, guides, resources and stories.