Python haystack.connections() Examples

The following are 30 code examples of haystack.connections(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module haystack , or try the search function .
Example #1
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #2
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #3
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #4
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #5
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #6
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #7
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #8
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #9
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #10
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #11
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #12
Source File: conftest.py    From course-discovery with GNU Affero General Public License v3.0 6 votes vote down vote up
def haystack_default_connection(haystack_add_xdist_suffix_to_index_name):  # pylint: disable=redefined-outer-name,unused-argument
    skip_if_no_django()

    backend = haystack_connections['default'].get_backend()

    # Force Haystack to update the mapping for the index
    backend.setup_complete = False

    es = backend.conn
    index_name = backend.index_name
    ElasticsearchUtils.delete_index(es, index_name)
    ElasticsearchUtils.create_alias_and_index(es, index_name)
    ElasticsearchUtils.refresh_index(es, index_name)

    yield backend

    ElasticsearchUtils.delete_index(es, index_name) 
Example #13
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #14
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #15
Source File: multilingual_whoosh_backend.py    From Django-2-Web-Development-Cookbook-Third-Edition with MIT License 6 votes vote down vote up
def update(self, index, iterable,
               commit=True,
               language_specific=False):
        if not language_specific \
                and self.connection_alias == "default":
            current_language = (translation.get_language()
                                or settings.LANGUAGE_CODE)[:2]
            for lang_code, lang_name in settings.LANGUAGES:
                using = "default_%s" % lang_code
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit,
                               language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #16
Source File: backends.py    From linkedevents with MIT License 6 votes vote down vote up
def forward_to_backends(self, method, *args, **kwargs):
        # forwards the desired backend method to all the language backends
        initial_language = translation.get_language()
        # 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()
            getattr(backend.parent_class, method)(backend, *args, **kwargs)

        if initial_language is not None:
            translation.activate(initial_language)
        else:
            translation.deactivate() 
Example #17
Source File: multilingual_whoosh_backend.py    From Django-3-Web-Development-Cookbook-Fourth-Edition with MIT License 5 votes vote down vote up
def update(self, index, iterable, commit=True, language_specific=False):
        if not language_specific and self.connection_alias == "default":
            current_language = (translation.get_language() or settings.LANGUAGE_CODE)[
                :2
            ]
            for lang_code, lang_name in settings.LANGUAGES:
                lang_code_underscored = lang_code.replace("-", "_")
                using = f"default_{lang_code_underscored}"
                translation.activate(lang_code)
                backend = connections[using].get_backend()
                backend.update(index, iterable, commit, language_specific=True)
            translation.activate(current_language)
        elif language_specific:
            super().update(index, iterable, commit) 
Example #18
Source File: test_hooks.py    From django-multilingual-search with MIT License 5 votes vote down vote up
def test_post_save_and_delete_hook(self, es_obj):
        # index uses a global loader for the backend.
        documents = create_documents()
        doc = documents[0]
        doc.save()
        # send the post_save signal
        index = haystack.connections['default'].get_unified_index().get_index(Document)
        backend = index._get_backend('default')  # Multilingual Backend
        # test if the command has been sent to ES
        if not isinstance(backend.conn, mock.Mock):
            backend.conn = mock.MagicMock()
        es = backend.conn
        index.update_object(doc.object, 'default')

        self.assertFalse(es.delete.called)
        self.assertTrue(es.bulk.called)
        call_args = es.bulk.call_args_list
        self.assertEqual(len(call_args), len(settings.LANGUAGES))
        self.assertEqual(call_args[0][0][0][0], {'index': {'_id': 'testproject.document.1'}})
        index_list = [a[1] for a in call_args]
        self.assertIn({'doc_type': 'modelresult', 'index': 'testproject-en'}, index_list)
        self.assertIn({'doc_type': 'modelresult', 'index': 'testproject-de'}, index_list)
        self.assertIn({'doc_type': 'modelresult', 'index': 'testproject-es'}, index_list)
        self.assertIn(doc.object.text_en, call_args[0][0][0][1]['text'])
        # test delete
        index.remove_object(doc.object, 'default')

        self.assertTrue(es.delete.called)
        call_args = es.delete.call_args_list
        self.assertEqual(len(call_args), len(settings.LANGUAGES))
        self.assertEqual(call_args[0][1],
                         {'index': 'testproject-en', 'ignore': 404,
                          'doc_type': 'modelresult', 'id': 'testproject.document.1'})
        self.assertEqual(call_args[1][1],
                         {'id': 'testproject.document.1', 'doc_type': 'modelresult',
                         'ignore': 404, 'index': 'testproject-de'}) 
Example #19
Source File: test_hooks.py    From django-multilingual-search with MIT License 5 votes vote down vote up
def test_hook_called(self, mock_delete, mock_save, mock_es):
        # check if the signals are triggered
        rsp = RealtimeSignalProcessor(haystack.connections, haystack.connection_router)  # noqa
        self.assertFalse(mock_delete.called)
        self.assertFalse(mock_save.called)
        documents = create_documents()
        doc = documents[0]
        doc.save()
        self.assertTrue(mock_save.called)
        doc.object.delete()
        self.assertTrue(mock_delete.called) 
Example #20
Source File: test_hooks.py    From django-multilingual-search with MIT License 5 votes vote down vote up
def setUp(self):
        # Haystack stores the connection in a global variable. This has to be reset
        # because otherwise the mock is still refrenced (Django < 1.8).
        haystack.connections.reload('default')

        self.sp = DocumentOnlySignalProcessor(haystack.connections,
                                              haystack.connection_router) 
Example #21
Source File: utils.py    From mapstory with GNU General Public License v3.0 5 votes vote down vote up
def update_es_index(sender, instance):
    """
    Manually update an elastic search index.
    """
    b = BaseSignalProcessor(haystack.connections, haystack.connection_router)
    b.handle_save(sender, instance) 
Example #22
Source File: remove_unused_indexes.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def handle(self, *_args, **options):
        self.backends = options.get('using')
        if not self.backends:
            self.backends = list(haystack_connections.connections_info.keys())

        for backend_name in self.backends:
            connection = haystack_connections[backend_name]
            backend = connection.get_backend()
            indices_client = backend.conn.indices
            current_alias_name = backend.index_name
            self.remove_unused_indexes(indices_client=indices_client, current_alias_name=current_alias_name) 
Example #23
Source File: backends.py    From linkedevents with MIT License 5 votes vote down vote up
def get_query(self):
        language = translation.get_language()
        if not language:
            language = settings.LANGUAGES[0][0][:2]
        else:
            language = language[:2]
        using = '%s-%s' % (self.using, language)
        return connections[using].get_query() 
Example #24
Source File: 0049_filter_future_dates.py    From dissemin with GNU Affero General Public License v3.0 5 votes vote down vote up
def remove_from_index(paper):
    """
    Remove this paper from Haystack's index
    (to be called before deleting the paper for real)
    """
    using_backends = haystack.connection_router.for_write(instance=paper)
    for using in using_backends:
        try:
            index = haystack.connections[using].get_unified_index(
                                    ).get_index(Paper)
            index.remove_object(paper, using=using)
        except haystack.exceptions.NotHandled:
            pass 
Example #25
Source File: 0058_correct_crossref_pub_types.py    From dissemin with GNU Affero General Public License v3.0 5 votes vote down vote up
def update_crossref_pubtypes(apps, schema_editor):
    """
    This data migrations goes through all OaiRecords with source CrossRef and invalid publication type.
    It adjusts the publication type in the OaiRecord and the corresponding paper and updates the index.
    We do not use the most sober way with using apps given to this function.
    But since we do not alter DB this fine.
    The step is necessary, since updating the index requires a full object, not just the DB representation.
    """
    # Only source CrossRef
    c = OaiSource.objects.get(identifier='crossref')
    # Our valid publication types as list
    valid_pubtypes = [key for (key, value) in PAPER_TYPE_CHOICES]

    # As long as there are malicious entries, correct them in 1000er batches
    while OaiRecord.objects.filter(source=c).exclude(pubtype__in=valid_pubtypes).exists():
        for o in OaiRecord.objects.filter(source=c).exclude(pubtype__in=valid_pubtypes).select_related('about')[:1000]:
            new_pubtype = CITEPROC_PUBTYPE_TRANSLATION.get(o.pubtype, 'other')
            print('Change pubtype for OaiRecord {} from {} to {}'.format(o.id, o.pubtype, new_pubtype))
            o.pubtype = new_pubtype
            o.save()
            p = o.about
            p.doctype = o.pubtype
            p.save()
            # Objects in DB are no fine
            # Let's update the index
            using_backends = haystack.connection_router.for_write(instance=p)
            for using in using_backends:
                try:
                    index = haystack.connections[using].get_unified_index(
                                            ).get_index(Paper)
                    index.update_object(p, using=using)
                except haystack.exceptions.NotHandled as e:
                    print(e)
        print('Done with 1000 OaiRecords') 
Example #26
Source File: models.py    From dissemin with GNU Affero General Public License v3.0 5 votes vote down vote up
def remove_from_index(self):
        """
        Remove this paper from Haystack's index
        (to be called before deleting the paper for real)
        """
        using_backends = haystack.connection_router.for_write(instance=self)
        for using in using_backends:
            try:
                index = haystack.connections[using].get_unified_index(
                                        ).get_index(Paper)
                index.remove_object(self, using=using)
            except haystack.exceptions.NotHandled:
                pass 
Example #27
Source File: models.py    From dissemin with GNU Affero General Public License v3.0 5 votes vote down vote up
def update_index(self):
        """
        Updates Haystack's index for this paper
        """
        using_backends = haystack.connection_router.for_write(instance=self)
        for using in using_backends:
            try:
                index = haystack.connections[using].get_unified_index(
                                        ).get_index(Paper)
                index.update_object(self, using=using)
            except haystack.exceptions.NotHandled:
                pass

# Rough data extracted through OAI-PMH 
Example #28
Source File: tests.py    From dissemin with GNU Affero General Public License v3.0 5 votes vote down vote up
def haystack_index(self, django_db_blocker):
        with django_db_blocker.unblock():
            self.r3.institution = self.i
            self.r3.department = self.d
            self.r3.save()
            haystack.connections.reload('default')
            call_command('update_index', verbosity=0)
            yield
            haystack.connections['default'].get_backend().clear() 
Example #29
Source File: whoosh_cn_backend.py    From thirtylol with MIT License 5 votes vote down vote up
def setup(self):
        """
        Defers loading until needed.
        """
        from haystack import connections
        new_index = False

        # Make sure the index is there.
        if self.use_file_storage and not os.path.exists(self.path):
            os.makedirs(self.path)
            new_index = True

        if self.use_file_storage and not os.access(self.path, os.W_OK):
            raise IOError("The path to your Whoosh index '%s' is not writable for the current user/group." % self.path)

        if self.use_file_storage:
            self.storage = FileStorage(self.path)
        else:
            global LOCALS

            if LOCALS.RAM_STORE is None:
                LOCALS.RAM_STORE = RamStorage()

            self.storage = LOCALS.RAM_STORE

        self.content_field_name, self.schema = self.build_schema(connections[self.connection_alias].get_unified_index().all_searchfields())
        self.parser = QueryParser(self.content_field_name, schema=self.schema)

        if new_index is True:
            self.index = self.storage.create_index(self.schema)
        else:
            try:
                self.index = self.storage.open_index(schema=self.schema)
            except index.EmptyIndexError:
                self.index = self.storage.create_index(self.schema)

        self.setup_complete = True 
Example #30
Source File: whoosh_cn_backend.py    From Django-blog with MIT License 5 votes vote down vote up
def setup(self):
        """
        Defers loading until needed.
        """
        from haystack import connections
        new_index = False

        # Make sure the index is there.
        if self.use_file_storage and not os.path.exists(self.path):
            os.makedirs(self.path)
            new_index = True

        if self.use_file_storage and not os.access(self.path, os.W_OK):
            raise IOError("The path to your Whoosh index '%s' is not writable for the current user/group." % self.path)

        if self.use_file_storage:
            self.storage = FileStorage(self.path)
        else:
            global LOCALS

            if getattr(LOCALS, 'RAM_STORE', None) is None:
                LOCALS.RAM_STORE = RamStorage()

            self.storage = LOCALS.RAM_STORE

        self.content_field_name, self.schema = self.build_schema(
            connections[self.connection_alias].get_unified_index().all_searchfields())
        self.parser = QueryParser(self.content_field_name, schema=self.schema)

        if new_index is True:
            self.index = self.storage.create_index(self.schema)
        else:
            try:
                self.index = self.storage.open_index(schema=self.schema)
            except index.EmptyIndexError:
                self.index = self.storage.create_index(self.schema)

        self.setup_complete = True