Implement searching individual fields

So far only for the public view, since we already have a custom search
form there, where this is easy to add.
This initial implementation supports the common set of metadata fields
that the searchbar placeholder also mentions, but adding more fields
is trivial.
This commit is contained in:
Lucas Schwiderski 2024-06-19 16:50:16 +02:00
parent 8d1d39b21e
commit 10f0c4a077
No known key found for this signature in database
GPG key ID: AA12679AAA6DF4D8
2 changed files with 35 additions and 2 deletions

View file

@ -358,13 +358,37 @@ class PublicIndexView(ListView):
def get_queryset(self, **kwargs): def get_queryset(self, **kwargs):
qs = super().get_queryset(**kwargs) qs = super().get_queryset(**kwargs)
query = self.request.GET.get('q') query = self.request.GET.get('q', default = '').strip()
if query and query.strip():
if not query:
return qs.distinct()
query_type = self.request.GET.get('query_type')
if not query_type or query_type == 'all':
qs = qs.filter(Q(title__icontains=query) | Q(url__icontains=query) | Q(timestamp__icontains=query) | Q(tags__name__icontains=query)) qs = qs.filter(Q(title__icontains=query) | Q(url__icontains=query) | Q(timestamp__icontains=query) | Q(tags__name__icontains=query))
try: try:
qs = qs | query_search_index(query) qs = qs | query_search_index(query)
except Exception as err: except Exception as err:
print(f'[!] Error while using search backend: {err.__class__.__name__} {err}') print(f'[!] Error while using search backend: {err.__class__.__name__} {err}')
elif query_type == 'fulltext':
try:
qs = qs | query_search_index(query)
except Exception as err:
print(f'[!] Error while using search backend: {err.__class__.__name__} {err}')
elif query_type == 'meta':
qs = qs.filter(Q(title__icontains=query) | Q(url__icontains=query) | Q(timestamp__icontains=query) | Q(tags__name__icontains=query))
elif query_type == 'url':
qs = qs.filter(Q(url__icontains=query))
elif query_type == 'title':
qs = qs.filter(Q(title__icontains=query))
elif query_type == 'timestamp':
qs = qs.filter(Q(timestamp__icontains=query))
elif query_type == 'tags':
qs = qs.filter(Q(tags__name__icontains=query))
else:
print(f'[!] Unknown value for query_type: "{query_type}"')
return qs.distinct() return qs.distinct()
def get(self, *args, **kwargs): def get(self, *args, **kwargs):

View file

@ -6,6 +6,15 @@
<form id="changelist-search" action="{% url 'public-index' %}" method="get"> <form id="changelist-search" action="{% url 'public-index' %}" method="get">
<div> <div>
<label for="searchbar"><img src="/static/admin/img/search.svg" alt="Search"></label> <label for="searchbar"><img src="/static/admin/img/search.svg" alt="Search"></label>
<select name="query_type" id="query_type">
<option value="all" selected>All</option>
<option value="fulltext">Content</option>
<option value="meta">Metadata</option>
<option value="url">URL</option>
<option value="title">Title</option>
<option value="timestamp">Timestamp</option>
<option value="tags">Tags</option>
</select>
<input type="text" size="40" name="q" value="" id="searchbar" autofocus placeholder="Title, URL, tags, timestamp, or content...".> <input type="text" size="40" name="q" value="" id="searchbar" autofocus placeholder="Title, URL, tags, timestamp, or content...".>
<input type="submit" value="Search" style="height: 36px; padding-top: 6px; margin: 8px"/> <input type="submit" value="Search" style="height: 36px; padding-top: 6px; margin: 8px"/>
<input type="button" <input type="button"