T
T
TitanFighter2016-11-20 21:41:28
Django
TitanFighter, 2016-11-20 21:41:28

How to index only those models\rows where some_field=some_value?

I am using 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 rows where partner_id=2. The `index_queryset` below does not index as I want, since all peers 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 do I do the indexing I need?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abdulla Mursalov, 2016-11-21
@amaprograma

python manage.py rebuild_index

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question