簡體   English   中英

用django Haystack搜索

[英]Search with django Haystack

我在多個不同的時間跟隨了多個不同的示例,但是似乎我一直都缺少一些東西。 ->簡而言之,將whoosh或elasticsearch與干草堆一起使用,搜索結果將一無所獲。

模型

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'{之后的令牌數量}

之所以不起作用,是因為我的機器上沒有運行Elasticsearch。 因此,通過在機器上安裝並運行elasticsearch服務器,我可以獲得搜索結果。 可以在這里找到說明: http : //chriskief.com/2014/05/14/django-haystack-and-elasticsearch-part-1/

需要額外的pyelasticsearch和elasticsearch來運行搜索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM