5

我想根据返回的不同模型名称(类)对结果进行分面。是否有捷径可寻?

4

3 回答 3

5

您是否尝试过添加SearchIndex包含此信息的字段?例如

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定义中,只需将该facet方法链接到您的结果。

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

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

请注意,此处的字段被详细标记以避免可能与model_nameHaystack 添加的字段冲突。它没有刻面,我不确定复制它是否会导致冲突。

于 2012-10-18T17:07:34.223 回答
0

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

于 2017-05-22T12:37:56.730 回答
0

文档对此进行了非常好的演练。

您需要的最低要求:

  1. 是添加faceted=True到您的model_names领域的参数。
  2. 重建您的架构和索引。
  3. 添加.facet('model_names')到您想要分面的任何 SearchQuerySet 中。

对该问题的更多解释将使答案更完整。

于 2015-10-15T01:26:14.920 回答