尽管设置了所有要求,为什么django中的haystack弹性搜索不起作用

时间:2017-09-20 17:05:46

标签: django elasticsearch django-haystack

在这里,我完成了haystack官方文档中给出的所有说明,但仍然无法获取结果。

我使用的是django版本1.8.1和haystack 2.4.1以及elasticsearch 2.4.0

这是我的代码:

views.py

from haystack.query import SearchQuerySet

def search1(request):
     result=SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text',''))

     return render_to_response('ajax_search.html',{"result":result})

model.py

class vastu(models.Model):
   vastu_key = models.ForeignKey(product_type)
   fetchall_key = models.ForeignKey(d_auth)
   title = models.CharField(verbose_name='Title', max_length=25)
   brand = models.CharField(verbose_name='Brand:', max_length=25, blank=True, null=True)
   image = models.ImageField(upload_to=upload_location1, null=True, blank=True,
   height_field="height_field", width_field="width_field")
   height_field = models.IntegerField(default=0)
   width_field = models.IntegerField(default=0)

   price = models.IntegerField(verbose_name='Price:', max_length=20)
   desc = models.TextField(verbose_name='Description', blank=True, max_length=500)
  dprice = models.IntegerField(verbose_name='Discount Price:', null=True, blank=True, max_length=20)
  bookmark = models.IntegerField(null=True, blank=True, max_length=02)

def __str__(self):
    return self.vastu_key

class Meta:
    ordering = [ 'desc','brand','title']

search_indexes.py

import datetime
from haystack import indexes
from dashboard.models import vastu

class vastuIndex(indexes.SearchIndex,indexes.Indexable):
    text = indexes.CharField(document=True,use_template=True)
    content_auto = indexes.EdgeNgramField(model_attr='title')

def get_model(self):
    return vastu

def index_queryset(self, using=None):
    return self.get_model().objects.all()

文本文件路径

/dashboard/templates/search/indexes/dashboard/vastu_text.txt

vast_text.txt

{{ object.title }}
{{ object.brand }}

search.html 位置

/home/mayuresh/shopcon/dashboard/templates/search/search.html

search.html

   {% extends 'base.html' %}

{% block content %}
<h2>Search</h2>

<form method="get" action="/user/search/">
    <table>
        {{ form.as_table }}
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="submit" value="Search">
            </td>
        </tr>
    </table>

    {% if query %}
        <h3>Results</h3>

        {% for result in page.object_list %}
            <p>
                <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</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 }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                |
                {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
            </div>
        {% endif %}
    {% else %}
        {# Show some example queries to run, maybe query syntax, something else? #}
    {% endif %}
</form>
      {% endblock %}

ajax_search.html

{% load static from staticfiles %}

{% if check == None %}

<ul class="col-md-5 nav nav-pills nav-stacked search-results">

<li> Type Something... </li>

 </ul>



 {% else %}
 {% if result > 0 %}

<ul class="col-md-5 nav nav-pills nav-stacked search-results">
{% for obj in result %}


        <div style="display:inline-block; text-decoration:none;">
            <h4>{{ obj.object.title|title }}</h4>
            <p style="margin-top:-10px; color:#444444">  {{obj.object.brand|title}}</p>
        </div>
 <li> he he he </li>


{% endfor %}





  </ul>


{% else %}

  <ul class="col-md-5 nav nav-pills nav-stacked search-results">

   <a><li> not found </li></a>

  </ul>
   {% endif %}
  {% endif %}

settings.py

HAYSTACK_CONNECTIONS = {
'default': {
    'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
    'URL': 'http://127.0.0.1:9200/',
    'INDEX_NAME': 'haystack',
},
  }

已安装的应用中添加了所有干草堆。

在urls.py中

                       url(r'^search/',include('haystack.urls')),

我们在urls.py中的搜索网址是

    url(r'^search/$', 'userapp.views.search1', name='search'),

根据文档的说法,我在点击搜索网址时完成了所有安装以及elasticsearch正在运行。

仍然没有找到模板中的结果,所有的ajax设置工作正常,之前我用它来搜索基本搜索。

请帮助解决问题。

欢迎任何建议。

非常感谢你。

0 个答案:

没有答案