问题
I want to show rating and price range in facet search however the rating is not displayed and price range is displayed but is disabled. What else should i have to do to show them in facet search? This is the source code of django-oscar.
Here is the configuration
FURNITURE_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 20000'), u'[0 TO 20000]'),
(_('20000 to 40000'), u'[20000 TO 40000]'),
(_('40000 to 60000'), u'[40000 TO 60000]'),
(_('60000+'), u'[60000 TO *]'),
]
}),
]),
}
class ProductIndex(indexes.SearchIndex, indexes.Indexable):
# Search text
text = indexes.CharField(
document=True, use_template=True,
template_name='search/indexes/product/item_text.txt')
name = indexes.EdgeNgramField(model_attr='name', null=True)
name_exact = indexes.CharField(model_attr='name', null=True, indexed=False)
# Fields for faceting
product_class = indexes.CharField(null=True, faceted=True)
category = indexes.MultiValueField(null=True, faceted=True)
price = indexes.FloatField(null=True, faceted=True)
num_in_stock = indexes.IntegerField(null=True, faceted=True)
rating = indexes.IntegerField(null=True, faceted=True)
# Spelling suggestions
suggestions = indexes.FacetCharField()
date_created = indexes.DateTimeField(model_attr='created_at')
date_updated = indexes.DateTimeField(model_attr='updated_at')
_strategy = None
def get_model(self):
return get_model('catalogue', 'Product')
def index_queryset(self, using=None):
# Only index browsable products (not each individual child product)
return self.get_model().browsable.order_by('-date_updated')
def read_queryset(self, using=None):
return self.get_model().browsable.base_queryset()
def prepare_product_class(self, obj):
return obj.get_product_class().name
def prepare_category(self, obj):
categories = obj.categories.all()
if len(categories) > 0:
return [category.full_name for category in categories]
def prepare_rating(self, obj):
if obj.rating is not None:
return int(obj.rating)
# Pricing and stock is tricky as it can vary per customer. However, the
# most common case is for customers to see the same prices and stock levels
# and so we implement that case here.
def get_strategy(self):
if not self._strategy:
self._strategy = Selector().strategy()
return self._strategy
def prepare_price(self, obj):
strategy = self.get_strategy()
result = None
if obj.is_parent:
result = strategy.fetch_for_parent(obj)
elif obj.has_stockrecords:
result = strategy.fetch_for_product(obj)
if result:
if result.price.is_tax_known:
return result.price.incl_tax
return result.price.excl_tax
def prepare_num_in_stock(self, obj):
strategy = self.get_strategy()
if obj.is_parent:
# Don't return a stock level for parent products
return None
elif obj.has_stockrecords:
result = strategy.fetch_for_product(obj)
return result.stockrecord.net_stock_level
def prepare(self, obj):
prepared_data = super(ProductIndex, self).prepare(obj)
# We use Haystack's dynamic fields to ensure that the title field used
# for sorting is of type "string'.
if is_solr_supported():
prepared_data['name_s'] = prepared_data['name']
# Use title to for spelling suggestions
prepared_data['suggestions'] = prepared_data['text']
return prepared_data
I have posted the question in https://groups.google.com/forum/#!topic/django-oscar/5Hnf-hMIEdQ but no help yet. I have tried updating the index as well but it is not working yet. Can anyone help me at this, please?
来源:https://stackoverflow.com/questions/49091266/show-price-and-rating-in-facet-search