Django-haystack全文搜索工作,但方面没有

时间:2014-02-09 18:13:32

标签: ajax django full-text-search django-haystack facets

使用django-haystack和elasticsearch。全文搜索工作正常,但我有 实施方面的麻烦。我在网上搜索了教程,但没有成功,换句话说 不明白他们中的任何一个。我是编程的初学者,所以一些帮助它将不胜感激。 先感谢您。抱歉我的英语如果不好,那不是我的主要语言。 这是我正在进行的全文搜索。

articles.models.py

class Article(models.Model):
    category =      models.CharField(max_length=60)
    subcategory =   models.CharField(max_length=100)
    name =          models.CharField(max_length=255)
    price =         models.DecimalField(max_digits=8, decimal_places=2)
    pub_date =      models.DateTimeField(auto_now_add=True)
    country =       models.CharField(max_length=60)
    city =          models.CharField(max_length=60)
    # other fields ...

12个类别(车辆和零件,计算机和零件......)

车辆及零件子类别(汽车,卡车,自行车,零件......) 我在提交文章表格时用javascript解决了这个问题,国家和城市也一样。

articles.search_indexes.py

class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
    text =          indexes.CharField(document=True, use_template=True)
    subcategory =   indexes.CharField(model_attr='subcategory', faceted=True)
    price =         indexes.DecimalField(model_attr='price', faceted=True)
    pub_date =      indexes.DateTimeField(model_attr='pub_date', faceted=True)
    country =       indexes.CharField(model_attr='country', faceted=True)
    city =          indexes.CharField(model_attr='city', faceted=True)

    content_auto = indexes.EdgeNgramField(model_attr='name')

    def get_model(self):
        return Article

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

articles_text

{{ object.name }}
{{ object.subcategory }}
{{ object.price }}
{{ object.pub_date }}
{{ object.country }}
{{ object.city }}

articles.views.py

def searchArticles(request):
    articles = SearchQuerySet().autocomplete(content_auto=request.POST.get('search_text', ''))
    return render_to_response('ajax_search.html', {'articles': articles})

base.html文件

    {% csrf_token %}
    <input type="text" id="search" class="edo-trazi" name="search" />
    <ul id="search-results">
    </ul>

ajax_search.html

{% if articles.count > 0 %}
{% for article in articles %}
<li class="edo-trazi-artikal"><img class="edo-trazi-slika" src="/static/aktiva/{{ artikal.object.slika }}"/>
<a class="edo-trazi-ime" href="/artikli/prikazi/{{ artikal.object.id }}/{{ artikal.object.slug }}/">{{ artikal.object.name }}</a>   
{% endfor %}
{% else %}
<li>No results!</li>
{% endif %}

ajax.js

$(function(){

$('#search').keyup(function() {

    $.ajax({
        type: "POST",
        url: "/search/",
        data: { 
            'search_text' : $('#search').val(),
            'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
        },
        success: searchSuccess,
        dataType: 'html'
    });

});

});

function searchSuccess(data, textStatus, jqXHR)
{
$('#search-results').html(data);
}    

project.urls.py

url(r'^search/$', 'articles.views.searchArticles'),
url(r'^vehicles-parts/', include('vehiclesParts.urls')),

以上示例代码运行正常,如果有人对自动填充感兴趣我可以指导 他就像我所知道的那样。

vehiclesParts.urls.py

url(r'^$', 'vehiclesParts.views.vehiclesPartsView', name='vehiclesParts'),
url(r'^search/$', 'vehiclesParts.views.searchVehiclesParts'),

vehiclesParts.views.py

def vehiclesPartsView(request):
    return render_to_response('vehiclesParts.html', context_instance=RequestContext(request))

def searchVehiclesParts(request):
    articles = SearchQuerySet().facet('subcategory').facet('price').facet('pub_date').facet('country').facet('city')
    # how to make this code run in similar way like above autocomplete(with ajax), also to 
    # filter results by category "filter (category= 'Vehicles and Parts')", because this
    # advanced search I'll have to do for each category to display and search
    # articles only for that category.
    return render_to_response('ajax-vehiclesParts.html', {'articles': articles})

ajax-vehiclesParts.html - 可以和ajax_search.html相同我只会添加一些额外的字段。 vehiclesParts.html - 如何在模板和选定的子类别或其他内容中添加构面以通过Ajax在ajax-vehiclesParts.html中显示结果?此外,如果可以记住选定的子类别,那么如果选择“城市伦敦”或其他东西,只显示该子类别的结果。

ajax-vehiclesParts.js ?????

0 个答案:

没有答案