In this post where we'll explore the process of migrating from Algolia to Meilisearch. 

Both Algolia and Meilisearch are powerful search engines that can enhance your website's functionality. However, you might be considering migrating to Meilisearch due to its open-source nature, cost-effectiveness, or other unique features it offers.

Let's get started!

What is Meilisearch?

Meilisearch is an open-source, instant search engine that has been designed with developers in mind. It offers a variety of features such as typo tolerance, filters and facets, and synonym management. Compared to Algolia, Meilisearch is not only cost-effective but also offers greater flexibility and control over your data and search functionality.

Installing Meilisearch

Before installing Meilisearch, ensure your system meets the minimum requirements, which include a Unix-like system and a Rust toolchain.

This should be the case for most all cloud providers, or of you're running locally on a macOS computer.

  1. Download Meilisearch:
    1. curl -L https://install.meilisearch.com | sh
    2. ./meilisearch --master-key="aSampleMasterKey"
  2. Next, set up Meilisearch by running ./meilisearch --master-key="aSampleMasterKey"
  3. You can configure Meilisearch by using command line options

If you encounter any issues during the installation, refer to the Meilisearch documentation or community forums for troubleshooting tips.

Migrating from Algolia to Meilisearch

Before migrating, ensure you have a backup of your Algolia data and have planned the migration process to minimize downtime.

There are some differences between Algolia and Meilisearch. Although migrating should be straight forward in most cases, understanding the differences is important. You can find more information on the Meilisearch documenation.

General steps we'll be performing

  1. Creating an Algolia client
  2. Fetching data from Algolia
  3. Creating a Meilisearch client
  4. Importing data into Meilisearch instance

Initialize project and install dependencies

      mkdir algolia-meilisearch-migration
cd algolia-meilisearch-migration
npm init -y
touch script.js
npm install -s [email protected] [email protected]
    

Create Algolia client

Inside the newly created script.js file, add the following

      const algoliaSearch = require("algoliasearch");

const algoliaClient = algoliaSearch(
  "APPLICATION_ID",
  "ADMIN_API_KEY"
);
const algoliaIndex = algoliaClient.initIndex("INDEX_NAME");
    
Replace APPLICATION_ID and ADMIN_API_KEY with your Algolia application ID and admin API key respectively.Replace INDEX_NAME with the name of the Algolia index you would like to migrate to Meilisearch.

Fetch data from Algolia

To fetch all Algolia index data at once, use Algolia's browseObjects method.

The batch callback method is invoked on each batch of hits and the content is concatenated in the records array. We will use records again later in the upload process.

      let records = [];
await algoliaIndex.browseObjects({
    batch: (hits) => {
      records = records.concat(hits);
    }
  });
    

Get list of Meilisearch keys

Be sure to visit the API keys documentation for more information. You'll likely be using this to create new keys.

You can programmatically generate keys with scoped searches to ensure a column exist, for example. This is a good way to allow multi-tenancy searches within the same Meilisearch instance.

      curl \
  -X GET 'http://localhost:7700/keys' \
  -H 'Authorization: Bearer MASTER_KEY'
    
Replace MASTER_KEY with the master key used to start you Meilisearch instances.

Create Meilisearch client

Create a Meilisearch client by passing the host URL and API key of your Meilisearch instance. The easiest option is to use the automatically generated admin API key.

      const { MeiliSearch } = require("meilisearch");

const meiliClient = new MeiliSearch({
  host: "MEILI_HOST",
  apiKey: "MEILI_API_KEY",
});
const meiliIndex = meiliClient.index("MEILI_INDEX_NAME");
    
Replace MEILI_HOST,MEILI_API_KEY, and MEILI_INDEX_NAME with your Meilisearch host URL, Meilisearch API key, and the index name where you would like to add documents. Meilisearch will create the index if it doesn't already exist.

Upload data to Meilisearch

Next, use the Meilisearch JavaScript method addDocumentsInBatches to upload all your records in batches of 100,000.

      const BATCH_SIZE = 100000;
await meiliIndex.addDocumentsInBatches(records, BATCH_SIZE);
    

That's all!

When you're ready to run the script, enter the below command:

      node script.js
    

Finished script

Here's the complete script - be sure to replace the placeholder variables with the appropriate keys.

      const algoliaSearch = require("algoliasearch");
const { MeiliSearch } = require("meilisearch");

const BATCH_SIZE = 1000;

(async () => {
  const algoliaClient = algoliaSearch("APPLICATION_ID", "ADMIN_API_KEY");
  const algoliaIndex = algoliaClient.initIndex("INDEX_NAME");

  let records = [];
  await algoliaIndex.browseObjects({
    batch: (hits) => {
      records = records.concat(hits);
    }
  });

  const meiliClient = new MeiliSearch({
    host: "MEILI_HOST",
    apiKey: "MEILI_API_KEY",
  });
  const meiliIndex = meiliClient.index("MEILI_INDEX_NAME");

  await meiliIndex.addDocumentsInBatches(records, BATCH_SIZE);
})();
    

Making the switch from Algolia to Meilisearch can offer numerous benefits

Some of the benefits include: cost savings, greater control over your data, and the flexibility of an open-source system.

We hope this guide has made the process of installing Meilisearch and migrating from Algolia easier.

Ready to give Meilisearch a try and explore the benefits it can bring to your website?

We have experience setting up and custom Meilisearch instances and migrating systems away from Algolia to Meilisearch.

Contact us to find out more!