|
|
|
|
@ -29,11 +29,11 @@ import re
|
|
|
|
|
import shutil
|
|
|
|
|
import threading
|
|
|
|
|
import warnings
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
import six
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from django.utils.encoding import force_str
|
|
|
|
|
from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery, EmptyResults, log_query
|
|
|
|
|
from haystack.constants import DJANGO_CT, DJANGO_ID, ID
|
|
|
|
|
@ -155,7 +155,7 @@ class WhooshSearchBackend(BaseSearchBackend):
|
|
|
|
|
connections[self.connection_alias].get_unified_index().all_searchfields())
|
|
|
|
|
self.parser = QueryParser(self.content_field_name, schema=self.schema)
|
|
|
|
|
|
|
|
|
|
if new_index:
|
|
|
|
|
if new_index is True:
|
|
|
|
|
self.index = self.storage.create_index(self.schema)
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
@ -230,7 +230,7 @@ class WhooshSearchBackend(BaseSearchBackend):
|
|
|
|
|
raise SearchBackendError(
|
|
|
|
|
"No fields were found in any search_indexes. Please correct this before attempting to search.")
|
|
|
|
|
|
|
|
|
|
return content_field_name, Schema(**schema_fields)
|
|
|
|
|
return (content_field_name, Schema(**schema_fields))
|
|
|
|
|
|
|
|
|
|
def update(self, index, iterable, commit=True):
|
|
|
|
|
"""`xjj`
|
|
|
|
|
@ -389,8 +389,7 @@ class WhooshSearchBackend(BaseSearchBackend):
|
|
|
|
|
self.index = self.index.refresh()
|
|
|
|
|
self.index.optimize()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def calculate_page(start_offset=0, end_offset=None):
|
|
|
|
|
def calculate_page(self, start_offset=0, end_offset=None):
|
|
|
|
|
"""`xjj`
|
|
|
|
|
|
|
|
|
|
根据偏移量计算分页信息。
|
|
|
|
|
@ -699,7 +698,7 @@ class WhooshSearchBackend(BaseSearchBackend):
|
|
|
|
|
|
|
|
|
|
# Deferred models will have a different class ("RealClass_Deferred_fieldname")
|
|
|
|
|
# which won't be in our registry:
|
|
|
|
|
model_instance._meta.concrete_model
|
|
|
|
|
model_klass = model_instance._meta.concrete_model
|
|
|
|
|
|
|
|
|
|
field_name = self.content_field_name
|
|
|
|
|
narrow_queries = set()
|
|
|
|
|
@ -944,8 +943,7 @@ class WhooshSearchBackend(BaseSearchBackend):
|
|
|
|
|
spelling_suggestion = ' '.join(suggested_words)
|
|
|
|
|
return spelling_suggestion
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _from_python(value):
|
|
|
|
|
def _from_python(self, value):
|
|
|
|
|
"""
|
|
|
|
|
Converts Python values to a string for Whoosh.
|
|
|
|
|
|
|
|
|
|
@ -968,8 +966,7 @@ class WhooshSearchBackend(BaseSearchBackend):
|
|
|
|
|
value = force_str(value)
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _to_python(value):
|
|
|
|
|
def _to_python(self, value):
|
|
|
|
|
"""
|
|
|
|
|
Converts values from Whoosh to native Python values.
|
|
|
|
|
|
|
|
|
|
@ -1021,8 +1018,7 @@ class WhooshSearchBackend(BaseSearchBackend):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WhooshSearchQuery(BaseSearchQuery):
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _convert_datetime(date):
|
|
|
|
|
def _convert_datetime(self, date):
|
|
|
|
|
"""`xjj`
|
|
|
|
|
|
|
|
|
|
将日期时间对象转换为 Whoosh 可识别的字符串格式。
|
|
|
|
|
@ -1079,7 +1075,7 @@ class WhooshSearchQuery(BaseSearchQuery):
|
|
|
|
|
str: 构建好的查询片段字符串。
|
|
|
|
|
"""
|
|
|
|
|
from haystack import connections
|
|
|
|
|
''
|
|
|
|
|
query_frag = ''
|
|
|
|
|
is_datetime = False
|
|
|
|
|
|
|
|
|
|
if not hasattr(value, 'input_type_name'):
|
|
|
|
|
@ -1124,7 +1120,7 @@ class WhooshSearchQuery(BaseSearchQuery):
|
|
|
|
|
'fuzzy': u'%s~',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if not value.post_process:
|
|
|
|
|
if value.post_process is False:
|
|
|
|
|
query_frag = prepared_value
|
|
|
|
|
else:
|
|
|
|
|
if filter_type in [
|
|
|
|
|
@ -1143,7 +1139,7 @@ class WhooshSearchQuery(BaseSearchQuery):
|
|
|
|
|
if isinstance(prepared_value, six.string_types):
|
|
|
|
|
possible_values = prepared_value.split(' ')
|
|
|
|
|
else:
|
|
|
|
|
if is_datetime:
|
|
|
|
|
if is_datetime is True:
|
|
|
|
|
prepared_value = self._convert_datetime(
|
|
|
|
|
prepared_value)
|
|
|
|
|
|
|
|
|
|
@ -1169,7 +1165,7 @@ class WhooshSearchQuery(BaseSearchQuery):
|
|
|
|
|
|
|
|
|
|
pv = self.backend._from_python(possible_value)
|
|
|
|
|
|
|
|
|
|
if is_datetime:
|
|
|
|
|
if is_datetime is True:
|
|
|
|
|
pv = self._convert_datetime(pv)
|
|
|
|
|
|
|
|
|
|
if isinstance(pv, six.string_types) and not is_datetime:
|
|
|
|
|
@ -1196,7 +1192,7 @@ class WhooshSearchQuery(BaseSearchQuery):
|
|
|
|
|
prepared_value = Exact(prepared_value).prepare(self)
|
|
|
|
|
query_frag = filter_types[filter_type] % prepared_value
|
|
|
|
|
else:
|
|
|
|
|
if is_datetime:
|
|
|
|
|
if is_datetime is True:
|
|
|
|
|
prepared_value = self._convert_datetime(prepared_value)
|
|
|
|
|
|
|
|
|
|
query_frag = filter_types[filter_type] % prepared_value
|
|
|
|
|
|