Django Haystack Faceting examples

2019-05-26 07:26发布

问题:

I want to use Django-Haystack-Solr in a site I am working on. I have worked through the examples in the Haystack documentation and have searched the internet extensively for other examples. I am having difficulty making the leap to integrating it in my site. I found http://www.slideshare.net/Nagyman/faceted-navigation-using-django-haystack-and-solr interesting, but fell short of how to pull it all together. If anyone has run across some "robust" Haystack faceting examples, websites that are open-source, or would be willing to share some of your own code please provide links/share code. Thanks for the help.

btw - I want to facet on multiple field from multiple models(tables), and figure out a way to use alternate labels for the facets.

回答1:

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.



回答2:

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 -->