3

使用 django-haystack 和 elasticsearch。全文搜索工作正常,但我无法实现方面。我已经在网上搜索了教程,但没有成功,换句话说,没有理解其中的任何一个。我是编程的初学者,所以一些帮助将不胜感激。先感谢您。对不起,如果我的英语不好,那不是我的主要语言。这是我的工作全文搜索。

文章模型.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 解决了这个问题,国家和城市也是如此。

文章.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()

文章文本

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

文章.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);
}    

项目.urls.py

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

上面的示例代码运行正常,如果有人对自动完成感兴趣,我可以尽可能多地指导他。

车辆零件.urls.py

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

车辆零件.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 一样,我只是添加一些附加字段。VehicleParts.html--如何在模板和选定的子类别或其他内容中添加方面以通过 Ajax 在 ajax-vehiclesParts.html 中显示结果?此外,如果可以记住选定的子类别,那么如果下一个选择“伦敦城市”或其他仅显示该子类别的结果。

ajax-vehiclesParts.js ?????

4

0 回答 0