Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Multiple Algolia applications in one Django project #323

Open
ademidun opened this issue Apr 9, 2022 · 0 comments
Open

Support Multiple Algolia applications in one Django project #323

ademidun opened this issue Apr 9, 2022 · 0 comments

Comments

@ademidun
Copy link

ademidun commented Apr 9, 2022

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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant