[Answer]-Django-haystack: Predicting facets

1👍

The facet_counts method of SearchQuerySet does what you want:

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}

Then, you can sum up the number of products by John and the number of products by another author. That will give you the number of products if you were to broaden the search to both of them:

num_john_products = author_counts.get('John', 0)
num_bob_products = author_counts.get('Bob', 0)
total = num_john_products + num_bob_products

Or, you could even find the author other than John with the most products, and show the number of results if you were to also include that author in the search:

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 + count

Leave a comment