# based on http://anthony-tresontani.github.io/Django/2012/09/20/multilingual-search/
import re
from django.conf import settings
from django.utils import translation
from haystack import connections
from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery
from haystack.constants import DEFAULT_ALIAS
from haystack.utils.loading import load_backend


def get_using(language, alias=DEFAULT_ALIAS):
    new_using = alias + "-" + language
    using = new_using if new_using in settings.HAYSTACK_CONNECTIONS else alias
    return using

class MultilingualSearchBackend(BaseSearchBackend):
    def update(self, index, iterable, commit=True):
        initial_language = translation.get_language()[:2]
        # retrieve unique backend name
        backends = []
        for language, _ in settings.LANGUAGES:
            using = '%s-%s' % (self.connection_alias, language)
            # Ensure each backend is called only once
            if using in backends:
                continue
            else:
                backends.append(using)
            translation.activate(language)
            backend = connections[using].get_backend()
            backend.parent_class.update(backend, index, iterable, commit)

        translation.activate(initial_language)
    def clear(self, **kwargs):
        return

class MultilingualSearchQuery(BaseSearchQuery):
    def __init__(self, using=DEFAULT_ALIAS):
        language = translation.get_language()[:2]
        using = get_using(language)
        super(MultilingualSearchQuery, self).__init__(using=using)

class LanguageSearchBackend(BaseSearchBackend):
    def update(self, *args, **kwargs):
        # Handle all updates through the main Multilingual object.
        return

class LanguageSearchQuery(BaseSearchQuery):
    def __init__(self, **kwargs):
        pass

class LanguageSearchEngine(BaseEngine):
    def __init__(self, **kwargs):
        conn_config = settings.HAYSTACK_CONNECTIONS[kwargs['using']]
        base_engine = load_backend(conn_config['BASE_ENGINE'])(**kwargs)

        backend_bases = (LanguageSearchBackend, base_engine.backend)
        backend_class = type('LanguageSearchBackend', backend_bases,
                             {'parent_class': base_engine.backend})
        self.backend = backend_class

        query_bases = (LanguageSearchQuery, base_engine.query)
        query_class = type('LanguageSearchQuery', query_bases,
                           {'parent_class': base_engine.query})
        self.query = query_class

        super(LanguageSearchEngine, self).__init__(**kwargs)


class MultilingualSearchEngine(BaseEngine):
    backend = MultilingualSearchBackend
    query = MultilingualSearchQuery

@@ -368,190 +368,8 @@
 d%0A%0A%0A
-def get_using(language, alias=DEFAULT_ALIAS):%0A    new_using = alias + %22-%22 + language%0A    using = new_using if new_using in settings.HAYSTACK_CONNECTIONS else alias%0A    return using%0A%0A
 clas
@@ -1131,24 +1131,25 @@
     return%0A%0A
+#
 class Multil
@@ -1176,32 +1176,33 @@
 seSearchQuery):%0A
+#
     def __init__
@@ -1229,16 +1229,164 @@
 _ALIAS):
+%0A%0Aclass MultilingualSearchEngine(BaseEngine):%0A    backend = MultilingualSearchBackend%0A    #query = MultilingualSearchQuery%0A%0A    def get_query(self):
 %0A       
@@ -1448,18 +1448,31 @@
 g = 
-get_
+'%25s-%25s' %25 (self.
 using
-(
+, 
 lang
@@ -1489,65 +1489,44 @@
     
-super(MultilingualSearchQuery, self).__init__(using=using
+return connections%5Busing%5D.get_query(
 )%0A%0Ac
@@ -1742,46 +1742,8 @@
 y):%0A
-    def __init__(self, **kwargs):%0A    
     
@@ -2221,403 +2221,97 @@
     
-query_bases = (LanguageSearchQuery, base_engine.query)%0A        query_class = type('LanguageSearchQuery', query_bases,%0A                           %7B'parent_class': base_engine.query%7D)%0A        self.query = query_class%0A%0A        super(LanguageSearchEngine, self).__init__(**kwargs)%0A%0A%0Aclass MultilingualSearchEngine(BaseEngine):%0A    backend = MultilingualSearchBackend%0A    query = MultilingualSearchQuery
+self.query = base_engine.query%0A%0A        super(LanguageSearchEngine, self).__init__(**kwargs)
 %0A

Fix bugs with multilingual search queries