Using django-haystack and elasticsearch. Full text search works fine, but I have a problem for implementing faces. I searched for a textbook on the net, but without success, in other words did not understand any of them. I am starting to program, so some of them will be appreciated. Thanks in advance. Sorry, my English, if this is not good, this is not my main 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)
12 categories (Vehicles and spare parts, computers and spare parts ....)
Subcategories for vehicles and spare parts (automobile, trucks, bicycles, spare parts ....) I decided this using javascript when submitting the article form, the same for the 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 code code works fine, if someone is interested in autocomplete, I can specify it as far 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')
ajax-vehiclesParts.html - May be the same as ajax_search.html. I will just add additional additive fields. vehiclesParts.html - How to add graphs to the template and to the selected subcategory or something else to display the results in ajax-vehiclesParts.html via Ajax? Also, if it is possible to remember the selected subcategory, therefore, if in the next โLondon cityโ or something else is selected, to display the results only for this subcategory.
ajax-vehiclesParts.js ?????