Django Haystack 搜索中没有结果

发布于 2024-09-25 21:44:22 字数 2929 浏览 1 评论 0原文

我已经阅读了入门文档和网络上的其他几个示例。 这就是我的 search_indexes.py 的样子:

from haystack.indexes import *
from haystack import site
from models import Entry

class EntryIndex(SearchIndex):
    text = CharField(document=True)
    headline = CharField(model_attr='headline')
    subheadline = CharField(model_attr='subheadline')
    category = CharField(model_attr='category__name')

    author = CharField(model_attr='get_author')
    email = CharField(model_attr='get_email')

    tags = CharField(model_attr='tags')

    content = CharField(model_attr='content')

    def get_queryset(self):
        return Entry.objects.exclude(dt_published=None).order_by('-is_featured', '-dt_published', '-dt_written', 'headline')

site.register(Entry, EntryIndex)

但是当我搜索时,我没有得到任何结果。奇怪的是,如果我使用搜索短语“a”或任何其他单个字母, 我看到了这该死的东西中的每一个条目。

无论如何......在我看来,搜索引擎没有在任何字段中查找。 :/

此行以下的任何内容都不那么相关(它有效,相信我):


我的观点:

from haystack.views import SearchView

class CustomSearchView(SearchView):
    def __name__(self):
        return "CustomSearchView"

    def extra_context(self):
        return common(self.request)

def search(request):
    return CustomSearchView(template='news/search_results.html')(request)

和 search_results.html:

{% extends "content.html" %}
{% load tagging_tags %}
{% load highlight %}


{% block title %}Viðskiptablaðið - Leitarniðurstöður{% endblock %}

{% block left_content %}

<h2>Search</h2>

<form method="get">
    <table>
        {{ form.as_table }}
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="Search">
            </td>
        </tr>
    </table>

    {% if query %}
        <h3>Results</h3>

        {% for result in page.object_list %}
            {% highlight result.summary with request.GET.q %}
            {% highlight result.object.headline with request.GET.q %}
            <p>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.headline }}</a>
            </p>
        {% empty %}
            <p>No results found.</p>
        {% endfor %}

        {% if page.has_previous or page.has_next %}
            <div>
                {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                |
                {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
            </div>
        {% endif %}
    {% else %}
        {# Show some example queries to run, maybe query syntax, something else? #}
    {% endif %}
</form>

{% endblock %}

I've read the getting started documentation and several other examples on the web.
And this is what my search_indexes.py looks like:

from haystack.indexes import *
from haystack import site
from models import Entry

class EntryIndex(SearchIndex):
    text = CharField(document=True)
    headline = CharField(model_attr='headline')
    subheadline = CharField(model_attr='subheadline')
    category = CharField(model_attr='category__name')

    author = CharField(model_attr='get_author')
    email = CharField(model_attr='get_email')

    tags = CharField(model_attr='tags')

    content = CharField(model_attr='content')

    def get_queryset(self):
        return Entry.objects.exclude(dt_published=None).order_by('-is_featured', '-dt_published', '-dt_written', 'headline')

site.register(Entry, EntryIndex)

But when I search, I get no results. Strangely though if I use the search phrase 'a' or any other single letter,
I get what looks like every single entry in the damn thing.

Anyway... It looks to me like the search engine isn't looking in any of the fields. :/

Anything below this line is less relevant (it works, trust me):


My view:

from haystack.views import SearchView

class CustomSearchView(SearchView):
    def __name__(self):
        return "CustomSearchView"

    def extra_context(self):
        return common(self.request)

def search(request):
    return CustomSearchView(template='news/search_results.html')(request)

And search_results.html:

{% extends "content.html" %}
{% load tagging_tags %}
{% load highlight %}


{% block title %}Viðskiptablaðið - Leitarniðurstöður{% endblock %}

{% block left_content %}

<h2>Search</h2>

<form method="get">
    <table>
        {{ form.as_table }}
        <tr>
            <td> </td>
            <td>
                <input type="submit" value="Search">
            </td>
        </tr>
    </table>

    {% if query %}
        <h3>Results</h3>

        {% for result in page.object_list %}
            {% highlight result.summary with request.GET.q %}
            {% highlight result.object.headline with request.GET.q %}
            <p>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.headline }}</a>
            </p>
        {% empty %}
            <p>No results found.</p>
        {% endfor %}

        {% if page.has_previous or page.has_next %}
            <div>
                {% if page.has_previous %}<a href="?q={{ query }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
                |
                {% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}
            </div>
        {% endif %}
    {% else %}
        {# Show some example queries to run, maybe query syntax, something else? #}
    {% endif %}
</form>

{% endblock %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

Saygoodbye 2024-10-02 21:44:22

好吧,文档里有,但我觉得还不够清楚。

你所要做的就是以某种方式声明要搜索的数据(我认为这就是整个要点:

headline = CharField(model_attr='headline')
subheadline = CharField(model_attr='subheadline')

等等......)

好吧,哭够了。

您所要做的就是

text = CharField(document=True, use_template=True)

制作一个模板,在我的例子中:

search/indexes/news/entry_text.txt

{{ object.headline }}
{{ object.subheadline }}
{{ object.get_author }}
{{ object.get_email }}
{{ object.category.name }}
{{ object.tags }}
{{ object.content }}

漂亮,它有效。

ok, it is in the documentation but I feel it's not clear enough.

What you have to do is to declare somehow the data to be searched (i thought that was the whole point of:

headline = CharField(model_attr='headline')
subheadline = CharField(model_attr='subheadline')

etc...)

ok, enough crying.

All you have to do is

text = CharField(document=True, use_template=True)

and then make a template, in my case:

search/indexes/news/entry_text.txt

{{ object.headline }}
{{ object.subheadline }}
{{ object.get_author }}
{{ object.get_email }}
{{ object.category.name }}
{{ object.tags }}
{{ object.content }}

Beautiul, it works.

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