Permalink
Browse files

Show facets on https://simonwillison.net/search/ with no ?q=

Also fixed it so that the currently selected filters persist if you change the
search term in the search box.
  • Loading branch information...
simonw committed Oct 3, 2017
1 parent 79b1b13 commit 3955f41b3574ebd9006a98cdafa225d89aba9757
Showing with 36 additions and 19 deletions.
  1. +20 −16 blog/views.py
  2. +16 −3 templates/search.html
View
@@ -386,30 +386,27 @@ def tools(request):
def search(request):
q = request.GET.get('q')
if q:
return search_results(request, q)
else:
return render(request, 'search.html')
def search_results(request, q):
q = request.GET.get('q', '').strip()
start = time.time()
query = SearchQuery(q)
rank_annotation = SearchRank(models.F('search_document'), query)
query = None
rank_annotation = None
if q:
query = SearchQuery(q)
rank_annotation = SearchRank(models.F('search_document'), query)
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', '')
selected_month = request.GET.get('month', '')
values = ('pk', 'type', 'created', 'rank')
values = ['pk', 'type', 'created']
if q:
values.append('rank')
def make_queryset(klass, type_name):
qs = klass.objects.annotate(
rank=rank_annotation,
type=models.Value(type_name, output_field=models.CharField())
)
if selected_year:
@@ -418,6 +415,7 @@ def make_queryset(klass, type_name):
qs = qs.filter(created__month=int(selected_month))
if q:
qs = qs.filter(search_document=query)
qs = qs.annotate(rank=rank_annotation)
for tag in selected_tags:
qs = qs.filter(tags__tag=tag)
for exclude_tag in excluded_tags:
@@ -426,9 +424,11 @@ def make_queryset(klass, type_name):
# Start with a .none() queryset just so we can union stuff onto it
qs = Entry.objects.annotate(
rank=rank_annotation,
type=models.Value('empty', output_field=models.CharField())
).values(*values).none()
)
if q:
qs = qs.annotate(rank=rank_annotation)
qs = qs.values(*values).none()
type_counts_raw = {}
tag_counts_raw = {}
@@ -467,7 +467,11 @@ def make_queryset(klass, type_name):
row['month'], 0
) + row['n']
qs = qs.union(klass_qs.values(*values))
qs = qs.order_by('-rank')
if q:
qs = qs.order_by('-rank')
else:
qs = qs.order_by('-created')
type_counts = sorted(
[
@@ -513,7 +517,7 @@ def make_queryset(klass, type_name):
for obj in load_mixed_objects(page.object_list):
results.append({
'type': obj.original_dict['type'],
'rank': obj.original_dict['rank'],
'rank': obj.original_dict.get('rank'),
'obj': obj,
})
end = time.time()
View
@@ -11,6 +11,17 @@ <h2>Search{% if q %} for “{{ q }}”{% endif %}</h2>
<form action="{{ request.path }}" method="GET">
<input type="search" class="search-input" name="q" value="{{ q }}" style="width: 80%">
<input type="submit" class="search-submit" value="Search">
{% if selected %}
{% for pair in selected.items %}
{% if pair.0 == 'tags' %}
{% for tag in pair.1 %}
<input type="hidden" name="tag" value="{{ tag }}">
{% endfor %}
{% else %}
<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}">
{% endif %}
{% endfor %}
{% endif %}
</form>
{% if selected %}
@@ -34,9 +45,11 @@ <h2>Search{% if q %} for “{{ q }}”{% endif %}</h2>
<br>
{% if total %}
{% include "_pagination.html" with page_total=total %}
{% blog_mixed_list_with_dates results %}
{% include "_pagination.html" %}
{% if selected or q %}
{% include "_pagination.html" with page_total=total %}
{% blog_mixed_list_with_dates results %}
{% include "_pagination.html" %}
{% endif %}
{% endif %}
{% endblock %}

0 comments on commit 3955f41

Please sign in to comment.