def tearDown(self): import haystack # Restore. settings.HAYSTACK_CONNECTIONS['default']['URL'] = self.old_es_url connections['default']._index = self.old_ui logging.getLogger('haystack').removeHandler(self.cap) logging.getLogger('haystack').addHandler(haystack.stream)
def setUp(self): self.sample_objs = [] for i in xrange(1, 4): mock = MockModel() mock.id = i mock.author = 'daniel%s' % i mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i) self.sample_objs.append(mock) # Stow. # Point the backend at a URL that doesn't exist so we can watch the # sparks fly. self.old_es_url = settings.HAYSTACK_CONNECTIONS['default']['URL'] settings.HAYSTACK_CONNECTIONS['default']['URL'] = "%s/foo/" % self.old_es_url self.cap = CaptureHandler() logging.getLogger('haystack').addHandler(self.cap) import haystack logging.getLogger('haystack').removeHandler(haystack.stream) # Setup the rest of the bits. self.old_ui = connections['default'].get_unified_index() ui = UnifiedIndex() self.smmi = ElasticsearchMockSearchIndex() ui.build(indexes=[self.smmi]) connections['default']._index = ui self.sb = connections['default'].get_backend()
def __init__(self, connection_alias, **connection_options): super(ElasticsearchSearchBackend, self).__init__( connection_alias, **connection_options ) if "URL" not in connection_options: raise ImproperlyConfigured( "You must specify a 'URL' in your settings for connection '%s'." % connection_alias ) if "INDEX_NAME" not in connection_options: raise ImproperlyConfigured( "You must specify a 'INDEX_NAME' in your settings for connection '%s'." % connection_alias ) self.conn = elasticsearch.Elasticsearch( connection_options["URL"], timeout=self.timeout, **connection_options.get("KWARGS", {}) ) self.index_name = connection_options["INDEX_NAME"] self.log = logging.getLogger("haystack") self.setup_complete = False self.existing_mapping = {}
def setup(): log = logging.getLogger("haystack") try: import elasticsearch if not ((1, 0, 0) <= elasticsearch.__version__ < (2, 0, 0)): raise ImportError from elasticsearch import Elasticsearch, ElasticsearchException except ImportError: log.error( "Skipping ElasticSearch 1 tests: 'elasticsearch>=1.0.0,<2.0.0' not installed." ) raise unittest.SkipTest("'elasticsearch>=1.0.0,<2.0.0' not installed.") es = Elasticsearch(settings.HAYSTACK_CONNECTIONS["elasticsearch"]["URL"]) try: es.info() except ElasticsearchException as e: log.error( "elasticsearch not running on %r" % settings.HAYSTACK_CONNECTIONS["elasticsearch"]["URL"], exc_info=True, ) raise unittest.SkipTest( "elasticsearch not running on %r" % settings.HAYSTACK_CONNECTIONS["elasticsearch"]["URL"], e, )
def setUp(self): super(SearchResultTestCase, self).setUp() cap = CaptureHandler() logging.getLogger("haystack").addHandler(cap) self.no_data = {} self.extra_data = {"stored": "I am stored data. How fun."} self.no_overwrite_data = { "django_id": 2, "django_ct": "haystack.anothermockmodel", "stored": "I am stored data. How fun.", } self.no_data_sr = MockSearchResult("haystack", "mockmodel", "1", 2) self.extra_data_sr = MockSearchResult("haystack", "mockmodel", "1", 3, **self.extra_data) self.no_overwrite_data_sr = MockSearchResult("haystack", "mockmodel", "1", 4, **self.no_overwrite_data)
def __init__(self, connection_alias, **connection_options): super(SolrSearchBackend, self).__init__(connection_alias, **connection_options) if not 'URL' in connection_options: raise ImproperlyConfigured("You must specify a 'URL' in your settings for connection '%s'." % connection_alias) self.conn = Solr(connection_options['URL'], timeout=self.timeout, **connection_options.get('KWARGS', {})) self.log = logging.getLogger('haystack')
def setUp(self): super(SearchResultTestCase, self).setUp() cap = CaptureHandler() logging.getLogger('haystack').addHandler(cap) self.no_data = {} self.extra_data = { 'stored': 'I am stored data. How fun.', } self.no_overwrite_data = { 'django_id': 2, 'django_ct': 'haystack.anothermockmodel', 'stored': 'I am stored data. How fun.', } self.no_data_sr = MockSearchResult('haystack', 'mockmodel', '1', 2) self.extra_data_sr = MockSearchResult('haystack', 'mockmodel', '1', 3, **self.extra_data) self.no_overwrite_data_sr = MockSearchResult('haystack', 'mockmodel', '1', 4, **self.no_overwrite_data)
def __init__(self, connection_alias, **connection_options): super(GAESearchBackend, self).__init__(connection_alias, **connection_options) if 'INDEX_NAME' not in connection_options: raise ImproperlyConfigured("You must specify a 'INDEX_NAME' in your settings for connection '%s'." % connection_alias) self.index_name = connection_options['INDEX_NAME'] self.setup_complete = False self.log = logging.getLogger('haystack') self.batch_size = connection_options.get('BATCH_SIZE', 200)
def setUp(self): super(SearchResultTestCase, self).setUp() cap = CaptureHandler() logging.getLogger('haystack').addHandler(cap) self.no_data = {} self.extra_data = { 'stored': 'I am stored data. How fun.', } self.no_overwrite_data = { 'django_id': 2, 'django_ct': 'haystack.anothermockmodel', 'stored': 'I am stored data. How fun.', } # The str(1) bit might seem unnecessary but it avoids test_unicode needing to handle # the differences between repr() output on Python 2 and 3 for a unicode literal: self.no_data_sr = MockSearchResult('haystack', 'mockmodel', str(1), 2) self.extra_data_sr = MockSearchResult('haystack', 'mockmodel', str(1), 3, **self.extra_data) self.no_overwrite_data_sr = MockSearchResult('haystack', 'mockmodel', str(1), 4, **self.no_overwrite_data)
def __init__(self, connection_alias, **connection_options): super(ElasticsearchSearchBackend, self).__init__(connection_alias, **connection_options) if not 'URL' in connection_options: raise ImproperlyConfigured("You must specify a 'URL' in your settings for connection '%s'." % connection_alias) if not 'INDEX_NAME' in connection_options: raise ImproperlyConfigured("You must specify a 'INDEX_NAME' in your settings for connection '%s'." % connection_alias) self.conn = elasticsearch.Elasticsearch(connection_options['URL'], timeout=self.timeout, **connection_options.get('KWARGS', {})) self.index_name = connection_options['INDEX_NAME'] self.log = logging.getLogger('haystack') self.setup_complete = False self.existing_mapping = {}
def __init__(self, connection_alias, **connection_options): super(ElasticsearchSearchBackend, self).__init__(connection_alias, **connection_options) if not 'URL' in connection_options: raise ImproperlyConfigured("You must specify a 'URL' in your settings for connection '%s'." % connection_alias) if not 'INDEX_NAME' in connection_options: raise ImproperlyConfigured("You must specify a 'INDEX_NAME' in your settings for connection '%s'." % connection_alias) self.conn = pyelasticsearch.ElasticSearch(connection_options['URL'], timeout=self.timeout) self.index_name = connection_options['INDEX_NAME'] self.log = logging.getLogger('haystack') self.setup_complete = False self.existing_mapping = {}
def __init__(self, connection_alias, **connection_options): super(WhooshSearchBackend, self).__init__(connection_alias, **connection_options) self.setup_complete = False self.use_file_storage = True self.post_limit = getattr(connection_options, 'POST_LIMIT', 128 * 1024 * 1024) self.path = connection_options.get('PATH') if connection_options.get('STORAGE', 'file') != 'file': self.use_file_storage = False if self.use_file_storage and not self.path: raise ImproperlyConfigured("You must specify a 'PATH' in your settings for connection '%s'." % connection_alias) self.log = logging.getLogger('haystack')
def __init__(self, connection_alias, **connection_options): super(SolrSearchBackend, self).__init__(connection_alias, **connection_options) if "URL" not in connection_options: raise ImproperlyConfigured( "You must specify a 'URL' in your settings for connection '%s'." % connection_alias) self.collate = connection_options.get("COLLATE_SPELLING", True) self.conn = Solr(connection_options["URL"], timeout=self.timeout, **connection_options.get("KWARGS", {})) self.log = logging.getLogger("haystack")
def __init__(self, connection_alias, **connection_options): super(ElasticsearchSearchBackend, self).__init__(connection_alias, **connection_options) if not 'URL' in connection_options: raise ImproperlyConfigured("You must specify a 'URL' in your settings for connection '%s'." % connection_alias) if not 'INDEX_NAME' in connection_options: raise ImproperlyConfigured("You must specify a 'INDEX_NAME' in your settings for connection '%s'." % connection_alias) self.conn = pyelasticsearch.ElasticSearch(connection_options['URL'], timeout=self.timeout) self.index_name = connection_options['INDEX_NAME'] self.log = logging.getLogger('haystack') self.setup_complete = False self.existing_mapping = {} self._auto_gen_phrase_query = connection_options.get('AUTO_GENERATE_PHRASE_QUERY', True) self._analyse_wildcard = connection_options.get('ANALYZE_WILDCARD', True)
def __init__(self, connection_alias, **connection_options): super(ElasticsearchSearchBackend, self).__init__(connection_alias, **connection_options) if not 'URL' in connection_options: raise ImproperlyConfigured("You must specify a 'URL' in your settings for connection '%s'." % connection_alias) if not 'INDEX_NAME' in connection_options: raise ImproperlyConfigured("You must specify a 'INDEX_NAME' in your settings for connection '%s'." % connection_alias) self.conn = pyelasticsearch.ElasticSearch(connection_options['URL'], timeout=self.timeout) self.index_name = connection_options['INDEX_NAME'] self.log = logging.getLogger('haystack') self.setup_complete = False self.existing_mapping = {} self.index_settings = connection_options.get('INDEX_SETTINGS', self.DEFAULT_SETTINGS) self.search_analyzer = connection_options.get('SEARCH_ANALYZER')
def __init__(self, connection_alias, **connection_options): super().__init__(connection_alias, **connection_options) self.setup_complete = False self.use_file_storage = True self.post_limit = getattr(connection_options, "POST_LIMIT", 128 * 1024 * 1024) self.path = connection_options.get("PATH") if connection_options.get("STORAGE", "file") != "file": self.use_file_storage = False if self.use_file_storage and not self.path: raise ImproperlyConfigured( "You must specify a 'PATH' in your settings for connection '%s'." % connection_alias) self.log = logging.getLogger("haystack")
def __init__(self, connection_alias, **connection_options): super(SolrSearchBackend, self).__init__(connection_alias, **connection_options) if "URL" not in connection_options: raise ImproperlyConfigured( "You must specify a 'URL' in your settings for connection '%s'." % connection_alias ) self.collate = connection_options.get("COLLATE_SPELLING", True) self.conn = Solr( connection_options["URL"], timeout=self.timeout, **connection_options.get("KWARGS", {}) ) self.log = logging.getLogger("haystack")
def __init__(self, using=None, query=None): # ``_using`` should only ever be a value other than ``None`` if it's # been forced with the ``.using`` method. self._using = using self.query = None self._determine_backend() # If ``query`` is present, it should override even what the routers # think. if query is not None: self.query = query self._result_cache = [] self._result_count = None self._cache_full = False self._load_all = False self._ignored_result_count = 0 self.log = logging.getLogger('haystack')
def setup(): log = logging.getLogger('haystack') try: import elasticsearch if not ((2, 0, 0) <= elasticsearch.__version__ < (3, 0, 0)): raise ImportError from elasticsearch import Elasticsearch, exceptions except ImportError: log.error("Skipping ElasticSearch 2 tests: 'elasticsearch>=2.0.0,<3.0.0' not installed.") raise unittest.SkipTest("'elasticsearch>=2.0.0,<3.0.0' not installed.") url = settings.HAYSTACK_CONNECTIONS['elasticsearch']['URL'] es = Elasticsearch(url) try: es.info() except exceptions.ConnectionError as e: log.error("elasticsearch not running on %r" % url, exc_info=True) raise unittest.SkipTest("elasticsearch not running on %r" % url, e)
def __init__(self, connection_alias, **connection_options): super(SolrSearchBackend, self).__init__(connection_alias, **connection_options) if "zk" not in connection_options: raise ImproperlyConfigured( "You must specify a 'zk' (zookeeper str) in your settings for connection '%s'. (%s)" % (connection_alias, connection_options), ) self.collate = connection_options.get("COLLATE_SPELLING", True) self.conn = Solr(connection_options["zk"], connection_options["collection"], connection_options["user"], connection_options["password"], timeout=self.timeout, **connection_options.get("KWARGS", {})) self.log = logging.getLogger("haystack")
def setup(): log = logging.getLogger('haystack') try: import elasticsearch if not ((2, 0, 0) <= elasticsearch.__version__ < (3, 0, 0)): raise ImportError from elasticsearch import Elasticsearch, exceptions except ImportError: log.error("'elasticsearch>=2.0.0,<3.0.0' not installed.", exc_info=True) raise unittest.SkipTest("'elasticsearch>=2.0.0,<3.0.0' not installed.") url = settings.HAYSTACK_CONNECTIONS['default']['URL'] es = Elasticsearch(url) try: es.info() except exceptions.ConnectionError as e: log.error("elasticsearch not running on %r" % url, exc_info=True) raise unittest.SkipTest("elasticsearch not running on %r" % url, e)
def setup(): log = logging.getLogger('haystack') try: import elasticsearch if not ((1, 0, 0) <= elasticsearch.__version__ < (2, 0, 0)): raise ImportError from elasticsearch import Elasticsearch, ElasticsearchException except ImportError: log.error("elasticsearch-py not installed.", exc_info=True) raise unittest.SkipTest("elasticsearch-py not installed.") es = Elasticsearch(settings.HAYSTACK_CONNECTIONS['default']['URL']) try: es.info() except ElasticsearchException as e: log.error("elasticsearch not running on %r" % \ settings.HAYSTACK_CONNECTIONS['default']['URL'], exc_info=True) raise unittest.SkipTest("elasticsearch not running on %r" % \ settings.HAYSTACK_CONNECTIONS['default']['URL'], e)
def __init__(self, connection_alias, **connection_options): super().__init__(connection_alias, **connection_options) if "URL" not in connection_options: raise ImproperlyConfigured( "You must specify a 'URL' in your settings for connection '%s'." % connection_alias ) self.collate = connection_options.get("COLLATE_SPELLING", True) # Support to `date_facet` on Solr >= 6.6. Olders set `date` self.date_facet_field = connection_options.get("DATE_FACET_FIELD", "range") self.conn = Solr( connection_options["URL"], timeout=self.timeout, **connection_options.get("KWARGS", {}) ) self.log = logging.getLogger("haystack")
def __init__(self, connection_alias, **connection_options): super().__init__(connection_alias, **connection_options) if "URL" not in connection_options: raise ImproperlyConfigured( "You must specify a 'URL' in your settings for connection '%s'." % connection_alias) if "INDEX_NAME" not in connection_options: raise ImproperlyConfigured( "You must specify a 'INDEX_NAME' in your settings for connection '%s'." % connection_alias) self.conn = elasticsearch.Elasticsearch(connection_options["URL"], timeout=self.timeout, **connection_options.get( "KWARGS", {})) self.index_name = connection_options["INDEX_NAME"] self.log = logging.getLogger("haystack") self.setup_complete = False self.existing_mapping = {}
def setup(): log = logging.getLogger("haystack") try: import elasticsearch if not ((5, 0, 0) <= elasticsearch.__version__ < (6, 0, 0)): raise ImportError from elasticsearch import Elasticsearch, exceptions except ImportError: log.error( "Skipping ElasticSearch 5 tests: 'elasticsearch>=5.0.0,<6.0.0' not installed." ) raise unittest.SkipTest("'elasticsearch>=5.0.0,<6.0.0' not installed.") url = settings.HAYSTACK_CONNECTIONS["elasticsearch"]["URL"] es = Elasticsearch(url) try: es.info() except exceptions.ConnectionError as e: log.error("elasticsearch not running on %r" % url, exc_info=True) raise unittest.SkipTest("elasticsearch not running on %r" % url, e)
def __init__(self, connection_alias, **connection_options): super(SolrSearchBackend, self).__init__(connection_alias, **connection_options) if 'URL' not in connection_options: raise ImproperlyConfigured( "You must specify a 'URL' in your settings for connection '%s'." % connection_alias) if 'COLLECTION' not in connection_options: raise ImproperlyConfigured( "You must specify a 'collection' in your settings for connection '%s'." % connection_alias) self.collate = connection_options.get('COLLATE_SPELLING', True) zookeeper = ZooKeeper(connection_options['URL']) self.conn = SolrCloud(zookeeper, connection_options['COLLECTION'], timeout=self.timeout, **connection_options.get('KWARGS', {})) self.log = logging.getLogger('haystack')
def __init__(self, connection_alias, **connection_options): super(ElasticsearchSearchBackend, self).__init__(connection_alias, **connection_options) if not 'URL' in connection_options: raise ImproperlyConfigured("You must specify a 'URL' in your settings for connection '%s'." % connection_alias) if not 'INDEX_NAME' in connection_options: raise ImproperlyConfigured("You must specify a 'INDEX_NAME' in your settings for connection '%s'." % connection_alias) # Use different modules depending on the version. if connection_options.get('ES_1X'): self.elasticsearch = elasticsearch_1x helpers = elasticsearch_1x_helpers else: self.elasticsearch = elasticsearch_9x helpers = elasticsearch_9x_helpers self.conn = self.elasticsearch.Elasticsearch(connection_options['URL'], timeout=self.timeout, **connection_options.get('KWARGS', {})) self.TransportError = self.elasticsearch.TransportError self.es_bulk_index = helpers.bulk_index self.index_name = connection_options['INDEX_NAME'] self.log = logging.getLogger('haystack') self.setup_complete = False self.existing_mapping = {}
def __init__(self, connection_alias, **connection_options): super(WhooshSearchBackend, self).__init__(connection_alias, **connection_options) self.setup_complete = False self.use_file_storage = True self.post_limit = getattr(connection_options, 'POST_LIMIT', 128 * 1024 * 1024) self.path = connection_options.get('PATH') if connection_options.get('STORAGE', 'file') != 'file': self.use_file_storage = False if self.use_file_storage and not self.path: raise ImproperlyConfigured("You must specify a 'PATH' in your settings for connection '%s'." % connection_alias) self.log = logging.getLogger('haystack') analyzer = getattr(settings, 'HAYSTACK_WHOOSH_ANALYZER', None) if analyzer is not None: from django.utils.module_loading import import_by_path self.analyzer = import_by_path(analyzer) self.log.info('Using analyzer %s' % analyzer) else: self.analyzer = StemmingAnalyzer() self.log.info('Using default analyzer')
def __setstate__(self, data_dict): """ For unpickling. """ self.__dict__ = data_dict self.log = logging.getLogger('haystack')
from haystack.inputs import PythonData from haystack.models import SearchResult if settings.DEBUG: from haystack.utils import log as logging class NullHandler(logging.Handler): def emit(self, record): pass ch = logging.StreamHandler() ch.setLevel(logging.WARNING) ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) logger = logging.getLogger('haystack.simple_backend') logger.setLevel(logging.WARNING) logger.addHandler(NullHandler()) logger.addHandler(ch) else: logger = None class SimpleSearchBackend(BaseSearchBackend): def update(self, indexer, iterable, commit=True): if logger is not None: logger.warning('update is not implemented in this backend') def remove(self, obj, commit=True): if logger is not None: logger.warning('remove is not implemented in this backend')
from haystack.models import SearchResult if settings.DEBUG: from haystack.utils import log as logging class NullHandler(logging.Handler): def emit(self, record): pass ch = logging.StreamHandler() ch.setLevel(logging.WARNING) ch.setFormatter( logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s')) logger = logging.getLogger('haystack.simple_backend') logger.setLevel(logging.WARNING) logger.addHandler(NullHandler()) logger.addHandler(ch) else: logger = None class SimpleSearchBackend(BaseSearchBackend): def update(self, indexer, iterable, commit=True): if logger is not None: logger.warning('update is not implemented in this backend') def remove(self, obj, commit=True): if logger is not None: logger.warning('remove is not implemented in this backend')
# -*- coding: utf-8 -*- import string from haystack import indexes from haystack.utils import log as logging from colab_noosfero.models import (NoosferoArticle, NoosferoCommunity, NoosferoSoftwareCommunity, NoosferoComment) logger = logging.getLogger('haystack') # The string maketrans always return a string encoded with latin1 # http://stackoverflow.com/questions/1324067/how-do-i-get-str-translate-to-work-with-unicode-strings table = string.maketrans( string.punctuation, '.' * len(string.punctuation) ).decode('latin1') class NoosferoCommunityIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.EdgeNgramField(document=True, use_template=True, stored=False) title = indexes.EdgeNgramField(model_attr='name') description = indexes.EdgeNgramField(model_attr='description', null=True) url = indexes.EdgeNgramField(model_attr='url', indexed=False) icon_name = indexes.EdgeNgramField() type = indexes.EdgeNgramField() modified = indexes.DateTimeField(model_attr='modified', null=True) created_at = indexes.DateTimeField(model_attr='created_at', null=True)
def _get_log(self): return logging.getLogger('haystack')
from django.utils.encoding import force_text from elasticsearch_dsl import Q from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery, log_query from haystack.models import SearchResult from haystack.utils import log as logging from blog.documents import ArticleDocument, ArticleDocumentManager from blog.models import Article logger = logging.getLogger(__name__) class ElasticSearchBackend(BaseSearchBackend): def __init__(self, connection_alias, **connection_options): super( ElasticSearchBackend, self).__init__( connection_alias, **connection_options) self.manager = ArticleDocumentManager() def _get_models(self, iterable): models = iterable if iterable and iterable[0] else Article.objects.all() docs = self.manager.convert_to_doc(models) return docs def _create(self, models): self.manager.create_index() docs = self._get_models(models) self.manager.rebuild(docs)
# -*- coding: utf-8 -*- import math import string from django.template import loader, Context from django.utils.text import slugify from haystack import indexes from haystack.utils import log as logging from search.base_indexes import BaseIndex from .models import Attachment, Ticket, Wiki, Revision logger = logging.getLogger("haystack") # the string maketrans always return a string encoded with latin1 # http://stackoverflow.com/questions/1324067/how-do-i-get-str-translate-to-work-with-unicode-strings table = string.maketrans(string.punctuation, "." * len(string.punctuation)).decode("latin1") class AttachmentIndex(BaseIndex, indexes.Indexable): title = indexes.CharField(model_attr="filename") description = indexes.CharField(model_attr="description", null=True) modified = indexes.DateTimeField(model_attr="created", null=True) used_by = indexes.CharField(model_attr="used_by", null=True, stored=False) mimetype = indexes.CharField(model_attr="mimetype", null=True, stored=False) size = indexes.IntegerField(model_attr="size", null=True, stored=False) filename = indexes.CharField(stored=False) def get_model(self):
# -*- coding: utf-8 -*- import string from haystack import indexes from haystack.utils import log as logging from .models import (GitlabProject, GitlabMergeRequest, GitlabIssue, GitlabComment) logger = logging.getLogger('haystack') # The string maketrans always return a string encoded with latin1 # http://stackoverflow.com/questions/1324067/how-do-i-get-str-translate-to-work-with-unicode-strings table = string.maketrans( string.punctuation, '.' * len(string.punctuation) ).decode('latin1') class GitlabProjectIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.EdgeNgramField(document=True, use_template=True, stored=False) title = indexes.EdgeNgramField(model_attr='name') description = indexes.EdgeNgramField(model_attr='description', null=True) tag = indexes.EdgeNgramField() url = indexes.EdgeNgramField(model_attr='url', indexed=False) icon_name = indexes.EdgeNgramField() type = indexes.EdgeNgramField()