Django Haystack在模型类型上的分面

时间:2011-07-24 21:19:51

标签: django-haystack facet

我想根据返回的不同model_names(类)来研究结果。有一个简单的方法吗?

3 个答案:

答案 0 :(得分:5)

您是否尝试使用此信息添加SearchIndex字段? E.g。

class NoteIndex(SearchIndex, indexes.Indexable):
    title = CharField(model_attr='title')
    facet_model_name = CharField(faceted=True)

    def get_model(self):
        return Note

    def prepare_facet_model_name(self, obj):
        return "note"


class MemoIndex(SearchIndex, indexes.Indexable):
    title = CharField(model_attr='title')
    facet_model_name = CharField(faceted=True)

    def get_model(self):
        return Memo

    def prepare_facet_model_name(self, obj):
        return "memo"

依此类推,只需为每个搜索索引返回一个不同的字符串。您也可以创建一个mixin并返回get_model返回的模型的名称。

假设您已将此字段添加到每个SearchIndex定义中,只有chain the facet method添加到您的结果中。

results = form.search().facet('facet_model_name')

现在facet_counts方法将返回一个字典,其中包含分面字段和每个构面值的结果计数,在本例中为模型名称。

请注意,此处的字段大致标记,以避免与Haystack添加的字段model_name发生冲突。它不是刻面的,我不确定重复它是否会引起冲突。

答案 1 :(得分:0)

here为此做了很好的演练。

您需要的最低限度:

  1. 是将faceted=True添加到model_names字段的参数中。
  2. 重建您的架构和索引。
  3. .facet('model_names')添加到您想要的任何SearchQuerySet中。
  4. 对该问题的更多解释将能够得到更完整的答案。

答案 2 :(得分:0)

如果您只想过滤模型类型,可以使用ModelSearchForm