I use django-oscar + Solr + haystack.

*===============================================================================================================* | id | partner_sku|price_currency|price_excl_tax|num_in_stock|date_created|date_updated|partner_id|product_id| |"10451"|"S0010436" | "USD" | 74.00 | 20 |'some_date' |'some_date' | 1 | 5992 | |"10452"|"S0010436" | "USD" | 80.00 | 0 |'some_date' |'some_date' | 2 | 5992 | *===============================================================================================================* 

I want to index only the rows where partner_id = 2. index_queryset below does not index as I want, since all partners continue to be indexed.

 class ProductIndexes(CelerySearchIndex, indexes.Indexable): text = indexes.EdgeNgramField( document=True, use_template=True, template_name='search/indexes/cpu/item_text.txt') upc = indexes.CharField(model_attr="upc", null=True) title = indexes.CharField(model_attr='title', null=True) # Fields for faceting product_class = indexes.CharField(null=True, faceted=True) category = indexes.MultiValueField(null=True, faceted=True) partner = indexes.MultiValueField(null=True, faceted=True) price = indexes.FloatField(null=True, faceted=True) vendor = indexes.CharField(null=True, faceted=True) rating = indexes.IntegerField(null=True, faceted=True) num_in_stock = indexes.BooleanField(null=True, faceted=True) # Spelling suggestions suggestions = indexes.FacetCharField() date_created = indexes.DateTimeField(model_attr='date_created') date_updated = indexes.DateTimeField(model_attr='date_updated') def get_model(self): return get_model('catalogue', 'Product') def index_queryset(self, using=None): return self.get_model().objects.filter(stockrecords__partner_id=2).order_by('-num_in_stock') 

How to make the indexing I need?

python manage.py rebuild_index did not forget to run

    1 answer 1

    The final correct code should look like this (see comments in the code):

     class ProductIndexes(CelerySearchIndex, indexes.Indexable): text = indexes.EdgeNgramField( document=True, use_template=True, template_name='search/indexes/cpu/item_text.txt') upc = indexes.CharField(model_attr="upc", null=True) title = indexes.CharField(model_attr='title', null=True) # ДОБАВЛЕНО НОВОЕ ПОЛЕ local_stock = indexes.IntegerField(null=True, faceted=False) # Fields for faceting product_class = indexes.CharField(null=True, faceted=True) category = indexes.MultiValueField(null=True, faceted=True) partner = indexes.MultiValueField(null=True, faceted=True) price = indexes.FloatField(null=True, faceted=True) vendor = indexes.CharField(null=True, faceted=True) rating = indexes.IntegerField(null=True, faceted=True) num_in_stock = indexes.BooleanField(null=True, faceted=True) # Spelling suggestions suggestions = indexes.FacetCharField() date_created = indexes.DateTimeField(model_attr='date_created') date_updated = indexes.DateTimeField(model_attr='date_updated') def get_model(self): return get_model('catalogue', 'Product') # МЕТОД ИЗМЕНЕН def index_queryset(self, using=None): return self.get_model().browsable.order_by('-date_updated') # ДОБАВЛЕН НОВЫЙ МЕТОД def prepare_local_stock(self, obj): local_stock = StockRecord.objects.filter(partner_id=2, product=obj) if local_stock.exists(): local_stock = local_stock[0].num_in_stock else: local_stock = 0 return int(local_stock) 

    In the file search_handler.py (refers to django-oscar , is responsible for the withdrawal of goods in the category) you need to specify that the sorting goes across the local_stock field, i.e we register return sqs.order_by('-local_stock')