如何索引那些models \ rows,其中some_field = some_value?

时间:2016-10-22 22:31:12

标签: django solr django-haystack django-oscar

我使用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     |
*===============================================================================================================*

我想仅为那些partner_id=2的行编制索引。下面的index_queryset不能满足我的需求,因为其他合作伙伴也会编入索引。

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')
执行

python manage.py rebuild_index

如何实现我需要的索引?

1 个答案:

答案 0 :(得分:0)

假设这是同一个问题:django oscar github issue 2169 当您尝试过滤num_in_stock BooleanField ????

我建议将其更改为IntegerField并添加`prepare_num_in_stock函数,如下所示:

def prepare_num_in_stock(self, obj)
    # define get_local_stock as you please
    return int(obj.get_local_stock())

希望这有帮助。