搜索django Haystack

时间:2014-08-03 01:46:26

标签: django search elasticsearch whoosh

我已经多次跟踪了多个不同的例子,但似乎我遗漏了一些东西。 - >简而言之,无论是嗖嗖声还是弹性搜索以及干草堆,搜索结果都没有。

模型

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 %}

我知道这有点长,但我似乎无法弄清楚出了什么问题。搜索页面正在运行,但搜索文本时未找到任何结果。

我想输入一个&#39; search_text&#39;在文本表单中,并返回其中&#39; search_text&#39;出现在数据库中的文档中,在此之前和之后都有一定数量的令牌

|标题|用户| {之前的令牌数}&#39; search_text&#39; {跟随令牌的数量}

1 个答案:

答案 0 :(得分:0)

为什么它不起作用的原因是我没有在我的机器上运行弹性搜索。因此,通过在machin上安装和运行elasticsearch服务器,我能够获得搜索结果。可以在此处找到说明:http://chriskief.com/2014/05/14/django-haystack-and-elasticsearch-part-1/

另外,运行搜索需要pyelasticsearch和elasticsearch。