django-haystack:如何访问模板上的拼写建议?

发布于 2024-08-31 04:04:00 字数 305 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咋地 2024-09-07 04:04:00

如果您在模板中设置了搜索查询,您可以执行以下操作:

{{ sqs.spelling_suggestion }}

查看:
http://docs.haystacksearch.org/dev/searchqueryset_api.html#spelling-suggestion

了解更多详情。

为了让 haystack 找到拼写建议,搜索模板应包含您要查找的字段。因此,如果您的搜索模板包含 {{ object.title }},您应该选择拼写建议。

忘记了。

python manage.py update_index

也许您在添加 lorem 注释后

If you have the search query set in the template, you can do:

{{ sqs.spelling_suggestion }}

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

python manage.py update_index

after you added the lorem note.

蘸点软妹酱 2024-09-07 04:04:00

如果您使用默认的 SearchView{{ suggest }} 就足够了。

请参阅:

https://github.com/toastdriven/django -haystack/blob/master/haystack/views.py#L118

...查看模板上下文中可用的内容。

哈桑正确地指出,您必须更新/重建索引,并且您的搜索模板中需要正确的字段。

{{ suggestion }} should suffice if you are using the default SearchView.

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.

朦胧时间 2024-09-07 04:04:00

我发现上述解决方案仅在我们扩展 haystack.views.SearchView 时才有效。基于新通用视图的 SearchView 即 haystack.generic_views.SearchView 似乎没有将 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 中访问上下文变量 {{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.e haystack.generic_views.SearchView do not seem to be adding the suggestion 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:

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

Add the view to urls.py

urlpatterns = [
    path('search/', MySearchView.as_view(), name='haystack_search'),

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文