Permalink
Browse files

/search/ now correctly handles multiple ?tag= params

For example:

https://simonwillison.net/search/?q=london&exclude.tag=london&tag=quora&tag=startups

This now returns documents where a full text search matcehs "london",
excluding documents that are tagged "london", filtered to just documents that
are tagged both "quora" and "startups".

Previously is cb88c2d multiple ?tag= params were treated as OR, now they
are treated as AND.
  • Loading branch information...
simonw committed Oct 2, 2017
1 parent 250ed78 commit 776a562a70cf1d908db27fe6a96f3e539c0b85a2
Showing with 9 additions and 11 deletions.
  1. +9 −11 blog/views.py
View
@@ -400,24 +400,22 @@ def search_results(request, q):
'search_document': query
}
tags = request.GET.getlist('tag')
if tags:
filter_kwargs['tags__tag__in'] = tags
exclude_kwargs = {}
exclude_tags = request.GET.getlist('exclude.tag')
if exclude_tags:
exclude_kwargs['tags__tag__in'] = exclude_tags
values = ('pk', 'type', 'created', 'rank')
def make_queryset(klass, type_name):
return klass.objects.annotate(
qs = klass.objects.annotate(
rank=rank_annotation,
type=models.Value(type_name, output_field=models.CharField())
).filter(
**filter_kwargs
).exclude(
**exclude_kwargs
).values(*values).order_by()
)
if q:
qs = qs.filter(search_document=query)
for tag in tags:
qs = qs.filter(tags__tag=tag)
for exclude_tag in exclude_tags:
qs = qs.exclude(tags__tag=exclude_tag)
return qs.values(*values).order_by()
# Start with a .none() queryset just so we can union stuff onto it
qs = Entry.objects.annotate(

0 comments on commit 776a562

Please sign in to comment.