-
Notifications
You must be signed in to change notification settings - Fork 62
Open
Description
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:
algoliasearch-django/algoliasearch_django/registration.py
Lines 28 to 33 in e192aec
class AlgoliaEngine(object): | |
def __init__(self, settings=SETTINGS): | |
"""Initializes the Algolia engine.""" | |
try: | |
app_id = settings['APPLICATION_ID'] |
and Algolia engine:
algoliasearch-django/algoliasearch_django/__init__.py
Lines 16 to 22 in e192aec
AlgoliaIndex = models.AlgoliaIndex | |
AlgoliaEngine = registration.AlgoliaEngine | |
algolia_engine = registration.algolia_engine | |
# Algolia Engine functions | |
register = algolia_engine.register |
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'
Metadata
Metadata
Assignees
Labels
No labels