Skip to content

Support Multiple Algolia applications in one Django project #323

@ademidun

Description

@ademidun

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:

class AlgoliaEngine(object):
def __init__(self, settings=SETTINGS):
"""Initializes the Algolia engine."""
try:
app_id = settings['APPLICATION_ID']

and Algolia engine:

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions