Permalink
Browse files

Added facet by year

  • Loading branch information...
simonw committed Oct 3, 2017
1 parent e805ad6 commit 0b361c78545634e8acf9c9b53f55659ca5b90ba7
Showing with 33 additions and 1 deletion.
  1. +21 −0 blog/views.py
  2. +12 −1 templates/search.html
View
@@ -6,6 +6,7 @@
from django.contrib.postgres.search import SearchQuery, SearchRank
from django.views.decorators.cache import never_cache
from django.db import models
from django.db.models.functions import TruncYear
from django.conf import settings
from django.core.paginator import (
Paginator,
@@ -401,6 +402,7 @@ def search_results(request, q):
selected_tags = request.GET.getlist('tag')
excluded_tags = request.GET.getlist('exclude.tag')
selected_type = request.GET.get('type', '')
selected_year = request.GET.get('year', '')
values = ('pk', 'type', 'created', 'rank')
@@ -409,6 +411,8 @@ def make_queryset(klass, type_name):
rank=rank_annotation,
type=models.Value(type_name, output_field=models.CharField())
)
if selected_year:
qs = qs.filter(created__year=int(selected_year))
if q:
qs = qs.filter(search_document=query)
for tag in selected_tags:
@@ -425,6 +429,7 @@ def make_queryset(klass, type_name):
type_counts_raw = {}
tag_counts_raw = {}
year_counts_raw = {}
for klass, type_name in (
(Entry, 'entry'),
@@ -443,6 +448,12 @@ def make_queryset(klass, type_name):
n=models.Count('tag')
).values_list('tag', 'n'):
tag_counts_raw[tag] = tag_counts_raw.get(tag, 0) + count
for row in klass_qs.order_by().annotate(
year=TruncYear('created')
).values('year').annotate(n=models.Count('pk')):
year_counts_raw[row['year']] = year_counts_raw.get(
row['year'], 0
) + row['n']
qs = qs.union(klass_qs.values(*values))
qs = qs.order_by('-rank')
@@ -461,6 +472,14 @@ def make_queryset(klass, type_name):
key=lambda t: t['n'], reverse=True
)[:40]
year_counts = sorted(
[
{'year': year, 'n': value}
for year, value in year_counts_raw.items()
],
key=lambda t: t['year']
)
paginator = Paginator(qs, 30)
page_number = request.GET.get('page') or '1'
try:
@@ -486,9 +505,11 @@ def make_queryset(klass, type_name):
'duration': end - start,
'type_counts': type_counts,
'tag_counts': tag_counts,
'year_counts': year_counts,
'selected_tags': selected_tags,
'excluded_tags': excluded_tags,
'selected_type': selected_type,
'selected_year': selected_year,
})
View
@@ -13,12 +13,15 @@ <h2>Search{% if q %} for “{{ q }}”{% endif %}</h2>
<input type="submit" class="search-submit" value="Search">
</form>
{% if selected_tags or selected_type %}
{% if selected_tags or selected_type or selected_year %}
<p class="search-selections">
Filters:
{% if selected_type %}
<a class="selected-tag" href="{% remove_qsarg "type" selected_type %}">Type: {{ selected_type }} <strong>&#x00D7;</strong></a>
{% endif %}
{% if selected_year %}
<a class="selected-tag" href="{% remove_qsarg "year" selected_year %}">Year: {{ selected_year }} <strong>&#x00D7;</strong></a>
{% endif %}
{% for selected_tag in selected_tags %}
<a class="selected-tag" href="{% remove_qsarg "tag" selected_tag %}">{{ selected_tag }} <strong>&#x00D7;</strong></a>
{% endfor %}
@@ -45,6 +48,14 @@ <h3>Types</h3>
{% endfor %}
</ul>
{% endif %}
{% if year_counts %}
<h3>Years</h3>
<ul>
{% for t in year_counts %}
<li><a href="{% add_qsarg "year" t.year|date:"Y" %}">{{ t.year|date:"Y" }}</a> {{ t.n }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% if tag_counts %}
<h3>Tags</h3>
<ul>

0 comments on commit 0b361c7

Please sign in to comment.