Skip to main content

A Meilisearch backend for Wagtail CMS

Project description

Wagtailmeili

Test Version codecov Ruff License

A search backend for Wagtail using MeiliSearch.

[!CAUTION] This package is still in development and until version 1.0.0, I will not maintain a DeprecationWarning pattern. I built the integration with meilisearch about 2 years ago for a project and decided to make it a public package to improve it and integrate more features.

[!TIP]
If you need support or require help with a Wagtail project, you can hire me 😊

Introduction

en - https://softquantum.com/resources/wagtailmeili-integrating-a-blazing-fast-search-engine-with-wagtail

fr - https://softquantum.com/fr/ressources/wagtailmeili-integrer-un-moteur-de-recherche-rapide-avec-wagtail/

Requirements

Wagtailmeili requires the following:

  • Python >= 3.11
  • Wagtail >= 5.2

Installation

In your Wagtail project

Configure your MeiliSearch instance in your settings.py file.

Install Meilisearch python client e.g., using pip

  pip install meilisearch

Add wagtailmeili to your INSTALLED_APPS

INSTALLED_APPS = [
    # ...
    "wagtailmeili",
    # ...
]

add the search backend 'meilisearch' to your WAGTAILSEARCH_BACKENDS

[!CAUTION] Leave the 'default' backend for the admin as you don't want to depend only on what was indexed in meilisearch Different use cases to consider so still work in progress.

import os

WAGTAILSEARCH_BACKENDS = {
    "meilisearch": {
        "BACKEND": "wagtailmeili.backend",
        "HOST":  os.environ.get("MEILISEARCH_HOST", "http://127.0.0.1"),
        "PORT": os.environ.get("MEILISEARCH_PORT", "7700"),
        "MASTER_KEY": os.environ.get("MEILISEARCH_MASTER_KEY", "your-master-key"),
        # "STOP_WORDS": ...
        # "RANKING_RULES: ...
        # "SKIP_MODELS": ...
        # "SKIP_MODELS_BY_FIELD_VALUE": ...
    },
    "default": {
        "BACKEND": "wagtail.search.backends.database",
    }
}

Features

Default search configs

  • STOP_WORDS: see defaults in settings.py
  • RANKING_RULES: see defaults in settings.py
  • SKIP_MODELS: "skip_models" is a list of models that you want to skip from indexing no matter the model setup.
WAGTAILSEARCH_BACKENDS = {
    "meilisearch": {
        "SKIP_MODELS": ["app_label.Model1", "app_label.Model2",],
        # ...
    }
}
  • SKIP_MODELS_BY_FIELD_VALUE: A convenient way to skip instances based on attributes
WAGTAILSEARCH_BACKENDS = {
    "meilisearch": {
        # add this to not index pages that are not published for example
        "SKIP_MODELS_BY_FIELD_VALUE": {
            "wagtailmeili_testapp.MoviePage": {
                "field": "live",
                "value": False,
            },
        },
        # ...
    }
}

Model fields

In any model you will be doing a search on with Meilisearch, add the page or model manager.
It will use the correct backend when doing something like MySuperPage.objects.search().

from wagtail.models import Page
from django.db import models
from wagtailmeili.manager import MeilisearchPageManager, MeilisearchModelManager

class MySuperPage(Page):
    """A Super Page to do incredible things indexed in Meilisearch."""
 
    objects = MeilisearchPageManager()

class MySuperModel(models.Model):
    """A Super Model to do incredible things indexed in Meilisearch."""
 
    objects = MeilisearchModelManager()

To declare sortable attributes or add ranking rules for the model, just add, for example:

from wagtail.models import Page


class MySuperPage(Page):
    """A Super Page to do incredible things indexed in Meilisearch."""
    
    sortable_attributes = [
        "published_last_timestamp", 
        # ...
    ]
    ranking_rules = [
        "published_last_timestamp:desc",
        # ...
    ]

Template Tag filter

{% load meilisearch %}

{% get_matches_position result %}

Roadmap before 1.0.0 (unsorted)

  • -[x] Adding tests (v0.3.3)
  • -[x] Cleaning up index if pages are unpublished or models deleted (v0.4.0)
  • -[ ] Refactoring index.py to be with easier testing
  • -[ ] Exploring Meilisearch and bringing more of its features for Wagtail
  • -[ ] Getting a leaner implementation (looking at Autocomplete and rebuilder)
  • -[ ] Giving more love to the Sample project with a frontend
  • -[ ] official documentation

Sample Project: WMDB

The Wagtail Movie Database (WMDB) is a sample project for testing purposes. To run the project, follow these steps:

  1. start the local meilisearch instance
meilisearch --master-key=<your masterKey>
  1. copy the directory wagtail_moviedb wherever you want
  2. create a virtualenv and activate it (instructions on linux/macOS)
python -m venv .venv
source .venv/bin/activate
  1. install the dependencies
pip install -r requirements.txt
  1. add an .env file
MEILISEARCH_MASTER_KEY="your masterKey"
  1. apply migrations
python manage.py migrate
  1. Create a superuser (optional)
python manage.py createsuperuser
  1. load movies & start local web server
python manage.py load_movies
python manage.py runserver
  1. visit your meilisearch local instance: https://127.0.0.1:7700, give it your master-key. You should see some movies loaded.
  2. update index (optional):
python manage.py update_index

Contributions

Welcome to all contributions!

Prerequisites

  • Install Meilisearch locally following their documentation
  • Start Meilisearch instance in your favorite terminal
meilisearch --master-key correctmasterkey

Install

To make changes to this project, first clone this repository:

git clone git@github.com:softquantum/wagtailmeili.git
cd wagtailmeili

With your preferred virtualenv activated, install testing dependencies:

Using pip

pip install "pip>=21.3"
pip install -e '.[dev]' -U

How to run tests

You can run tests as shown below:

pytest 

or with tox

tox

License

This project is released under the 3-Clause BSD License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtailmeili-0.4b0.tar.gz (6.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wagtailmeili-0.4b0-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file wagtailmeili-0.4b0.tar.gz.

File metadata

  • Download URL: wagtailmeili-0.4b0.tar.gz
  • Upload date:
  • Size: 6.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for wagtailmeili-0.4b0.tar.gz
Algorithm Hash digest
SHA256 17065b327a2019a21d159ca932860ce16f7ed3eede932c8d27364f08f1a4c7eb
MD5 2d5c50921d1e68220402d94e1e0204aa
BLAKE2b-256 139dbfcff46bdd9cdd08cd6741c124415374439c5418f55fce79a3b14fd2edc9

See more details on using hashes here.

File details

Details for the file wagtailmeili-0.4b0-py3-none-any.whl.

File metadata

  • Download URL: wagtailmeili-0.4b0-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for wagtailmeili-0.4b0-py3-none-any.whl
Algorithm Hash digest
SHA256 4de9dbc910cfe14873e198105783cbe8a3dedc4b06e8d4b50c81c8f444fd16ff
MD5 e364d33cf9f5f24641456c3ca5d03d58
BLAKE2b-256 e4be592e8e2754acad5a1404bb41f83ae7eb4bcd4d3cae8b5ad3d9631dd49d29

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page