Custom Django integration for MeiliSearch.
Project description
Django-Meili
A package to integrate Meilisearch with Django in a seamless way. This pacakge is tested on Meilisearch v1.60
.
Install
pip install django_meili
Usage
Settings
Update your settings.py
file to include the following:
INSTALLED_APPS = [
# ..., <--- Any 3rd Party code
"django_meili",
# ..., <--- All your modules
]
# ....
MEILISEARCH = {}
You must define the django_meili
application before any of your code that
uses the application.
Example in Models
Update a model to include the following:
from django_meili.models import IndexMixin
from django.db import models
class Post(IndexMixin, models.Model):
"""Model definition for Post."""
title = models.CharField(max_length=255)
body = models.TextField()
class Meta:
"""Meta definition for Post."""
verbose_name = "Post"
verbose_name_plural = "Posts"
class MeiliMeta:
filterable_fields = ("title",)
searchable_fields = ("id", "title", "body")
displayed_fields = ("id", "title", "body")
def __str__(self):
return self.title
Searching
Now you can search from meilisearch using Model.meilisearch
:
Post.meilisearch.search("Hello World") # => <Queryset for Post>
API
MEILISEARCH
in settings.py
These are the settings available to the package. The values show are the defaults.
MEILISEARCH = {
'HTTPS': False, # Whether HTTPS is enabled for the meilisearch server
'HOST': 'localhost', # The host for the meilisearch server
'MASTER_KEY': None, # The master key for meilisearch. See https://www.meilisearch.com/docs/learn/security/basic_security for more detail
'PORT': 7700, # The port for the meilisearch server
'TIMEOUT': None, # The timeout to wait for when using sync meilisearch server
'CLIENT_AGENTS': None, # The client agents for the meilisearch server
'DEBUG': DEBUG, # Whether to throw exceptions on failed creation of documents
'SYNC': False, # Whether to execute operations to meilisearch in a synchronous manner (waiting for each rather than letting the task queue operate)
'OFFLINE': False, # Whether to make any http requests for the application.
'DEFAULT_BATCH_SIZE': 1000, # For syncindex the default batch size for import queryset
}
django_meili.models.IndexMixin
The IndexMixin
is how an index is defined on a model.
To configure the IndexMixin
define a class on the model called MeiliMeta
.
The IndexMixin
defines two new properties on the model:
meilisearch
- The queryset used to search._meilisearch
- theMeiliMeta
values available on the model.
In addition, the IndexMixin
defines three methods:
meili_filter()
- Should this row be synced in meilisearchmeili_serialize()
- How the model is serialized into a dictionarymeili_geo()
- What does the_geo
column look like (optional)
MeiliMeta
The listed values here are default values. The displayed, searchable, filterable, and sortable should all be iterables containing field names, see the example above.
class MeiliMeta:
displayed_fields = None # the fields displayed when querying meilisearch
searchable_fields = None # the searchable fields when querying meilisearch
filterable_fields = None # the fields available to filter by using meilisearch
sortable_fields = None # the fields that can be sorted by using meilisearch
supports_geo = False # Does the model support geolocation
index_name = "<model.__name__>" # the name of the meilisearch index
primary_key = "pk" # the primary key field for the index
django_meili.querysets.IndexQuerySet
The queryset defines the searchable operations on the index. It attempts to mimic the django queryset API, but differs in 2 notable ways:
- To do geo-filtering, you pass a positional argument
- Not all queryset operations are implemented.
Development
- clone the repo
- ./bin/setup.sh
- ./bin/test.sh
- Develop
Contact
If there are any issues, please feel free to make an issue. If you have suggested improvements, please make an issue where we can discuss.
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
Built Distribution
File details
Details for the file django_meili-0.0.7.tar.gz
.
File metadata
- Download URL: django_meili-0.0.7.tar.gz
- Upload date:
- Size: 104.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 122915d42ab4328591c40f3826a9f8d76e1093c6772c0566e5cb2f3070cdd257 |
|
MD5 | 3c640c052daed8160f7732fa5b520831 |
|
BLAKE2b-256 | 9a3f439b0a8611a4e6b522ec5c49a70b90810e861db50653b31f53eb81e3da0d |
File details
Details for the file django_meili-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: django_meili-0.0.7-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6d1917c5264e4fdf2686006b930aae1b57a60cfc118d49270c04e2566724508 |
|
MD5 | 03bc5c473ac1c97216ced50ac34fb292 |
|
BLAKE2b-256 | 00b79d9dbc086488ca3e0496a17d069d9854abd551179db9fe277b5b90171045 |