Latest Posts

Installing and safely upgrading Meilisearch to the latest version

Services

Meilisearch is an amazing open-source search engine with an intuitive API to rival the likes of Algolia. Upgrading can be scary, so I automated it with a bash script!


Meilisearch is an amazing standalone binary, a performant search engine that rivals the likes of Algolia. It's self hosted, open source and can be run on very little resources.

Upgrading it is a scary process, so I've written an automation script for Linux and thought I'd share it here for my own reference and others!

Installation

To install Meilisearch, simply use their download script, make it executable, move it to your binaries path and and create a systemctl control script:

  1. Grab the meilisearch binary
# execute the installer
curl -s -L https://install.meilisearch.com | sh > /dev/null

# make the binary executable
chmod +x ./meilisearch

# move it to your binary path
mv ./meilisearch /usr/bin/meilisearch
  1. Create a systemctl service file under /etc/systemd/system/meilisearch.service (make sure to set your own master key, I'd recommend a 60+ character token)
Description=MeiliSearch
After=systemd-user-sessions.service
[Service]
Type=simple
ExecStart=/usr/bin/meilisearch --http-addr 0.0.0.0:7700 --db-path=/etc/meilisearch/data.ms --no-analytics --dump-dir=/etc/meilisearch/dumps --env=production --master-key [YOUR MASTER KEY]
[Install]
WantedBy=multi-user.target
  1. Load in your service by running systemctl daemon-reload

  2. Now you can start your service for the very first time by running systemctl start meilisearch, all being well, your service should be running and logs visible via journalctl: journalctl -xe -u meilisearch

Upgrading

  1. Save the below code snippet into a file called upgrade.sh.

WARNING: MAKE SURE YOU TAKE A SNAPSHOT OF YOUR SETUP FIRST

#!/bin/sh

masterKey=[YOUR_MASTER_KEY]

# download latest meilisearch
echo "Downloading latest Meilisearch"
curl -s -L https://install.meilisearch.com | sh > /dev/null

# clear our existing dumps
rm -rf /etc/meilisearch/dumps/*.dump

# create a dump with the existing service
response=$(curl -s -o /dev/null -w "%{http_code}" -X POST "http://0.0.0.0:7700/dumps" -H "Authorization: Bearer $masterKey")

# check we managed to queue a dump...
if [ "$response" -ne 202 ]; then
  echo "Failed to create dump: Response code $response"
  exit 1
fi

echo "Waiting for dump creation..."
while ! test -f /etc/meilisearch/dumps/*; do sleep 1; done

filename=$(ls /etc/meilisearch/dumps/*)

echo "Found dump $filename, lets give it another 10 seconds..."

sleep 10

echo "Stopping Meilisearch"
systemctl stop meilisearch

# create our new binary and move it
chmod +x ./meilisearch
mv ./meilisearch /usr/bin/meilisearch

# load our new binary
systemctl daemon-reload

# move our data directory
mv /etc/meilisearch/data.ms /etc/meilisearch/data.ms.bkp

# run with our dump
/usr/bin/meilisearch --import-dump "$filename" --http-addr 0.0.0.0:7700 --db-path=/etc/meilisearch/data.ms --no-analytics --dump-dir=/etc/meilisearch/dumps  --env=production --master-key $masterKey
  1. Run this file by calling sh ./upgrade.sh. This script automates the snapshotting up, stopping and restoring the snapshot of Meilisearch.

Once you see the Meilisearch ASCII text, give everything a test and check everything is working as expected, exit out of the process and call on systemctl to manage the process itself with systemctl restart meilisearch

If you're not such a fan of managing your own infrastructure, Meilisearch has their own competitively priced cloud offering, suprisingly called Meilisearch Cloud!