django_small_white Created with Sketch.

Setup

Introduction

This package lets you easily integrate the Algolia Search API to your Django project. It’s based on the algoliasearch-client-python package.

You might be interested in this sample Django application providing a typeahead.js based auto-completion and Google-like instant search: algoliasearch-django-example

Compatible with Python 2.7, Python 3.3+ and Django 1.7+

Install

pip install algoliasearch-django

Setup

In your Django settings, add algoliasearch_django to INSTALLED_APPS and add these two settings:

ALGOLIA = {
    'APPLICATION_ID': 'MyAppID',
    'API_KEY': 'MyApiKey'
}

There are three optional settings:

  • INDEX_PREFIX: prefix all indexes. Use it to separate different applications, like site1_Products and site2_Products.
  • INDEX_SUFFIX: suffix all indexes. Use it to differenciate development and production environment, like Location_dev and Location_prod.
  • AUTO_INDEXING: automatically synchronize the models with Algolia (default to True).

Quick Start

Simply call algoliasearch.register() for each of the models you want to index. A good place to do this is in your application’s AppConfig (generally named apps.py). More info in the documentation

from django.apps import AppConfig
import algoliasearch_django as algoliasearch

class YourAppConfig(AppConfig):
    name = 'your_app'

    def ready(self):
        YourModel = self.get_model('your_model')
        algoliasearch.register(YourModel)

And then, don’t forget the line below in the __init__.py file of your Django application.

default_app_config = 'your_django_app.apps.YourAppConfig'

By default, all the fields of your model will be used. You can configure the index by creating a subclass of AlgoliaIndex. A good place to do this is in a separate file, like index.py.

from algoliasearch_django import AlgoliaIndex

class YourModelIndex(AlgoliaIndex):
    fields = ('name', 'date')
    geo_field = 'location'
    settings = {'searchableAttributes': ['name']}
    index_name = 'my_index'

And then replace algoliasearch.register(YourModel) with algoliasearch.register(YourModel, YourModelIndex).