Beispiel #1
0
from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery, EmptyResults, log_query
from haystack.constants import DJANGO_CT, DJANGO_ID, ID
from haystack.exceptions import MissingDependency, SearchBackendError, SkipDocument
from haystack.inputs import Clean, Exact, PythonData, Raw
from haystack.models import SearchResult
from haystack.utils import log as logging
from haystack.utils import get_identifier, get_model_ct
from haystack.utils.app_loading import haystack_get_model

from jieba.analyse import ChineseAnalyzer  #在顶部添加

try:
    import whoosh
except ImportError:
    raise MissingDependency(
        "The 'whoosh' backend requires the installation of 'Whoosh'. Please refer to the documentation."
    )

# Handle minimum requirement.
if not hasattr(whoosh, '__version__') or whoosh.__version__ < (2, 5, 0):
    raise MissingDependency(
        "The 'whoosh' backend requires version 2.5.0 or greater.")

# Bubble up the correct error.
from whoosh import index
from whoosh.analysis import StemmingAnalyzer
from whoosh.fields import ID as WHOOSH_ID
from whoosh.fields import BOOLEAN, DATETIME, IDLIST, KEYWORD, NGRAM, NGRAMWORDS, NUMERIC, Schema, TEXT
from whoosh.filedb.filestore import FileStorage, RamStorage
from whoosh.highlight import highlight as whoosh_highlight
from whoosh.highlight import ContextFragmenter, HtmlFormatter
from django.utils import six

from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery, EmptyResults, log_query
from haystack.constants import DJANGO_CT, DJANGO_ID, ID
from haystack.exceptions import MissingDependency, MoreLikeThisError, SkipDocument
from haystack.inputs import Clean, Exact, PythonData, Raw
from haystack.models import SearchResult
from haystack.utils import log as logging
from haystack.utils import get_identifier, get_model_ct
from haystack.utils.app_loading import haystack_get_model

try:
    from pysolr import Solr, SolrError
except ImportError:
    raise MissingDependency(
        "The 'solr' backend requires the installation of 'pysolr'. Please refer to the documentation."
    )


class SolrSearchBackend(BaseSearchBackend):
    # Word reserved by Solr for special use.
    RESERVED_WORDS = (
        'AND',
        'NOT',
        'OR',
        'TO',
    )

    # Characters reserved by Solr for special use.
    # The '\\' must come first, so as not to overwrite the other slash replacements.
    RESERVED_CHARACTERS = (
from django.core.exceptions import ImproperlyConfigured
from django.db.models.loading import get_model
from django.utils import six
import haystack
from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery, log_query
from haystack.constants import ID, DJANGO_CT, DJANGO_ID, DEFAULT_OPERATOR
from haystack.exceptions import MissingDependency, MoreLikeThisError
from haystack.inputs import PythonData, Clean, Exact, Raw
from haystack.models import SearchResult
from haystack.utils import get_identifier
from haystack.utils import log as logging

try:
    import requests
except ImportError:
    raise MissingDependency("The 'elasticsearch' backend requires the installation of 'requests'.")
try:
    import pyelasticsearch
except ImportError:
    raise MissingDependency("The 'elasticsearch' backend requires the installation of 'pyelasticsearch'. Please refer to the documentation.")


DATETIME_REGEX = re.compile(
    r'^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T'
    r'(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(\.\d+)?$')


class ElasticsearchSearchBackend(BaseSearchBackend):
    # Word reserved by Elasticsearch for special use.
    RESERVED_WORDS = (
        'AND',
Beispiel #4
0
from haystack.models import SearchResult
from haystack.constants import (DEFAULT_OPERATOR, DJANGO_CT, DJANGO_ID,
                                FUZZY_MAX_EXPANSIONS, DEFAULT_ALIAS,
                                FILTER_SEPARATOR, VALID_FILTERS)
from haystack.utils import get_model_ct
from haystack.utils.app_loading import haystack_get_model
from haystack.exceptions import MissingDependency

try:
    import elasticsearch
    if not ((5, 0, 0) <= elasticsearch.__version__ < (6, 0, 0)):
        raise ImportError
    from elasticsearch.helpers import bulk, scan
except ImportError:
    raise MissingDependency("The 'elasticsearch2' backend requires the \
                            installation of 'elasticsearch>=5.0.0,<6.0.0'. \
                            Please refer to the documentation.")

__all__ = ['Elasticsearch5SearchBackend', 'Elasticsearch5SearchEngine']

DATE_HISTOGRAM_FIELD_NAME_SUFFIX = '_haystack_date_histogram'
DATE_RANGE_FIELD_NAME_SUFFIX = '_haystack_date_range'

DEFAULT_FIELD_MAPPING = {'type': 'text'}
FIELD_MAPPINGS = {
    'text': {
        'type': 'text'
    },
    'keyword': {
        'type': 'keyword'
    },
Beispiel #5
0
from django.utils import simplejson

import haystack
from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery
from haystack.exceptions import MissingDependency
from haystack.models import SearchResult
from haystack.utils import get_identifier


from haystack_cloudsearch.cloudsearch_utils import (ID, DJANGO_CT, DJANGO_ID,
                                        gen_version,
                                        botobool)
try:
    import boto
except ImportError:
    raise MissingDependency("The 'cloudsearch' backend requires the installation of 'boto'. Please refer to the documentation.")

from boto.cloudsearch import CloudsearchProcessingException, CloudsearchNeedsIndexingException

class CloudsearchSearchBackend(BaseSearchBackend):

    def __init__(self, connection_alias, **connection_options):
        super(CloudsearchSearchBackend, self).__init__(connection_alias, **connection_options)

        if not 'AWS_ACCESS_KEY_ID' in connection_options:
            raise ImproperlyConfigured("You must specify a 'AWS_ACCESS_KEY_ID' in your settings for connection '%s'." % connection_alias)

        if not 'AWS_SECRET_KEY' in connection_options:
            raise ImproperlyConfigured("You must specify a 'AWS_SECRET_KEY' in your settings for connection '%s'." % connection_alias)

        # Allow overrides for the SearchDomain prefix
import logging
import warnings
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.encoding import force_unicode
from django.contrib.contenttypes.models import ContentType
from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery, log_query
from haystack.exceptions import MissingDependency, SearchBackendError
from haystack.utils import get_identifier
from haystack.constants import ID, DJANGO_CT, DJANGO_ID
from sphinx_haystack.models import Document
try:
    import MySQLdb
except ImportError:
    raise MissingDependency(
        "The 'sphinx' backend requires the installation of 'MySQLdb'. Please refer to the documentation."
    )
try:
    # Pool connections if SQLAlchemy is present.
    import sqlalchemy.pool as pool
    # TODO: troubleshoot 'MySQL server has gone away'
    # For now disable connection pool.
    # MySQLdb = pool.manage(MySQLdb)
    connection_pooling = True
except ImportError:
    connection_pooling = False

DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = 9306