【问题标题】:Search with django Haystack使用 django Haystack 搜索
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 2013-02-12
      • 2014-07-17
      • 2012-12-29
      • 1970-01-01
      • 2012-05-30
      • 2017-02-04
      相关资源
      最近更新 更多