Using MeiliSearch to speed up your search in Django

MeiliSearch is an open-source search engine that is designed to be fast and easy to use. It can be integrated into a Django project to improve the search functionality and provide users with faster and more relevant search results.

Using MeiliSearch to speed up your search in Django
Photo by Daniel Lerman / Unsplash

MeiliSearch is an open-source search engine that is designed to be fast and easy to use. It can be integrated into a Django project to improve the search functionality and provide users with faster and more relevant search results. Here is a step-by-step guide on how to use MeiliSearch to speed up your search in a Django project.

Installing Meilisearch

  1. First, ensure that you have Django and MeiliSearch installed on your system. If you do not already have MeiliSearch installed, you can do so by running the following command:

pip install meilisearch

2. Next, we need to add MeiliSearch to the INSTALLED_APPS list in your Django project's settings.py file. This will allow Django to recognize MeiliSearch as an installed app. Add the following line to your INSTALLED_APPS list:


INSTALLED_APPS = [
    ...
    'meilisearch',
    ...
]

3. In order to use MeiliSearch, we also need to add a MeiliSearch configuration dictionary to the settings.py file. This dictionary will contain information about the MeiliSearch server, such as the host and port. Add the following code to your settings.py file


MEILISEARCH_CONFIG = {
    'host': 'http://localhost',
    'api_key': 'masterKey',
    'search_timeout': 1000,
}

4. With the MeiliSearch configuration in place, we can now add MeiliSearch to the list of installed Django middleware. This will allow MeiliSearch to be used throughout the Django project. Add the following line to the MIDDLEWARE list in your settings.py file:


MIDDLEWARE = [    
...    'meilisearch.middleware.MeiliSearchMiddleware',    

]

5. Finally, we need to run the migrations to create the necessary tables in the database. Run the following command to apply the migrations:


python manage.py migrate

Adding MeiliSearch to a Django Model

Once you have installed MeiliSearch in your Django project, you can add it to a Django model to enable the search functionality. First, you will need to import the MeiliSearchMixin class from the meilisearch library:


from meilisearch.mixins import MeiliSearchMixin

Next, you will need to add the MeiliSearchMixin class as a parent class to your Django model. This will allow the model to use the search functionality provided by MeiliSearch:


class MyModel(MeiliSearchMixin, models.Model): ...

Now, you will need to specify which fields in your model should be indexed by MeiliSearch. This can be done by adding a meilisearch_fields attribute to your model, which should be a list of field names to be indexed:


class MyModel(MeiliSearchMixin, models.Model): 
meilisearch_fields = ['field1', 'field2', 'field3'] ...

Finally, you can use the search() method provided by the MeiliSearchMixin class to search your model using MeiliSearch. This method takes a search query as its argument and returns a list of model instances that match the query:


#Search for model instances that match the given queryresults

MyModel.search('my search query')

With MeiliSearch installed and added to your Django model, you can now enjoy fast and accurate search functionality in your Django project. You can learn more about the advanced features and capabilities of MeiliSearch by visiting their official documentation.

PS: The above article was written with the help of ChatGPT.