Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSCAR_SEARCH_FACETS for filtering product lists

I am working on an ecommerce website using django oscar. Products are listed and I have to apply filters (e.g. year, price, etc.). Can we use oscar facet for this filtering functionality?

I have tried adding following as per oscar documentation, but don't know how to make it work and render on my front end.

OSCAR_SEARCH_FACETS = {
'fields': OrderedDict([
    ('product_class', {'name': _('Type'), 'field': 'product_class'}),
    ('rating', {'name': _('Rating'), 'field': 'rating'}),
]),
'queries': OrderedDict([
    ('price_range',
     {
         'name': _('Price range'),
         'field': 'price',
         'queries': [
             # This is a list of (name, query) tuples where the name will
             # be displayed on the front-end.
             (_('0 to 20'), u'[0 TO 20]'),
             (_('20 to 40'), u'[20 TO 40]'),
             (_('40 to 60'), u'[40 TO 60]'),
             (_('60+'), u'[60 TO *]'),
         ]
     }),
])
}

Is it even possible to use this for filtering of products, or is there some other way?

like image 923
Kishan Mehta Avatar asked Sep 22 '15 07:09

Kishan Mehta


1 Answers

Simple Answer

Yes, you can use oscar to filter products based on various search facets. You will have to use a search backend other than haystack's simple backend.

The facets that you have mentioned in the code in your question is what is available by default. Oscar offers more fields and queries that can be displayed as search filters. If you want fields that are custom to your application and not indexed by oscar by default(ex. custom product attributes), check out the detailed answer.

Detailed Answer

On the latest version of oscar, this is how you can add additional filters such as year, brand, occasion which are not present in oscar by default.

Search Backend

First, use a search backend other than Haystack's simple backend. Django oscar officially supports Apache Solr(Documentation here). If you are using the latest version of Solr, you might have to make some changes to schema.xml and solrconfig.xml

Indexing Fields

Certain fields such as price of the product or product_class are automatically indexed by Django Oscar. If you want to index other fields such as product attributes, you will have to edit oscar search app's search_indexes.py to specify the new fields to index. The recommended way to fork oscar's search app to specify your own search_indexes.py. Don't forget to reindex the products after you had added new fields to be indexed.

Specify Search Facets The OSCAR_SEARCH_FACETS setting in settings.py can be used to specify what facets to display on the front end. Specify the fields and queries for OSCAR_SEARCH_FACETS, restart your server and you will see the new facets on the front end.

like image 53
warun26 Avatar answered Sep 20 '22 04:09

warun26