私のWebアプリケーションでは、Elsesearchを使用してDjango Haystackを検索エンジンとして使用しています。Djangoのhaystack/elasticsearchで正確なフレーズを強調表示
My SearchForm
検索クエリに引用符付きのトークンが含まれている場合、完全検索用の子クラスフィルタ(content__exact
パラメータ)。私が見ていることは、私は引用符で検索を行う際に、ということである
{% highlight result.object.<field> with query %}
:私はのように、私のアプリで検索用語を強調するためにDjangoテンプレートタグ{% highlight %}
を使用してい
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
複数の単語をスペースで区切って指定すると、"História de fratura"
などの検索結果が表示され、トークン"de"
のみが強調表示されます。したがって、ハイライタークラスでは、引用符付きの用語を単一のトークンとして扱い、検索結果で強調表示することはできません。
検索結果の引用符で囲まれたキーワード全体を強調表示するにはどうすればよいですか?