I've read the getting started documentation and several other examples on the web. And this is what my search_indexes.py looks like:
我已经在网上阅读了入门文档和其他几个例子。这就是我的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)
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.
但是当我搜索时,我没有得到任何结果。奇怪的是,如果我使用搜索短语'a'或任何其他单个字母,我会得到看起来像该死的东西中的每一个条目。
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:
和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 %}
1 个解决方案
#1
11
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
搜索/索引/新闻/ entry_text.txt
{{ object.headline }}
{{ object.subheadline }}
{{ object.get_author }}
{{ object.get_email }}
{{ object.category.name }}
{{ object.tags }}
{{ object.content }}
Beautiul, it works.
Beautiul,它的工作原理。
#1
11
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
搜索/索引/新闻/ entry_text.txt
{{ object.headline }}
{{ object.subheadline }}
{{ object.get_author }}
{{ object.get_email }}
{{ object.category.name }}
{{ object.tags }}
{{ object.content }}
Beautiul, it works.
Beautiul,它的工作原理。