Django Haystack Faceting examples

亡梦爱人 提交于 2019-12-06 06:28:02

Here's a recent tutorial I did on the topic. This uses Elastic search instead of solr which I personally believe is easier to implement.

Django Haystack + Elasticsearch + Autocomplete + Faceting Tutorial

I implemented the following demo store site to demonstrate faceting based on multiple selection.

Though not apparent from the image, this tutorial also discusses auto-complete implementation.

But even if you want to use this with Solr, most of the code related to faceting and auto-complete remains the same. So yes, this code is equally applicable if you are using Solr.For solr you just need to change the value of HAYSTACK_CONNECTIONS in your settings file and build Solr index by running manage.py build_solr_schema. The drop the XML output in your Solr’s schema.xml file and restart your Solr server. Nothing else changes in the Python/Django code.

Whoosh implements faceting but Django Haystack is yet to catch up with Whoosh on this development, so stay clear of it if you want to implement faceting.

The code is too large to be shared here but for anyone wanting to dive right into the code, here's the complete source code.

Though Old question but anyway trying to give answer. :) Put something like in your url.conf

sqs = SearchQuerySet().facet('auther') 

+

urlpatterns += patterns('haystack.views',
url(r'^$', FacetedSearchView(form_class=FacetedSearchForm, searchqueryset=sqs), name='haystack_search'),

)

You should have facets defined in the app1.

title = indexes.CharField(model_attr='title',faceted=True,null=True)  

And template should be something like following.

         <!-- Begin faceting. -->
<div>
    <dl>


        {% if facets.fields.wish_text %}
           {% for author in facets.fields.title  %}
                <dd><a href="{{ request.get_full_path }}&amp;selected_facets=author_exact:{{ author.0|urlencode }}">{{ author.0 }}</a> ({{ author.1 }})</dd>
            {% endfor %}
        {% else %}
            <p>No author facets.</p>
        {% endif %}
    </dl>

</div>
<!-- End faceting -->
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!