├── tests ├── __init__.py ├── urls.py ├── models.py ├── test_commands.py ├── settings.py ├── test_engine.py └── test_index.py ├── src ├── management │ ├── __init__.py │ └── commands │ │ ├── __init__.py │ │ ├── algolia_clearindex.py │ │ ├── algolia_applysettings.py │ │ └── algolia_reindex.py ├── version.py ├── __init__.py ├── registration.py └── models.py ├── setup.cfg ├── MANIFEST.in ├── requirements.txt ├── runtests.py ├── ChangeLog ├── tox.ini ├── .gitignore ├── LICENSE ├── .travis.yml ├── setup.py └── README.md /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/management/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/management/commands/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/version.py: -------------------------------------------------------------------------------- 1 | VERSION = '1.2.2' 2 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [bdist_wheel] 2 | universal=1 3 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | include README.md 3 | include ChangeLog 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | django>=1.7 2 | algoliasearch 3 | tox 4 | wheel 5 | pypandoc 6 | -------------------------------------------------------------------------------- /tests/urls.py: -------------------------------------------------------------------------------- 1 | from django.conf.urls import patterns 2 | 3 | urlpatterns = patterns('') 4 | -------------------------------------------------------------------------------- /tests/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Example(models.Model): 5 | uid = models.IntegerField() 6 | name = models.CharField(max_length=20) 7 | address = models.CharField(max_length=200) 8 | lat = models.FloatField() 9 | lng = models.FloatField() 10 | category = [] 11 | 12 | def location(self): 13 | return (self.lat, self.lng) 14 | -------------------------------------------------------------------------------- /runtests.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | import sys 5 | 6 | import django 7 | from django.conf import settings 8 | from django.test.utils import get_runner 9 | 10 | 11 | def main(): 12 | os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' 13 | django.setup() 14 | TestRunner = get_runner(settings) 15 | test_runner = TestRunner() 16 | failures = test_runner.run_tests(['tests']) 17 | sys.exit(bool(failures)) 18 | 19 | if __name__ == '__main__': 20 | main() 21 | -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- 1 | ''' 2 | AlgoliaSearch integration for Django. 3 | http://www.algolia.com 4 | ''' 5 | 6 | from __future__ import unicode_literals 7 | 8 | from django.contrib.algoliasearch.models import AlgoliaIndex 9 | from django.contrib.algoliasearch.registration import AlgoliaEngine 10 | from django.contrib.algoliasearch.registration import algolia_engine 11 | 12 | algolia_engine = algolia_engine 13 | register = algolia_engine.register 14 | unregister = algolia_engine.unregister 15 | get_registered_model = algolia_engine.get_registered_models 16 | get_adapter = algolia_engine.get_adapter 17 | raw_search = algolia_engine.raw_search 18 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | CHANGELOG 2 | 3 | 2015-12-03 1.2.2 4 | * [FIX] Compatibility warning with Django 1.9 5 | 6 | 2015-07-09 1.2.1 7 | * [ADD] `get_queryset` for grain indexing possibility 8 | * [ADD] Option to deactivate auto-indexing 9 | * [FIX] Various bugs 10 | 11 | 2015-07-04 1.2.0 12 | * [REMOVE] algolia_buildindex command. Use algolia_reindex instead. 13 | * [CHANGE] Settings format. Last format is still supported. 14 | * [ADD] Unit test. 15 | * [ADD] Tag capacity 16 | * [ADD] Conditional indexing 17 | * [ADD] Search capacity on backend 18 | * [FIX] Invalid custom_objectID attribute 19 | * [FIX] Exception throw by the command when using Django 1.7 20 | -------------------------------------------------------------------------------- /src/management/commands/algolia_clearindex.py: -------------------------------------------------------------------------------- 1 | from django.core.management.base import BaseCommand 2 | from django.contrib import algoliasearch 3 | 4 | 5 | class Command(BaseCommand): 6 | help = 'Clear index.' 7 | 8 | def add_arguments(self, parser): 9 | parser.add_argument('--model', nargs='+', type=str) 10 | 11 | def handle(self, *args, **options): 12 | '''Run the management command.''' 13 | self.stdout.write('Clear index:') 14 | for model in algoliasearch.get_registered_model(): 15 | adapter = algoliasearch.get_adapter(model) 16 | if options.get('model', None) and not (model.__name__ in 17 | options['model']): 18 | continue 19 | 20 | adapter.clear_index() 21 | self.stdout.write('\t* {}'.format(model.__name__)) 22 | -------------------------------------------------------------------------------- /src/management/commands/algolia_applysettings.py: -------------------------------------------------------------------------------- 1 | from django.core.management.base import BaseCommand 2 | from django.contrib import algoliasearch 3 | 4 | 5 | class Command(BaseCommand): 6 | help = 'Apply index settings.' 7 | 8 | def add_arguments(self, parser): 9 | parser.add_argument('--model', nargs='+', type=str) 10 | 11 | def handle(self, *args, **options): 12 | '''Run the management command.''' 13 | self.stdout.write('Apply settings to index:') 14 | for model in algoliasearch.get_registered_model(): 15 | adapter = algoliasearch.get_adapter(model) 16 | if options.get('model', None) and not (model.__name__ in 17 | options['model']): 18 | continue 19 | 20 | adapter.set_settings() 21 | self.stdout.write('\t* {}'.format(model.__name__)) 22 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = 3 | {py27,py32,py33,py34}-django17 4 | {py27,py32,py33,py34,py35}-django18 5 | {py27,py34,py35}-django19 6 | coverage 7 | skip_missing_interpreters = True 8 | 9 | [testenv] 10 | deps = 11 | django17: Django>=1.7,<1.8 12 | django18: Django>=1.8,<1.9 13 | django19: Django>=1.9,<1.10 14 | passenv = 15 | ALGOLIA* 16 | TRAVIS* 17 | commands = python runtests.py 18 | 19 | [testenv:coverage] 20 | basepython = python2.7 21 | deps = coverage 22 | passenv = 23 | ALGOLIA* 24 | TRAVIS* 25 | commands = 26 | coverage run --branch --source=django.contrib.algoliasearch runtests.py 27 | coverage report 28 | 29 | [testenv:coveralls] 30 | basepython = python2.7 31 | deps = 32 | coverage 33 | coveralls 34 | passenv = 35 | ALGOLIA* 36 | TRAVIS* 37 | commands = 38 | coverage run --branch --source=django.contrib.algoliasearch runtests.py 39 | coverage report 40 | coveralls 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /src/management/commands/algolia_reindex.py: -------------------------------------------------------------------------------- 1 | from django.core.management.base import BaseCommand 2 | from django.contrib import algoliasearch 3 | 4 | 5 | class Command(BaseCommand): 6 | help = 'Reindex all models to Algolia' 7 | 8 | def add_arguments(self, parser): 9 | parser.add_argument('--batchsize', nargs='?', default=1000, type=int) 10 | parser.add_argument('--model', nargs='+', type=str) 11 | 12 | def handle(self, *args, **options): 13 | '''Run the management command.''' 14 | self.stdout.write('The following models were reindexed:') 15 | for model in algoliasearch.get_registered_model(): 16 | adapter = algoliasearch.get_adapter(model) 17 | if options.get('model', None) and not (model.__name__ in 18 | options['model']): 19 | continue 20 | 21 | counts = adapter.reindex_all( 22 | batch_size=options.get('batchsize', None)) 23 | self.stdout.write('\t* {} --> {}'.format(model.__name__, counts)) 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Algolia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | install: 3 | - pip install --upgrade pip 4 | - pip install tox 5 | script: 6 | - tox 7 | env: 8 | matrix: 9 | - TOXENV=py27-django17 10 | - TOXENV=py27-django18 11 | - TOXENV=py27-django19 12 | - TOXENV=py32-django17 13 | - TOXENV=py32-django18 14 | - TOXENV=py33-django17 15 | - TOXENV=py33-django18 16 | - TOXENV=py34-django17 17 | - TOXENV=py34-django18 18 | - TOXENV=py34-django19 19 | - TOXENV=py35-django18 20 | - TOXENV=py35-django19 21 | - TOXENV=coveralls 22 | global: 23 | - secure: upiNM5+0o3+OnDmkNUhIiytQwIvDsT/rYuC0yrE3cwQV/UgzYBn66YAO03aBBheL6jjqyzteqKnSAm/sNJ7wPkH4brebG6JPWOYMLa2EPdRRfA+Ztxz8ZwKDwQ8syzsdUiw8aHwcPNB0Uc2wO2cUbBKyq6SfVHGJXDVIsViZgnl8CO3KpvwYJ8gj1aj1GQh7aR7y7lsXuTlO9/uFO3AQS6iIJ5QbWPDtjuFdz31xZbW2nIVIv1pQ/OgKsGt0gyLZ1KzirXv9+5enyqQd3ZdVZVTp516Cyxah2bKFQ0shUVDPrLoHQ3iXKUynV1kEzCcqNa4Icu6JfOIyddB0F1NC6FPkUkhnBIrONrCjWBCbUJFHPBCGzmmtSGGXbcQ9zJXFiTmop2P5aWLyvGpavaH840mz/zRLBCpjACGuuB91+pUdvkTHufia1TUW+KN+YSyXyOPPo8a2HvQb3Hj9ehaxKM7yqdL+DeeCXqZDX6U2UGjrLwPEFMdSojm2c8kDBXN7tI/Gi+TiWsnbn9kBHYt9ZWZRdZ4gN95X8KPaoJu4YC9mxmsG83DEUlM+3otyymSqRqp3x8aEHIfw7+Myxvr2rcb4ul8pQ0ps0MGeZDTMa69FQejrS8CZfw5CUG5rwYLHBijE2+gW4r1HgHv2Yz0MPimAQJ98nTon8gsDmxAIsAk= 24 | - secure: eCgpcXJB4l9qBffkbbBY65666Pr0jkq0dKhhe5MVN46HYtmqTIfucYiMVwouySEQeILkPyb2W/l+lDFBWmbnwJe1/cGbu1ccukt4j7PGun9UpHxN7jnnww+3t7y9pY7VG9g2fC/A9Jxq7TTut4wGu9yqddWBxQczCqidr9POCXDLSPD+SfM2nc1+n2C2uM62pxHZwHIsyl2mWtaglpcQ53GGni4NDmmQw4izT0arOXH0Pc7mCjYK+8ucdtHq3yuhtP2q5nn9jeioJcGNqUMFnh+IAVr/Y5+oLPsqigGbG2sHVBWESKWo08joKRkTBtb8rvO7hmBERdaUumHCRsN2vUCcVTUbV32FaIp1AJbZ09WFpn1KObJyTF3DLa/PkaDWjZ4NqacbHb4Zq+PRbFLLI3UybBz2Cyw1M/8JgxCbAh6KivLaFeebI83aFQuRskIzPAaPEMMW2Si9v55fSfkJ6NESLOPY9h4VhI6qvZjJpXf8lgB1bnm8PkKsILXUL95kn0m2qljfkeH6+YaBfTZhRqWGkXOwtG8BNXl/F+cI5jqksoxZF4jpTDTVBWmExd+RXZFD3NFlA/tNCM9SDuuCfku2beMHOJpaySmBe59IR/H1PDzpLlfIMWavBtMhNajIsBNRCNtazDXKufDXDNEGElnZGY/R9lZHNraK18qU4Gg= 25 | -------------------------------------------------------------------------------- /tests/test_commands.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | from django.core.management import call_command 3 | 4 | from django.contrib.algoliasearch import AlgoliaIndex 5 | from django.contrib.algoliasearch import register 6 | from django.contrib.algoliasearch import unregister 7 | 8 | try: 9 | from StringIO import StringIO 10 | except: 11 | from io import StringIO 12 | 13 | from .models import Example 14 | 15 | 16 | class CommandsTestCase(TestCase): 17 | def setUp(self): 18 | class ExampleIndex(AlgoliaIndex): 19 | field = 'name' 20 | 21 | register(Example, ExampleIndex) 22 | self.output = StringIO() 23 | 24 | def tearDown(self): 25 | unregister(Example) 26 | 27 | def test_reindex(self): 28 | call_command('algolia_reindex', stdout=self.output) 29 | self.output.seek(0) 30 | result = self.output.readlines() 31 | self.assertEqual(len(result), 2) 32 | 33 | regex = r'Example --> \d+' 34 | try: 35 | self.assertRegex(result[1], regex) 36 | except AttributeError: 37 | self.assertRegexpMatches(result[1], regex) 38 | 39 | def test_clearindex(self): 40 | call_command('algolia_clearindex', stdout=self.output) 41 | self.output.seek(0) 42 | result = self.output.readlines() 43 | self.assertEqual(len(result), 2) 44 | 45 | regex = r'Example' 46 | try: 47 | self.assertRegex(result[1], regex) 48 | except AttributeError: 49 | self.assertRegexpMatches(result[1], regex) 50 | 51 | def test_applysettings(self): 52 | call_command('algolia_applysettings', stdout=self.output) 53 | self.output.seek(0) 54 | result = self.output.readlines() 55 | self.assertEqual(len(result), 2) 56 | 57 | regex = r'Example' 58 | try: 59 | self.assertRegex(result[1], regex) 60 | except AttributeError: 61 | self.assertRegexpMatches(result[1], regex) 62 | -------------------------------------------------------------------------------- /tests/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for core project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.8.2. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.8/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.8/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | SECRET_KEY = 'MillisecondsMatter' 18 | DEBUG = False 19 | 20 | # Application definition 21 | INSTALLED_APPS = ( 22 | 'django.contrib.admin', 23 | 'django.contrib.auth', 24 | 'django.contrib.contenttypes', 25 | 'django.contrib.sessions', 26 | 'django.contrib.messages', 27 | 'django.contrib.staticfiles', 28 | 'django.contrib.algoliasearch', 29 | 'tests' 30 | ) 31 | 32 | MIDDLEWARE_CLASSES = ( 33 | 'django.contrib.sessions.middleware.SessionMiddleware', 34 | 'django.middleware.common.CommonMiddleware', 35 | 'django.middleware.csrf.CsrfViewMiddleware', 36 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 37 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 38 | 'django.contrib.messages.middleware.MessageMiddleware', 39 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 40 | 'django.middleware.security.SecurityMiddleware', 41 | ) 42 | 43 | ROOT_URLCONF = 'urls' 44 | 45 | # Database 46 | DATABASES = { 47 | 'default': { 48 | 'ENGINE': 'django.db.backends.sqlite3', 49 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 50 | } 51 | } 52 | 53 | # Internationalization 54 | LANGUAGE_CODE = 'en-us' 55 | TIME_ZONE = 'UTC' 56 | USE_I18N = True 57 | USE_L10N = True 58 | USE_TZ = True 59 | 60 | # AlgoliaSearch settings 61 | ALGOLIA = { 62 | 'APPLICATION_ID': os.getenv('ALGOLIA_APPLICATION_ID'), 63 | 'API_KEY': os.getenv('ALGOLIA_API_KEY'), 64 | 'INDEX_PREFIX': 'django' + os.getenv('TRAVIS_JOB_NUMBER', ''), 65 | 'INDEX_SUFFIX': 'test' 66 | } 67 | -------------------------------------------------------------------------------- /tests/test_engine.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | from django.contrib.algoliasearch import AlgoliaIndex 4 | from django.contrib.algoliasearch.registration import AlgoliaEngine 5 | from django.contrib.algoliasearch.registration import RegistrationError 6 | 7 | from .models import Example 8 | 9 | 10 | class EngineTestCase(TestCase): 11 | def setUp(self): 12 | self.engine = AlgoliaEngine() 13 | 14 | def test_is_register(self): 15 | self.assertFalse(self.engine.is_registered(Example)) 16 | self.engine.register(Example) 17 | self.assertTrue(self.engine.is_registered(Example)) 18 | 19 | def test_get_adapater(self): 20 | self.engine.register(Example) 21 | self.assertEquals(AlgoliaIndex, 22 | self.engine.get_adapter(Example).__class__) 23 | 24 | def test_get_adapater_from_instance(self): 25 | self.engine.register(Example) 26 | instance = Example() 27 | self.assertEquals( 28 | AlgoliaIndex, 29 | self.engine.get_adapter_from_instance(instance).__class__) 30 | 31 | def test_register(self): 32 | self.engine.register(Example) 33 | self.assertIn(Example, self.engine.get_registered_models()) 34 | 35 | def test_register_with_custom_index(self): 36 | class ExampleIndex(AlgoliaIndex): 37 | pass 38 | 39 | self.engine.register(Example, ExampleIndex) 40 | self.assertEqual(ExampleIndex.__name__, 41 | self.engine.get_adapter(Example).__class__.__name__) 42 | 43 | def test_register_fail(self): 44 | self.engine.register(Example) 45 | with self.assertRaises(RegistrationError): 46 | self.engine.register(Example) 47 | 48 | def test_unregister(self): 49 | self.engine.register(Example) 50 | self.engine.unregister(Example) 51 | self.assertNotIn(Example, self.engine.get_registered_models()) 52 | 53 | def test_unregister_fail(self): 54 | with self.assertRaises(RegistrationError): 55 | self.engine.unregister(Example) 56 | 57 | # TODO: test signalling hooks and update/delete methods 58 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import os 4 | import sys 5 | 6 | try: 7 | from setuptools import setup 8 | except ImportError: 9 | from distutils.core import setup 10 | 11 | 12 | # Allow setup.py to be run from any path 13 | os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))) 14 | 15 | path_readme = os.path.join(os.path.dirname(__file__), 'README.md') 16 | try: 17 | import pypandoc 18 | README = pypandoc.convert(path_readme, 'rst') 19 | except (IOError, ImportError): 20 | with open(path_readme) as readme: 21 | README = readme.read() 22 | 23 | path_version = os.path.join(os.path.dirname(__file__), 'src/version.py') 24 | if sys.version_info[0] == 3: 25 | exec(open(path_version).read()) 26 | else: 27 | execfile(path_version) 28 | 29 | 30 | setup( 31 | name = 'algoliasearch-django', 32 | version = VERSION, 33 | license = 'MIT License', 34 | packages = [ 35 | 'django.contrib.algoliasearch', 36 | 'django.contrib.algoliasearch.management', 37 | 'django.contrib.algoliasearch.management.commands' 38 | ], 39 | package_dir = { 40 | 'django.contrib.algoliasearch': 'src', 41 | 'django.contrib.algoliasearch.management': 'src/management', 42 | 'django.contrib.algoliasearch.management.commands': 'src/management/commands' 43 | }, 44 | install_requires = ['django>=1.7', 'algoliasearch'], 45 | description = 'Algolia Search integration for Django', 46 | long_description = README, 47 | author = 'Algolia Team', 48 | author_email = 'support@algolia.com', 49 | url = 'https://github.com/algolia/algoliasearch-django', 50 | keywords = ['algolia', 'pyalgolia', 'search', 'backend', 'hosted', 'cloud', 51 | 'full-text search', 'faceted search', 'django'], 52 | classifiers = [ 53 | 'Environment :: Web Environment', 54 | 'Framework :: Django', 55 | 'Intended Audience :: Developers', 56 | 'License :: OSI Approved :: MIT License', 57 | 'Operating System :: OS Independent', 58 | 'Programming Language :: Python', 59 | 'Programming Language :: Python :: 2', 60 | 'Programming Language :: Python :: 3', 61 | 'Topic :: Internet :: WWW/HTTP', 62 | ] 63 | ) 64 | -------------------------------------------------------------------------------- /src/registration.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | import logging 4 | 5 | from django.conf import settings 6 | from django.db.models.signals import post_save, pre_delete 7 | 8 | from django.contrib.algoliasearch.models import AlgoliaIndex 9 | from django.contrib.algoliasearch.version import VERSION 10 | 11 | from algoliasearch import algoliasearch 12 | 13 | logger = logging.getLogger(__name__) 14 | 15 | 16 | class AlgoliaEngineError(Exception): 17 | '''Something went wrong with Algolia engine.''' 18 | 19 | 20 | class RegistrationError(AlgoliaEngineError): 21 | '''Something went wrong when registering a model with the search sengine.''' 22 | 23 | 24 | class AlgoliaEngine(object): 25 | def __init__(self, app_id=None, api_key=None): 26 | '''Initializes Algolia engine.''' 27 | params = getattr(settings, 'ALGOLIA', None) 28 | 29 | if not (app_id and api_key): 30 | if params: 31 | app_id = params['APPLICATION_ID'] 32 | api_key = params['API_KEY'] 33 | else: 34 | # @Deprecated: 1.1.0 35 | app_id = settings.ALGOLIA_APPLICATION_ID 36 | api_key = settings.ALGOLIA_API_KEY 37 | 38 | if params: 39 | self.auto_indexing = params.get('AUTO_INDEXING', True) 40 | 41 | self.__registered_models = {} 42 | self.client = algoliasearch.Client(app_id, api_key) 43 | self.client.set_extra_header('User-Agent', 44 | 'Algolia for Django {}'.format(VERSION)) 45 | 46 | def is_registered(self, model): 47 | '''Checks whether the given models is registered with Algolia engine.''' 48 | return model in self.__registered_models 49 | 50 | def register(self, model, index_cls=AlgoliaIndex): 51 | ''' 52 | Registers the given model with Algolia engine. 53 | 54 | If the given model is already registered with Algolia engine, a 55 | RegistrationError will be raised. 56 | ''' 57 | # Check for existing registration 58 | if self.is_registered(model): 59 | raise RegistrationError( 60 | '{} is already registered with Algolia engine'.format(model)) 61 | # Perform the registration. 62 | index_obj = index_cls(model, self.client) 63 | self.__registered_models[model] = index_obj 64 | 65 | if self.auto_indexing: 66 | # Connect to the signalling framework. 67 | post_save.connect(self.__post_save_receiver, model) 68 | pre_delete.connect(self.__pre_delete_receiver, model) 69 | logger.info('REGISTER %s', model) 70 | 71 | def unregister(self, model): 72 | ''' 73 | Unregisters the given model with Algolia engine. 74 | 75 | If the given model is not registered with Algolia engine, a 76 | RegistrationError will be raised. 77 | ''' 78 | if not self.is_registered(model): 79 | raise RegistrationError( 80 | '{} is not registered with Algolia engine'.format(model)) 81 | # Perform the unregistration. 82 | del self.__registered_models[model] 83 | 84 | if self.auto_indexing: 85 | # Disconnect fron the signalling framework. 86 | post_save.disconnect(self.__post_save_receiver, model) 87 | pre_delete.disconnect(self.__pre_delete_receiver, model) 88 | logger.info('UNREGISTER %s', model) 89 | 90 | def get_registered_models(self): 91 | ''' 92 | Returns a sequence of models that have been registered with Algolia 93 | engine. 94 | ''' 95 | return list(self.__registered_models.keys()) 96 | 97 | def get_adapter(self, model): 98 | '''Returns the adapter associated with the given model.''' 99 | if self.is_registered(model): 100 | return self.__registered_models[model] 101 | raise RegistrationError( 102 | '{} is not registered with Algolia engine'.format(model)) 103 | 104 | def get_adapter_from_instance(self, instance): 105 | model = instance.__class__ 106 | return self.get_adapter(model) 107 | 108 | def update_obj_index(self, obj): 109 | adapter = self.get_adapter_from_instance(obj) 110 | adapter.update_obj_index(obj) 111 | 112 | def delete_obj_index(self, obj): 113 | adapter = self.get_adapter_from_instance(obj) 114 | adapter.delete_obj_index(obj) 115 | 116 | def raw_search(self, model, query='', params={}): 117 | '''Return the raw JSON.''' 118 | adapter = self.get_adapter(model) 119 | return adapter.raw_search(query, params) 120 | 121 | # Signalling hooks. 122 | 123 | def __post_save_receiver(self, instance, **kwargs): 124 | '''Signal handler for when a registered model has been saved.''' 125 | logger.debug('RECEIVE post_save FOR %s', instance.__class__) 126 | self.update_obj_index(instance) 127 | 128 | def __pre_delete_receiver(self, instance, **kwargs): 129 | '''Signal handler for when a registered model has been deleted.''' 130 | logger.debug('RECEIVE pre_delete FOR %s', instance.__class__) 131 | self.delete_obj_index(instance) 132 | 133 | # Algolia engine 134 | algolia_engine = AlgoliaEngine() 135 | -------------------------------------------------------------------------------- /tests/test_index.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | from django.db import models 3 | 4 | from django.contrib.algoliasearch import AlgoliaIndex 5 | from django.contrib.algoliasearch import algolia_engine 6 | from django.contrib.algoliasearch.models import AlgoliaIndexError 7 | 8 | from .models import Example 9 | 10 | 11 | class IndexTestCase(TestCase): 12 | def setUp(self): 13 | self.client = algolia_engine.client 14 | self.instance = Example(uid=4, 15 | name='SuperK', 16 | address='Finland', 17 | lat=63.3, 18 | lng=-32.0) 19 | self.instance.category = ['Shop', 'Grocery'] 20 | 21 | def test_default_index_name(self): 22 | index = AlgoliaIndex(Example, self.client) 23 | regex = r'django(\d+.\d+)?_Example_test' 24 | try: 25 | self.assertRegex(index.index_name, regex) 26 | except AttributeError: 27 | self.assertRegexpMatches(index.index_name, regex) 28 | 29 | def test_custom_index_name(self): 30 | class ExampleIndex(AlgoliaIndex): 31 | index_name = 'customName' 32 | 33 | index = ExampleIndex(Example, self.client) 34 | regex = r'django(\d+.\d+)?_customName_test' 35 | try: 36 | self.assertRegex(index.index_name, regex) 37 | except AttributeError: 38 | self.assertRegexpMatches(index.index_name, regex) 39 | 40 | def test_custom_objectID(self): 41 | class ExampleIndex(AlgoliaIndex): 42 | custom_objectID = 'uid' 43 | 44 | index = ExampleIndex(Example, self.client) 45 | obj = index._build_object(self.instance) 46 | self.assertEqual(obj['objectID'], 4) 47 | 48 | def test_invalid_custom_objectID(self): 49 | class ExampleIndex(AlgoliaIndex): 50 | custom_objectID = 'uuid' 51 | 52 | with self.assertRaises(AlgoliaIndexError): 53 | index = ExampleIndex(Example, self.client) 54 | 55 | def test_geo_fields(self): 56 | class ExampleIndex(AlgoliaIndex): 57 | geo_field = 'location' 58 | 59 | index = ExampleIndex(Example, self.client) 60 | obj = index._build_object(self.instance) 61 | self.assertEqual(obj['_geoloc'], {'lat': 63.3, 'lng': -32.0}) 62 | 63 | def test_none_geo_fields(self): 64 | class ExampleIndex(AlgoliaIndex): 65 | geo_field = 'location' 66 | 67 | Example.location = lambda x: None 68 | index = ExampleIndex(Example, self.client) 69 | obj = index._build_object(self.instance) 70 | self.assertIsNone(obj.get('_geoloc')) 71 | 72 | def test_invalid_geo_fields(self): 73 | class ExampleIndex(AlgoliaIndex): 74 | geo_field = 'position' 75 | 76 | with self.assertRaises(AlgoliaIndexError): 77 | index = ExampleIndex(Example, self.client) 78 | 79 | def test_tags(self): 80 | class ExampleIndex(AlgoliaIndex): 81 | tags = 'category' 82 | 83 | index = ExampleIndex(Example, self.client) 84 | obj = index._build_object(self.instance) 85 | self.assertListEqual(obj['_tags'], self.instance.category) 86 | 87 | def test_invalid_tags(self): 88 | class ExampleIndex(AlgoliaIndex): 89 | tags = 'categories' 90 | 91 | with self.assertRaises(AlgoliaIndexError): 92 | index = ExampleIndex(Example, self.client) 93 | 94 | def test_one_field(self): 95 | class ExampleIndex(AlgoliaIndex): 96 | fields = 'name' 97 | 98 | index = ExampleIndex(Example, self.client) 99 | obj = index._build_object(self.instance) 100 | self.assertNotIn('uid', obj) 101 | self.assertIn('name', obj) 102 | self.assertNotIn('address', obj) 103 | self.assertNotIn('lat', obj) 104 | self.assertNotIn('lng', obj) 105 | self.assertNotIn('location', obj) 106 | self.assertNotIn('category', obj) 107 | self.assertEqual(len(obj), 2) 108 | 109 | def test_multiple_fields(self): 110 | class ExampleIndex(AlgoliaIndex): 111 | fields = ('name', 'address') 112 | 113 | index = ExampleIndex(Example, self.client) 114 | obj = index._build_object(self.instance) 115 | self.assertNotIn('uid', obj) 116 | self.assertIn('name', obj) 117 | self.assertIn('address', obj) 118 | self.assertNotIn('lat', obj) 119 | self.assertNotIn('lng', obj) 120 | self.assertNotIn('location', obj) 121 | self.assertNotIn('category', obj) 122 | self.assertEqual(len(obj), 3) 123 | 124 | def test_fields_with_custom_name(self): 125 | class ExampleIndex(AlgoliaIndex): 126 | fields = { 127 | 'name': 'shopName', 128 | 'address': 'shopAddress' 129 | } 130 | 131 | index = ExampleIndex(Example, self.client) 132 | obj = index._build_object(self.instance) 133 | self.assertDictContainsSubset({ 134 | 'shopName': self.instance.name, 135 | 'shopAddress': self.instance.address 136 | }, obj) 137 | self.assertNotIn('name', obj) 138 | self.assertNotIn('address', obj) 139 | 140 | def test_invalid_fields(self): 141 | class ExampleIndex(AlgoliaIndex): 142 | fields = ('name', 'color') 143 | 144 | with self.assertRaises(AlgoliaIndexError): 145 | index = ExampleIndex(Example, self.client) 146 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Algolia Search for Django 2 | ================== 3 | 4 | This package lets you easily integrate the Algolia Search API to your [Django](https://www.djangoproject.com/) project. It's based on the [algoliasearch-client-python](https://github.com/algolia/algoliasearch-client-python) package. 5 | 6 | You might be interested in this sample Django application providing a typeahead.js based auto-completion and Google-like instant search: [algoliasearch-django-example](https://github.com/algolia/algoliasearch-django-example) 7 | 8 | Compatible with **Python 2.7**, **Python 3.2+** and **Django 1.7+** 9 | 10 | [![Build Status](https://travis-ci.org/algolia/algoliasearch-django.svg?branch=master)](https://travis-ci.org/algolia/algoliasearch-django) 11 | [![Coverage Status](https://coveralls.io/repos/algolia/algoliasearch-django/badge.svg?branch=master)](https://coveralls.io/r/algolia/algoliasearch-django) 12 | [![PyPI version](https://badge.fury.io/py/algoliasearch-django.svg?branch=master)](http://badge.fury.io/py/algoliasearch-django) 13 | 14 | Table of Content 15 | ------------- 16 | **Get started** 17 | 18 | 1. [Install](#install) 19 | 1. [Setup](#setup) 20 | 1. [Quick Start](#quick-start) 21 | 1. [Commands](#commands) 22 | 1. [Search](#search) 23 | 1. [Geo-search](#geo-search) 24 | 1. [Tags](#tags) 25 | 1. [Options](#options) 26 | 1. [Run Tests](#run-tests) 27 | 28 | 29 | Install 30 | ------------- 31 | 32 | ```sh 33 | pip install algoliasearch-django 34 | ``` 35 | 36 | Setup 37 | ------------- 38 | 39 | In your Django settings, add `django.contrib.algoliasearch` to `INSTALLED_APPS` and add these two settings: 40 | 41 | ```python 42 | ALGOLIA = { 43 | 'APPLICATION_ID': 'MyAppID', 44 | 'API_KEY': 'MyApiKey' 45 | } 46 | ``` 47 | 48 | There are two optional settings: 49 | 50 | * `INDEX_PREFIX`: prefix all indexes. Use it to separate different applications, like `site1_Products` and `site2_Products`. 51 | * `INDEX_SUFFIX`: suffix all indexes. Use it to differenciate development and production environment, like `Location_dev` and `Location_prod`. 52 | * `AUTO_INDEXING`: automatically synchronize the models with Algolia (default to **True**). 53 | 54 | Quick Start 55 | ------------- 56 | 57 | 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](https://docs.djangoproject.com/en/1.8/ref/applications/) 58 | 59 | ```python 60 | from django.apps import AppConfig 61 | from django.contrib import algoliasearch 62 | 63 | class YourAppConfig(AppConfig): 64 | name = 'your_app' 65 | 66 | def ready(self): 67 | YourModel = self.get_model('your_model') 68 | algoliasearch.register(YourModel) 69 | ``` 70 | 71 | And then, don't forget the line below in the `__init__.py` file of your Django application. 72 | 73 | ```python 74 | default_app_config = 'your_django_app.apps.YourAppConfig' 75 | ``` 76 | 77 | 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`. 78 | 79 | ```python 80 | from django.contrib.algoliasearch import AlgoliaIndex 81 | 82 | class YourModelIndex(AlgoliaIndex): 83 | fields = ('name', 'date') 84 | geo_field = 'location' 85 | settings = {'attributesToIndex': ['name']} 86 | index_name = 'my_index' 87 | ``` 88 | 89 | And then replace `algoliasearch.register(YourModel)` with `algoliasearch.register(YourModel, YourModelIndex)`. 90 | 91 | ## Commands 92 | 93 | * `python manage.py algolia_reindex`: reindex all the registered models. This command will first send all the record to a temporary index and then moves it. 94 | * `python manage.py algolia_applysettings`: (re)apply the index settings. 95 | * `python manage.py algolia_clearindex`: clear the index 96 | 97 | ## Search 98 | 99 | We recommend the usage of our [JavaScript API Client](https://github.com/algolia/algoliasearch-client-js) to perform queries directly from the end-user browser without going through your server. 100 | 101 | However, if you want to search from your backend you can use the `raw_search(YourModel, 'yourQuery', params)` method. It retrieves the raw JSON answer from the API. 102 | 103 | ```python 104 | from django.contrib.algoliasearch import raw_search 105 | 106 | params = { "hitsPerPage": 5 } 107 | raw_search(Contact, "jim", params) 108 | ``` 109 | 110 | ## Geo-Search 111 | 112 | Use the `geo_field` attribute to localize your record. `geo_field` should be a callable that returns a tuple (latitude, longitude). 113 | 114 | ```python 115 | class Contact(models.model): 116 | name = models.CharField(max_lenght=20) 117 | lat = models.FloatField() 118 | lng = models.FloatField() 119 | 120 | def location(self): 121 | return (self.lat, self.lng) 122 | 123 | 124 | class ContactIndex(AlgoliaIndex): 125 | fields = 'name' 126 | geo_field = 'location' 127 | 128 | 129 | algoliasearch.register(Contact, ContactIndex) 130 | ``` 131 | 132 | # Tags 133 | 134 | Use the `tags` attributes to add tags to your record. It can be a field or a callable. 135 | 136 | ```python 137 | class ArticleIndex(AlgoliaIndex): 138 | tags = 'category' 139 | ``` 140 | 141 | At query time, specify `{ tagFilters: 'tagvalue' }` or `{ tagFilters: ['tagvalue1', 'tagvalue2'] }` as search parameters to restrict the result set to specific tags. 142 | 143 | # Options 144 | 145 | ## Custom `objectID` 146 | 147 | You can choose which field will be used as the `objectID `. The field should be unique and can be a string or integer. By default, we use the `pk` field of the model. 148 | 149 | ```python 150 | class ArticleIndex(AlgoliaIndex): 151 | custom_objectID = 'post_id' 152 | ``` 153 | 154 | ## Custom index name 155 | 156 | You can customize the index name. By default, the index name will be the name of the model class. 157 | 158 | ```python 159 | class ContactIndex(algoliaindex): 160 | index_name = 'Entreprise' 161 | ``` 162 | 163 | ## Index settings 164 | 165 | We provide many ways to configure your index allowing you to tune your overall index relevancy. All the configuration is explained on [our website](https://www.algolia.com/doc/python#Settings). 166 | 167 | ```python 168 | class ArticleIndex(AlgoliaIndex): 169 | settings = { 170 | 'attributesToIndex': ['name', 'description', 'url'], 171 | 'customRanking': ['desc(vote_count)', 'asc(name)'] 172 | } 173 | ``` 174 | 175 | ## Restrict indexing to a subset of your data 176 | 177 | You can add constraints controlling if a record must be indexed or not. `should_index` should be a callable that returns a boolean. 178 | 179 | ```python 180 | class Contact(models.model): 181 | name = models.CharField(max_lenght=20) 182 | age = models.IntegerField() 183 | 184 | def is_adult(self): 185 | return (self.age >= 18) 186 | 187 | class ContactIndex(AlgoliaIndex): 188 | should_index = 'is_adult' 189 | ``` 190 | 191 | Run Tests 192 | ------------- 193 | 194 | To run the tests, first find your Algolia application id and Admin API key (found on the Credentials page). 195 | 196 | ```shell 197 | ALGOLIA_APPLICATION_ID={APPLICATION_ID} ALGOLIA_API_KEY={ADMIN_API_KEY} tox 198 | ``` 199 | -------------------------------------------------------------------------------- /src/models.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | from django.conf import settings 3 | import logging 4 | 5 | logger = logging.getLogger(__name__) 6 | 7 | 8 | class AlgoliaIndexError(Exception): 9 | '''Something went wrong with an Algolia Index.''' 10 | 11 | 12 | class AlgoliaIndex(object): 13 | '''An index in the Algolia backend.''' 14 | 15 | # Use to specify a custom field that will be used for the objectID. 16 | # This field should be unique. 17 | custom_objectID = None 18 | 19 | # Use to specify the fields that should be included in the index. 20 | fields = () 21 | 22 | # Use to specify the geo-fields that should be used for location search. 23 | # The attribute should be a callable that returns a tuple. 24 | geo_field = None 25 | 26 | # Use to specify the field that should be used for filtering by tag. 27 | tags = None 28 | 29 | # Use to specify the index to target on Algolia. 30 | index_name = None 31 | 32 | # Use to specify the settings of the index. 33 | settings = {} 34 | 35 | # Use to specify a callable that say if the instance should be indexed. 36 | # The attribute should be a callable that returns a boolean. 37 | should_index = None 38 | 39 | # Instance of the index from algoliasearch client 40 | __index = None 41 | 42 | def __init__(self, model, client): 43 | '''Initializes the index.''' 44 | self.model = model 45 | self.__client = client 46 | self.__set_index(client) 47 | 48 | try: 49 | all_fields = [f.name for f in model._meta.get_fields()] 50 | except AttributeError: # Django 1.7 51 | all_fields = model._meta.get_all_field_names() 52 | 53 | # Avoid error when there is only one field to index 54 | if isinstance(self.fields, str): 55 | self.fields = (self.fields, ) 56 | 57 | # Check fields 58 | for field in self.fields: 59 | if not (hasattr(model, field) or (field in all_fields)): 60 | raise AlgoliaIndexError( 61 | '{} is not an attribute of {}.'.format(field, model)) 62 | 63 | # If no fields are specified, index all the fields of the model 64 | if not self.fields: 65 | self.fields = all_fields 66 | self.fields.remove('id') 67 | 68 | # Check custom_objectID 69 | if self.custom_objectID: 70 | if not (hasattr(model, self.custom_objectID) or 71 | (self.custom_objectID in all_fields)): 72 | raise AlgoliaIndexError('{} is not an attribute of {}.'.format( 73 | self.custom_objectID, model)) 74 | 75 | # Check tags 76 | if self.tags: 77 | if not (hasattr(model, self.tags) or (self.tags in all_fields)): 78 | raise AlgoliaIndexError('{} is not an attribute of {}'.format( 79 | self.tags, model)) 80 | 81 | # Check geo_field + get the callable 82 | if self.geo_field: 83 | if hasattr(model, self.geo_field): 84 | attr = getattr(model, self.geo_field) 85 | if callable(attr): 86 | self.geo_field = attr 87 | else: 88 | raise AlgoliaIndexError('{} should be a callable.'.format( 89 | self.geo_field)) 90 | else: 91 | raise AlgoliaIndexError('{} is not an attribute of {}.'.format( 92 | self.geo_field, model)) 93 | 94 | # Check should_index + get the callable 95 | if self.should_index: 96 | if hasattr(model, self.should_index): 97 | attr = getattr(model, self.should_index) 98 | if callable(attr): 99 | self.should_index = attr 100 | else: 101 | raise AlgoliaIndexError('{} should be a callable.'.format( 102 | self.should_index)) 103 | else: 104 | raise AlgoliaIndexError('{} is not an attribute of {}.'.format( 105 | self.should_index, model)) 106 | 107 | def __set_index(self, client): 108 | '''Get an instance of Algolia Index''' 109 | params = getattr(settings, 'ALGOLIA', None) 110 | 111 | if not self.index_name: 112 | self.index_name = self.model.__name__ 113 | 114 | if params: 115 | if 'INDEX_PREFIX' in params: 116 | self.index_name = params['INDEX_PREFIX'] + '_' + self.index_name 117 | if 'INDEX_SUFFIX' in params: 118 | self.index_name += '_' + params['INDEX_SUFFIX'] 119 | else: 120 | # @Deprecated: 1.1.0 121 | if hasattr(settings, 'ALGOLIA_INDEX_PREFIX'): 122 | self.index_name = settings.ALGOLIA_INDEX_PREFIX + '_' + self.index_name 123 | if hasattr(settings, 'ALGOLIA_INDEX_SUFFIX'): 124 | self.index_name += '_' + settings.ALGOLIA_INDEX_SUFFIX 125 | 126 | self.__index = client.init_index(self.index_name) 127 | self.__tmp_index = client.init_index(self.index_name + '_tmp') 128 | 129 | def __get_objectID(self, instance): 130 | '''Return the objectID of an instance.''' 131 | if self.custom_objectID: 132 | attr = getattr(instance, self.custom_objectID) 133 | if callable(attr): 134 | attr = attr() 135 | return attr 136 | else: 137 | return instance.pk 138 | 139 | def _build_object(self, instance): 140 | '''Build the JSON object.''' 141 | tmp = {'objectID': self.__get_objectID(instance)} 142 | if isinstance(self.fields, dict): 143 | for key, value in self.fields.items(): 144 | attr = getattr(instance, key) 145 | if callable(attr): 146 | attr = attr() 147 | tmp[value] = attr 148 | else: 149 | for field in self.fields: 150 | attr = getattr(instance, field) 151 | if callable(attr): 152 | attr = attr() 153 | tmp[field] = attr 154 | 155 | if self.geo_field: 156 | loc = self.geo_field(instance) 157 | 158 | if loc: 159 | tmp['_geoloc'] = {'lat': loc[0], 'lng': loc[1]} 160 | 161 | if self.tags: 162 | attr = getattr(instance, self.tags) 163 | if callable(attr): 164 | attr = attr() 165 | if not isinstance(attr, list): 166 | attr = list(attr) 167 | tmp['_tags'] = attr 168 | 169 | logger.debug('BUILD %s FROM %s', tmp['objectID'], self.model) 170 | return tmp 171 | 172 | def update_obj_index(self, instance): 173 | '''Update the object.''' 174 | if self.should_index: 175 | if not self.should_index(instance): 176 | # Should not index, but since we don't now the state of the 177 | # instance, we need to send a DELETE request to ensure that if 178 | # the instance was previously indexed, it will be removed. 179 | self.delete_obj_index(instance) 180 | return 181 | 182 | obj = self._build_object(instance) 183 | self.__index.save_object(obj) 184 | logger.debug('UPDATE %s FROM %s', obj['objectID'], self.model) 185 | 186 | def delete_obj_index(self, instance): 187 | '''Delete the object.''' 188 | objectID = self.__get_objectID(instance) 189 | self.__index.delete_object(objectID) 190 | logger.debug('DELETE %s FROM %s', objectID, self.model) 191 | 192 | def raw_search(self, query='', params={}): 193 | '''Return the raw JSON.''' 194 | return self.__index.search(query, params) 195 | 196 | def set_settings(self): 197 | '''Apply the settings to the index.''' 198 | if self.settings: 199 | self.__index.set_settings(self.settings) 200 | logger.debug('APPLY SETTINGS ON %s', self.index_name) 201 | 202 | def clear_index(self): 203 | '''Clear the index.''' 204 | self.__index.clear_index() 205 | logger.debug('CLEAR INDEX %s', self.index_name) 206 | 207 | def reindex_all(self, batch_size=1000): 208 | '''Reindex all records.''' 209 | if self.settings: 210 | self.__tmp_index.set_settings(self.settings) 211 | logger.debug('APPLY SETTINGS ON %s_tmp', self.index_name) 212 | self.__tmp_index.clear_index() 213 | logger.debug('CLEAR INDEX %s_tmp', self.index_name) 214 | 215 | result = None 216 | counts = 0 217 | batch = [] 218 | 219 | if hasattr(self, 'get_queryset'): 220 | qs = self.get_queryset() 221 | else: 222 | qs = self.model.objects.all() 223 | 224 | for instance in qs: 225 | if self.should_index: 226 | if not self.should_index(instance): 227 | continue # should not index 228 | 229 | batch.append(self._build_object(instance)) 230 | if len(batch) >= batch_size: 231 | result = self.__tmp_index.save_objects(batch) 232 | logger.info('SAVE %d OBJECTS TO %s_tmp', len(batch), 233 | self.index_name) 234 | batch = [] 235 | counts += 1 236 | if len(batch) > 0: 237 | result = self.__tmp_index.save_objects(batch) 238 | logger.info('SAVE %d OBJECTS TO %s_tmp', len(batch), 239 | self.index_name) 240 | if result: 241 | self.__client.move_index(self.index_name + '_tmp', self.index_name) 242 | logger.info('MOVE INDEX %s_tmp TO %s', self.index_name, 243 | self.index_name) 244 | return counts 245 | --------------------------------------------------------------------------------