# encoding: utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
import json
import os
import re
import shutil
import threading
import warnings
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils import six
from django.utils.datetime_safe import datetime
from django.utils.encoding import force_text
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
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 whoosh.qparser import QueryParser
from whoosh.searching import ResultsPage
from whoosh.writing import AsyncWriter
from jieba.analyse import ChineseAnalyzer
DATETIME_REGEX = re.compile('^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(\.\d{3,6}Z?)?$')
LOCALS = threading.local()
LOCALS.RAM_STORE = None
class WhooshHtmlFormatter(HtmlFormatter):
"""
This is a HtmlFormatter simpler than the whoosh.HtmlFormatter.
We use it to have consistent results across backends. Specifically,
Solr, Xapian and Elasticsearch are using this formatting.
"""
template = '<%(tag)s>%(t)s</%(tag)s>'
class WhooshSearchBackend(BaseSearchBackend):
# Word reserved by Whoosh for special use.
RESERVED_WORDS = (
'AND',
'NOT',
'OR',
'TO',
)
# Characters reserved by Whoosh for special use.
# The '\\' must come first, so as not to overwrite the other slash replacements.
RESERVED_CHARACTERS = (
'\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}',
'[', ']', '^', '"', '~', '*', '?', ':', '.',
)
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 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
def build_schema(self, fields):
schema_fields = {
ID: WHOOSH_ID(stored=True, unique=True),
DJANGO_CT: WHOOSH_ID(stored=True),
DJANGO_ID: WHOOSH_ID(stored=True),
}
# Grab the number of keys that are hard-coded into Haystack.
# We'll use this to (possibly) fail slightly more gracefully later.
initial_key_count = len(schema_fields)
content_field_name = ''
for field_name, field_class in fields.items():
if field_class.is_multivalued:
if field_class.indexed is False:
schema_fields[field_class.index_fieldname] = IDLIST(stored=True, field_boost=field_class.boost)
else:
schema_fields[field_class.index_fieldname] = KEYWORD(stored=True, commas=True, scorable=True, field_boost=field_class.boost)
elif field_class.field_type in ['date', 'datetime']:
schema_fields[field_class.index_fieldname] = DATETIME(stored=field_class.stored, sortable=True)
elif field_class.field_type == 'integer':
schema_fields[field_class.index_fieldname] = NUMERIC(stored=field_class.stored, numtype=int, field_boost=field_class.boost)
elif field_class.field_type == 'float':
schema_fields[field_class.index_fieldname] = NUMERIC(stored=field_class.stored, numtype=float, field_boost=field_class.boost)
elif field_class.field_type == 'boolean':
# Field boost isn't supported on BOOLEAN as of 1.8.2.
schema_fields[field_class.index_fieldname] = BOOLEAN(stored=field_class.stored)
elif field_class.field_type == 'ngram':
schema_fields[field_class.index_fieldname] = NGRAM(minsize=3, maxsize=15, stored=field_class.stored, field_boost=field_class.boost)
elif field_class.field_type == 'edge_ngram':
schema_fields[field_class.index_fieldname] = NGRAMWORDS(minsize=2, maxsize=15, at='start', stored=field_class.stored, field_boost=field_class.boost)
else:
schema_fields[field_class.index_fieldname] = TEXT(stored=True, analyzer=ChineseAnalyzer(), field_boost=field_class.boost, sortable=True)
if field_class.document is True:
content_field_name = field_class.index_fieldname
schema_fields[field_class.index_fieldname].spelling = True
# Fail more gracefully than relying on the backend to die if no fields
# are found.
if len(schema_fields) <= initial_key_count:
raise SearchBackendError("No fields were found in any search_indexes. Please correct this before attempting to search.")
return (content_field_name, Schema(**schema_fields))
def update(self, index, iterable, commit=True):
if not self.setup_complete:
self.setup()
self.index = self.index.refresh()
writer = AsyncWriter(self.index)
for obj in iterable:
try:
doc = index.full_prepare(obj)
except SkipDocument:
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
毕设&课设&项目&实训-基于centos6+python3.6+django2+ansible2.4+celery4.2 运维管理系统 【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】: 所有源码都经过严格测试,可以直接运行。 功能在确认正常工作后才上传。 【适用人群】: 适用于希望学习不同技术领域的小白或进阶学习者。 可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】: 项目具有较高的学习借鉴价值,也可直接拿来修改复刻。 对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同进步。
资源推荐
资源详情
资源评论
收起资源包目录
毕设&课设&项目&实训-基于centos6+python3.6+django2+ansible2.4运维管理系统.zip (2000个子文件)
bootstrap.css 143KB
bootstrap.min.css 118KB
AdminLTE.css 109KB
AdminLTE.min.css 89KB
AdminLTE-without-plugins.css 88KB
AdminLTE-without-plugins.min.css 72KB
editormd.min.css 60KB
ionicons.css 56KB
ionicons.min.css 50KB
asciinema-player.css 50KB
_all-skins.css 47KB
custombox.min.css 41KB
_all-skins.min.css 41KB
font-awesome.css 37KB
jquery-ui.css 35KB
jquery-ui.css 35KB
jquery-ui.css 35KB
jquery-ui.css 35KB
jquery-ui.css 35KB
jquery-ui.css 35KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
jquery-ui.css 34KB
font-awesome.min.css 30KB
jquery-ui.min.css 30KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
jquery-ui.min.css 29KB
ambiance.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
jquery-confirm.min.css 22KB
_all.css 21KB
theme.css 17KB
select2.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
theme.css 17KB
AdminLTE-bootstrap-social.css 15KB
modaal.css 15KB
jsplumbtoolkit-defaults.css 15KB
_all.css 15KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
妄北y
- 粉丝: 2w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- S7-200SMART多段插补库(含使用说明+示例程序).rar
- 毕业设计-基于SSM协同过滤音乐推荐管理系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于SSM小区物业管理系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于SSM网上医院预约挂号系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于SSM学生毕业设计-论文选题系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于SSM学而优奖学金评定管理系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于thinkphp6.0+mysql+bootstrap4的疫情防控系统全部资料+详细文档+高分项目+源码.zip
- 3-各地区-不同行业-就业、失业、工资144个指标(1990-2021年).zip
- MATLAB代码:计及源-荷双重不确定性的电厂 微网日前随机优化调度 关键词:电厂 微网 随机优化 随机调度 源-荷双重不确定性 电厂调度 参考文档:Virtual power plant
- rds.zip
- common.zip
- 毕业设计-基于VUe+Element的人事管理系统全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于Structs+Hibernate+Spring+mahout+bootstrap+mysql 实现的网上书店前后台系统全部资料+详细文档+高分项
- 毕业设计-基于vue+Python在线考试系统前端全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于vue2的在线答题系统前端全部资料+详细文档+高分项目+源码.zip
- 毕业设计-基于VUE+PHP的高校校友信息管理系统毕业设计-全部资料+详细文档+高分项目+源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功