我正在试着看看django-haystack是否适合我的方面需求。目前,我想要显示我的面附近的数字。假设我们在他们的文档中显示了一个模型,但是有一个额外的分面字段(例如,作者用来写注释的样式……例如,风格是经典的或现代的)。所以index看起来就像这样。
class NoteIndex(SearchIndex, indexes.Indexable):
text = CharField(document=True, use_template=True)
author = CharField(model_attr='user', faceted=True)
style = CharField(model_attr='style', faceted=True)
pub_date = DateTimeField(model_attr='pub_date')现在我创建了一个搜索查询集,并将范围缩小到facet作者,例如john。现在我想显示如果我和John一起选择另一位作者(例如+10篇文章),将出现多少产品的估计。同样的问题也是关于从样式中添加额外的方面。我该怎么做。搜索BE是elasticsearch。
发布于 2013-01-26 06:05:32
SearchQuerySet的facet_counts方法可以完成您想要的操作:
results = SearchQuerySet().filter(text='some product').facet('author')
counts = results.facet_counts()
author_results = counts['fields']['author']
author_counts = {author: num_products for author, num_products in author_results}然后,您可以将John的产品数量和另一位作者的产品数量相加。如果您将搜索范围扩大到这两种产品,就会得到产品的数量:
num_john_products = author_counts.get('John', 0)
num_bob_products = author_counts.get('Bob', 0)
total = num_john_products + num_bob_products或者,您甚至可以找到除John之外拥有最多产品的作者,如果您将该作者也包括在搜索中,则会显示结果的数量:
author_counts.pop('John')
most_popular_author = max([(count, author) for author, count in author_counts])
count, author = most_popular_author
total_john_and_most_popular = num_john_products + counthttps://stackoverflow.com/questions/14444302
复制相似问题