[英]Django-Haystack + Whoosh - Are misspelling suggestions possible?
[英]django-haystack: how to access the spelling suggestions on the template?
我正在嘗試使用Solr和HAYSTACK_INCLUDE_SPELLING = True
django-haystack。
您如何訪問模板上的拼寫建議(由默認SearchView
生成)?
編輯:另一個問題:拼寫建議可以從數據庫中找到單詞嗎? 例如,使用干草堆文檔中的默認Note
模型和默認的SearchView
,當數據庫包含名為“ Lorem ipsum”的注釋時,當我搜索單詞“ Lorm”時,沒有拼寫建議。 正常嗎?
謝謝 :-)
如果您在模板中設置了搜索查詢,則可以執行以下操作:
{{ sqs.spelling_suggestion }}
查看: http : //docs.haystacksearch.org/dev/searchqueryset_api.html#spelling-suggestion
更多細節。
為了讓干草堆查找拼寫建議,搜索模板應包括您要查找的字段。 因此,如果您的搜索模板包含{{object.title}},則應該選擇拼寫建議。
也許你忘了做
python manage.py update_index
在添加lorem注釋之后。
如果您使用的是默認SearchView
則{{ suggestion }}
就足夠了。
看到:
https://github.com/toastdriven/django-haystack/blob/master/haystack/views.py#L118
...以查看模板上下文中可用的內容。
Hassan正確地說您必須更新/重建索引,並且在搜索模板中需要正確的字段。
我發現上述解決方案僅在我們擴展haystack.views.SearchView
。 基於新的基於通用視圖的SearchView,即haystack.generic_views.SearchView
似乎沒有在上下文中添加suggestion
字段,因此我們將無法訪問此上下文變量{{ suggestion }}
。
我們將需要手動將其添加到上下文中,如下所示:
from haystack.generic_views import SearchView
# Custom view.
class MySearchView(SearchView):
template_name = 'search/search.html'
queryset = SearchQuerySet().all()
form_class = SearchForm
def get_context_data(self, *args, **kwargs):
context = super(MySearchView, self).get_context_data(*args, **kwargs)
# Add any additional context data we need to display in forms
spell_suggestion = self.get_form().get_suggestion()
context['spell_suggestion'] = spell_suggestion
return context
將視圖添加到urls.py
urlpatterns = [
path('search/', MySearchView.as_view(), name='haystack_search'),
然后在search.html
以{{ spell_suggestion }}
訪問上下文變量
希望這可以幫助 :)
參考: https : //django-haystack.readthedocs.io/en/v2.4.1/views_and_forms.html#upgrading
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.