代码之家  ›  专栏  ›  技术社区  ›  demux

Django Haystack搜索没有结果

  •  8
  • demux  · 技术社区  · 14 年前

    这就是我要找的_索引.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)
    

    搜索和_结果.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 %}
    
    1 回复  |  直到 12 年前
        1
  •  12
  •   demux    14 年前

    好的,在文档中,但我觉得不够清楚。

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

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

    你要做的就是

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

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

    美女,这很管用。