diff --git a/src/open_inwoner/search/utils.py b/src/open_inwoner/search/utils.py index df6e6541da..a382b41eb7 100644 --- a/src/open_inwoner/search/utils.py +++ b/src/open_inwoner/search/utils.py @@ -1,6 +1,7 @@ import logging from django.db.utils import DatabaseError +from django.utils.functional import lazy from .models import Synonym @@ -8,12 +9,17 @@ def load_synonyms() -> list: - """used for ES""" - # todo should be refactored to be run after django migrations - try: - synonyms = [s.synonym_line() for s in Synonym.objects.all()] - except DatabaseError as exc: - logger.warning(f"Synonyms for elasticsearch were not loaded: {exc}") - return [] - - return synonyms + """ + Lazy load synonyms for Elasticsearch + """ + + def _load_synonyms(): + try: + synonyms = [s.synonym_line() for s in Synonym.objects.all()] + except DatabaseError as exc: + logger.warning(f"Synonyms for elasticsearch were not loaded: {exc}") + return [] + + return synonyms + + return lazy(_load_synonyms)