Algolia Search integration for Django

4.0.0 · active · verified Sun Apr 12

This package provides seamless integration of the Algolia Search API into Django projects. It is currently at version 4.0.0 and is actively maintained, with a focus on compatibility with modern Python and Django versions, built upon the `algoliasearch-client-python` library.

Warnings

Install

Imports

Quickstart

To get started, first configure your Algolia credentials in your Django `settings.py` under an `ALGOLIA` dictionary. Then, define your Django models. Finally, create an `index.py` file within your app and register your models for indexing using either `algoliasearch.register()` or the `@register` decorator with a subclass of `AlgoliaIndex` to specify index settings and fields. Remember to add `algoliasearch_django` to your `INSTALLED_APPS`.

import os
import django
from django.conf import settings
from django.db import models

# Configure Django settings minimally for standalone execution
if not settings.configured:
    settings.configure(
        INSTALLED_APPS=[
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'myapp',
            'algoliasearch_django'
        ],
        DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}},
        ALGOLIA={
            'APPLICATION_ID': os.environ.get('ALGOLIA_APPLICATION_ID', 'YOUR_APP_ID'),
            'API_KEY': os.environ.get('ALGOLIA_API_KEY', 'YOUR_API_KEY'),
            'INDEX_PREFIX': 'dev_',
        },
        DEFAULT_AUTO_FIELD='django.db.models.AutoField',
        SECRET_KEY='a-very-secret-key',
        DEBUG=True
    )
django.setup()

# models.py (example in a fictional 'myapp')
class Product(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    price = models.DecimalField(max_digits=10, decimal_places=2)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        app_label = 'myapp'

# index.py (example in 'myapp')
from algoliasearch_django import AlgoliaIndex
from algoliasearch_django.decorators import register

@register(Product)
class ProductIndex(AlgoliaIndex):
    fields = ('name', 'description', 'price', 'created_at')
    settings = {'searchableAttributes': ['name', 'description'], 'attributesForFaceting': ['price']}
    # You can customize `get_attribute_name` or `get_extra_attributes` for more complex indexing

# --- Example Usage (after defining models and index, typically run via manage.py) ---

# This part would typically be run via `python manage.py shell` or a management command.
# For demonstration, we simulate creating and saving an object.
# In a real Django app, signals handle auto-indexing on save.

# Create a product (this would automatically index if AUTO_INDEXING is True, which is default)
# from myapp.models import Product
# p1 = Product.objects.create(name="Wireless Headphones", description="High-quality audio experience.", price=199.99)
# p2 = Product.objects.create(name="Bluetooth Speaker", description="Portable and powerful sound.", price=79.99)
# print("Products created and indexed.")

# To manually reindex all: python manage.py algolia_reindex
# To apply settings: python manage.py algolia_applysettings

print("Algolia Django integration configured. To index data, run 'python manage.py algolia_reindex' or save model instances.")

view raw JSON →