GithubHelp home page GithubHelp logo

django-meili's Introduction

Django-Meili

A package to integrate Meilisearch with Django in a seamless way.

Usage

Set the following variables in settings.py.

MEILISEARCH = {
   "HOST": "localhost",
   "PORT": 7700,
   "HTTPS": False,
   "MASTER_KEY": "...",
   "SYNC": False, # Should Meilisearch action resolve before continuing
}

Register the app in INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    "django_meili",
    ...,
]

Just subclass django_meili.models.IndexMixin

from django_meili.models import IndexMixin

class Post(IndexMixin, models.Model):
    id = models.UUIDField()
    title = models.CharField(max_length=255)
    body = models.TextField()

    # Attributes to handle in Meilisearch
    displayed_fields = ("title", "body")
    searchable_fields = ("title", "body")

    def __str__(self):
        return self.title

Then when you are ready to search this model, just use the built-in "queryset".

Post.objects.create(title="Hello", body="World")
posts = Post.meilisearch.search("hello") # Returns a Django Queryset of results
print(posts.first()) # Hello

API

django_meili.models.IndexQuerySet

This is a custom queryset built to mimic Django's queryset. Currently does not support Q objects, but that is a plan for the future.

Methods:

Name Description Example
getitem Use slices to set limit and offset. The default is 20 and 0 respectively. Post.meilisearch[:10]
count Return the total number of documents within the index. Does not reflect the count for the search query Post.meilisearch.count()
order_by Takes a Django order_by parameter and sets a sort value based on that. Can take multiple sorts Post.meilisearch.order_by("-likes")
filter Takes a Django filter query. Supports: lte, gte, lt, gt, exact, in, range, isnull, exists Post.meilisearch.filter(title__exact="Hello World")
matching_strategy Set the default strategy. Defaults to last Post.meilisearch.matching_strategy('all')
attributes_to_search_on Choose what fields to search on, defaults to all. Post.meilisearch.attributes_to_search_on("title", "body")
search Executes the actual search. Takes an optional query and returns a Django Queryset of results Post.meilisearch.search()

django_meili.models.IndexMixin

Subclass Parameters:

Name Type Description
index_name Optional[str] The name of the index to generate. Defaults to __name__ attribute.
primary_key Optional[str] The primary key of the index. Defaults to pk.

Attributes:

Name Type Description
displayed_fields tuple[str] The fields to display. By default uses all.
searchable_fields tuple[str] The fields to search. By default uses all.
filterable_fields tuple[str] The fields to filter. By default uses none.
meilisearch django_meili.models.IndexQuerySet The queryset used for filtering and searching

Copyright 2024 Ian Kollipara <[email protected]>

django-meili's People

Contributors

ikollipara avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

django-meili's Issues

Potential error point

This code could potentially error. The reason is create_index is asyncronous in Meilisearch so if your code tries to update the settings before the index gets created it will error. You are unlikely to see this in development because there won't be many tasks and therefore the index should get created quickly.

One solution would be to wait for the index creation to complete. The thing to be aware of here is the program will be blocked while it waits. Most of the time this will be fast and not noticable, but it's something to be aware of. If you want me to do a PR to add this let me know and I can.

If you decide to leave things as they are it will be a race condition kind of bug. It will most likely be rare that you see it, so just be aware that if you see an error here that you can't explain or easily reproduce this could be the issue.

if index_name not in _current_indices:
client.create_index(
cls.__name__ if not index_name else index_name,
{"primaryKey": primary_key},
)
if cls.displayed_fields:
client.index(cls.__name__).update_displayed_attributes(cls.displayed_fields)
if cls.searchable_fields:
client.index(cls.__name__).update_searchable_attributes(
cls.searchable_fields
)
if cls.filterable_fields:
client.index(cls.__name__).update_filterable_attributes(
cls.filterable_fields
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.