【发布时间】:2014-09-25 21:00:24
【问题描述】:
我多次关注多个不同的示例,但似乎我错过了一些东西。 --> 简而言之,使用嗖嗖声或弹性搜索以及 haystack,搜索结果一无所获。
型号
class Note(models.Model):
user = models.ForeignKey(User)
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField()
def __unicode__(self):
return self.title
表格
class NotesSearchForm(SearchForm):
def no_query_found(self):
return self.searchqueryset.all()
def search(self):
# First, store the SearchQuerySet received from other processing. (the main work is run internally by Haystack here).
sqs = super(NotesSearchForm, self).search()
# if something goes wrong
if not self.is_valid():
return self.no_query_found()
# you can then adjust the search results and ask for instance to order the results by title
sqs = sqs.order_by(title)
return sqs
观看次数
def search(request):
# we retrieve the query to display it in the template
form = NotesSearchForm(request.GET)
# we call the search method from the NotesSearchForm. Haystack do the work!
results = form.search()
return render(request, 'search.html', {'search_query' : search_query,
'notes' : results,
})
模板
<!-- EXTENDS BASE
================================================== -->
{% extends 'base.html' %}
<!-- NAVBAR
================================================== -->
{% block nav %}
{% include 'nav1.html' %}
{% endblock %}
<!-- Marketing messaging and featurettes
================================================== -->
<!-- Wrap the rest of the page in another container to center all the content. -->
{% block marketing %}
<div class="container marketing">
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<div class="col-md-7">
<h2>Search</h2>
<form type="get" action="/search/">
<input type="text" name="q">
<button type="submit">Search</button>
</form>
<p>Your query "{{ search_query }} has returned {{ notes.count }} result{{ notes|pluralize }}"</p>
{% for note in notes %}
<h1>{{ note.title }}</h1>
<p>
{{ note.body }}
</p>
{% endfor %}
</div>
</div>
<hr class="featurette-divider">
<!-- /END THE FEATURETTES -->
</div><!-- /.container -->
{% endblock %}
我知道这有点长,但我似乎无法弄清楚出了什么问题。搜索页面正在工作,但搜索文本时未找到结果。
我想在文本表单中输入一个“search_text”,并让它返回所有在数据库中的文档中出现“search_text”的实例,并且前后带有一定数量的标记
|标题 |用户 | {前面的标记数} 'search_text' {后面的标记数}
【问题讨论】:
标签: django search elasticsearch whoosh