代码之家  ›  专栏  ›  技术社区  ›  alexrogo

如何使用带有自定义属性的django haystack搜索方面?

  •  0
  • alexrogo  · 技术社区  · 7 年前

    我有一个Django奥斯卡商店,我成功地安装了solr4.7.2作为搜索引擎。它可以很好地处理预定义的属性,例如upc、title、product\u class。。。

    但对附加属性的过滤不起作用。

    这是我的目录/models.py:

    class Product(AbstractProduct):
        video_url = models.URLField()
        co2 = models.IntegerField()
        is_public = models.BooleanField()
        test = models.TextField()
    

    co2 = indexes.IntegerField(model_attr="co2", null=True, indexed=False)
    
    def prepare_co2(self, obj):
            return self.apps.get_model().objects.filter(co2="2")
           # return obj.co2 etc. here I tried a lot of code, but didnt work
    

    我还尝试复制这个函数的现成代码。

    0 回复  |  直到 7 年前
        1
  •  2
  •   shad0w_wa1k3r    7 年前

    不能从prepare函数中过滤对象,只需指定haystack如何访问object字段。

    from haystack import indexes
    import oscar.apps.search.search_indexes as oscar_search_indexes
    
    
    class ProductIndex(oscar_search_indexes.ProductIndex):
        co2 = indexes.IntegerField(null=False, indexed=True)
    
        def prepare_co2(self, obj):
            return obj.co2
    

    上述方法应该有效(在更新Solr schema.xml后重新为产品编制索引),如果无效,则使用获得的错误或示例数据的意外查询行为更新问题。

        2
  •  0
  •   alexrogo    5 年前

    我的django奥斯卡版本有问题。我更新了它,现在我可以成功地重新索引schema.xml。