From 10f0c4a0775e0c1e5a6d74b293b83e08f1de2244 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Wed, 19 Jun 2024 16:50:16 +0200 Subject: [PATCH] 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. --- archivebox/core/views.py | 28 +++++++++++++++++++-- archivebox/templates/core/public_index.html | 9 +++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/archivebox/core/views.py b/archivebox/core/views.py index efaca2f5..3d7f2e23 100644 --- a/archivebox/core/views.py +++ b/archivebox/core/views.py @@ -358,13 +358,37 @@ class PublicIndexView(ListView): def get_queryset(self, **kwargs): qs = super().get_queryset(**kwargs) - query = self.request.GET.get('q') - if query and query.strip(): + query = self.request.GET.get('q', default = '').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)) 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 == '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() def get(self, *args, **kwargs): diff --git a/archivebox/templates/core/public_index.html b/archivebox/templates/core/public_index.html index 23ad5b21..1401dd63 100644 --- a/archivebox/templates/core/public_index.html +++ b/archivebox/templates/core/public_index.html @@ -6,6 +6,15 @@