algoliasearch-django
algoliasearch-django copied to clipboard
Support Multiple Algolia applications in one Django project
Description
Support indexing models in multiple applications with different APPLICATION_ID and API_KEY settings on a per-model basis while preserving the native register and hooks integration.
- Django version: 1.11
- Algolia Django integration version: 2.0.0
- Algolia Client Version: 2.6.1
- Language Version: Python 3.9
How this could work
Subclass AlgoliaEngine or modify the @register decorator. Not really show how the implementation details would work but here is some pseudocode to convey the general idea.
class ScholarshipAlgoliaEngine(AlgoliaEngine)
scholarship_algolia_settings = {
**settings.ALGOLIA,
'APPLICATION_ID': env['SCHOLARSHIP_ALGOLIA_APPLICATION_ID'],
'API_KEY': env['SCHOLARSHIP_ALGOLIA_API_KEY'],
}
def __init__(self, settings=scholarship_algolia_settings, *args, **kwargs):
super().__init__(self, settings=settings, *args, **kwargs) # args and kwargs not needed but maybe for backwards-compatibility?
@register(Scholarship, ScholarshipAlgoliaEngine)
class ScholarshipIndex(AlgoliaIndex):
pass
# OR
@register(Scholarship)
class ScholarshipIndex(AlgoliaIndex, ScholarshipAlgoliaEngine):
pass
# OR
custom_engine = ScholarshipAlgoliaEngine()
@custom_engine.register(Scholarship)
class ScholarshipIndex(AlgoliaIndex):
pass
Sources
Based on registration.py:
https://github.com/algolia/algoliasearch-django/blob/e192aec87ce906da6e853fcb05ce431b8d4f8440/algoliasearch_django/registration.py#L28-L33
and Algolia engine:
https://github.com/algolia/algoliasearch-django/blob/e192aec87ce906da6e853fcb05ce431b8d4f8440/algoliasearch_django/init.py#L16-L22
Another idea for how to implement this
@register(Scholarship)
class ScholarshipIndex(AlgoliaIndex):
APPLICATION_ID = env['SCHOLARSHIP_ALGOLIA_APPLICATION_ID'] # <-- new field
API_KEY = env['SCHOLARSHIP_ALGOLIA_API_KEY'] # <-- new field
fields = SCHOLARSHIP_FIELDS_FOR_INDEXING
settings = {'searchableAttributes': SCHOLARSHIP_FIELDS_FOR_SEARCHING,
'customRanking': [
'desc(deadline_score)',
]}
index_name = 'scholarship_index'
should_index = 'can_be_indexed'
Any models that don't have APPLICATION_ID or API_KEY provided would use the default settings in settings.py:
@register(Scholarship)
class ScholarshipIndex(AlgoliaIndex):
fields = SCHOLARSHIP_FIELDS_FOR_INDEXING
settings = {'searchableAttributes': SCHOLARSHIP_FIELDS_FOR_SEARCHING,
'customRanking': [
'desc(deadline_score)',
]}
index_name = 'scholarship_index'
should_index = 'can_be_indexed'