我的网络应用程序使用Django Haystack和Elasticsearch作为搜索引擎。
如果搜索查询包含带引号的令牌,则我的SearchForm
子类会过滤精确搜索(content__exact
参数)。
class NepSearchForm(SearchForm):
# ...
def search(self):
if not self.is_valid():
return self.no_query_found()
if not self.cleaned_data.get('q'):
return self.no_query_found()
sqs = self._parse_query(self.cleaned_data['q'])
if self.load_all:
sqs = sqs.load_all()
return sqs
def no_query_found(self):
return self.searchqueryset.all()
def _parse_query(self, query):
"""
Parse query treating modifiers 'AND', 'OR', 'NOT' to make what they're
supposed to.
:param query: query entered in search input box in form
:param sqs: SearchQuerySet until now
:return: SearchQuerySet object
"""
words = iter(shlex.split(query))
result = self.searchqueryset
for word in words:
try:
if word == 'AND':
result = result.filter_and(content=words.__next__())
elif word == 'OR':
# TODO: fail when changing order of the words. See
# TODO: functional test:
# TODO: test_search_with_OR_modifier_returns_correct_objects
result = result.filter_or(content=words.__next__())
elif word == 'NOT':
result = result.exclude(content=words.__next__())
# if "word" is compounded of more than one non blank word the
# term is inside quotes
elif len(word.split()) > 1:
result = result.filter(content__exact=word)
else:
result = result.filter(content=word)
except StopIteration:
return result
return result
我使用Django模板标记{% highlight %}
来突出显示在我的应用中搜索的字词,例如:
{% highlight result.object.<field> with query %}
我所看到的是,当我使用带有多个单词的引号进行搜索时,以空格分隔,例如"História de fratura"
,搜索结果只显示标记"de"
突出显示。因此,似乎Highlighter类不会将带引号的术语视为单个标记,以便在搜索结果中突出显示它们。
如何使用搜索结果中的引号内的整个术语来突出显示查询?