django-haystack:如何访问模板上的拼写建议?
我正在尝试使用 Solr 和 HAYSTACK_INCLUDE_SPELLING = True
来使用 django-haystack。
如何访问模板上的拼写建议(由默认 SearchView
生成)?
编辑:另一个问题:拼写建议可以从数据库中找到单词吗?例如,使用 haystack 文档中的默认 Note
模型和默认的 SearchView
,当我搜索单词“Lorm”时,当数据库包含注释称为“Lorem ipsum”。正常吗?
谢谢 :-)
I'm trying django-haystack with Solr and HAYSTACK_INCLUDE_SPELLING = True
.
How do you access the spelling suggestions on the template (generated by the default SearchView
) ?
Edit: another question: Can the Spelling Suggestion find words from the database ? For example, using the default Note
model from the haystack doc, and the default SearchView
, there is no spelling suggestions when I search the word "Lorm" when the database contains a note called "Lorem ipsum". Is it normal ?
Thanks :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在模板中设置了搜索查询,您可以执行以下操作:
查看:
http://docs.haystacksearch.org/dev/searchqueryset_api.html#spelling-suggestion
了解更多详情。
为了让 haystack 找到拼写建议,搜索模板应包含您要查找的字段。因此,如果您的搜索模板包含 {{ object.title }},您应该选择拼写建议。
忘记了。
也许您在添加 lorem 注释后
If you have the search query set in the template, you can do:
Look at:
http://docs.haystacksearch.org/dev/searchqueryset_api.html#spelling-suggestion
for more details.
For haystack to find the spelling suggestions, the search template should include the field you are looking for. So if you search template includes {{ object.title }} you should be picking up the spelling suggestion.
Maybe you forgot to do
after you added the lorem note.
如果您使用默认的
SearchView
,{{ suggest }}
就足够了。请参阅:
https://github.com/toastdriven/django -haystack/blob/master/haystack/views.py#L118
...查看模板上下文中可用的内容。
哈桑正确地指出,您必须更新/重建索引,并且您的搜索模板中需要正确的字段。
{{ suggestion }}
should suffice if you are using the defaultSearchView
.See:
https://github.com/toastdriven/django-haystack/blob/master/haystack/views.py#L118
...to see what is available in the template context.
Hassan is correct in stating that you must update/rebuild your index and that you need the right fields in your search template.
我发现上述解决方案仅在我们扩展 haystack.views.SearchView 时才有效。基于新通用视图的 SearchView 即
haystack.generic_views.SearchView
似乎没有将suggestion
字段添加到上下文中,我们将无法访问此上下文变量<代码>{{建议}}。我们需要手动将其添加到上下文中,如下所示:
将视图添加到 urls.py
然后在
search.html
中访问上下文变量{{pell_suggestion }}
希望这有帮助:)
参考:https://django- haystack.readthedocs.io/en/v2.4.1/views_and_forms.html#upgrading
I found that the above mentioned solutions only work if we are extending the
haystack.views.SearchView
. The new generic views based SearchView i.ehaystack.generic_views.SearchView
do not seem to be adding thesuggestion
field to the context and we will not be able to access this context variable{{ suggestion }}
.We will need to manually add it to the context as follows:
Add the view to urls.py
And then in the
search.html
access the context variable as{{ spell_suggestion }}
Hope this helps :)
Reference: https://django-haystack.readthedocs.io/en/v2.4.1/views_and_forms.html#upgrading