Django-haystack full text search working but facets don't
Asked Answered
C

0

3

Using django-haystack and elasticsearch. Full text search is working ok, but I have trouble to implement facets. I've searched tutorial on web but with no success, in other words didn't understand any of them. I am beginner in programing, so some help it will be appreciated. Thank you in advance. Sorry on my english if isn't good, it isn't my primary language. Here is my working full text search.

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 categories (Vehicles and Parts, Computers and Parts ....)

Subcategories for Vehicles and Parts (Car, Trucks, Bikes, Parts ....) I solved this with javascript when submitting form for Article, same for country and city.

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')),

Above example code is running ok, if someone is interested in autocomplete I can guide him as much as I know.

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--Can be same as ajax_search.html I'll just add some additonal fields. vehiclesParts.html--How to add facets in template and on selected subcategory or something else to display results in ajax-vehiclesParts.html via Ajax? Also if posible to remember selected subcategory so if next is selected 'city London' or something else to display results only for that subcategory.

ajax-vehiclesParts.js ?????

Chengtu answered 9/2, 2014 at 18:13 Comment(1)
Do you have a link for repo. I am not solution provider I am learner so we can learn together. Need to see settings in detail.Bailable

© 2022 - 2024 — McMap. All rights reserved.