From 91d4941438d50ee8fe92de1921e1c6989e2558cb Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 4 Jan 2023 19:06:06 -0800 Subject: [PATCH 1/2] Support comment search --- src/documents/index.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/documents/index.py b/src/documents/index.py index e9c340697..7ba50a324 100644 --- a/src/documents/index.py +++ b/src/documents/index.py @@ -5,6 +5,7 @@ from contextlib import contextmanager from dateutil.parser import isoparse from django.conf import settings +from documents.models import Comment from documents.models import Document from whoosh import classify from whoosh import highlight @@ -49,6 +50,7 @@ def get_schema(): path=TEXT(sortable=True), path_id=NUMERIC(), has_path=BOOLEAN(), + comments=TEXT(), ) @@ -90,6 +92,7 @@ def open_index_searcher(): def update_document(writer, doc): tags = ",".join([t.name for t in doc.tags.all()]) tags_ids = ",".join([str(t.id) for t in doc.tags.all()]) + comments = ",".join([str(c.comment) for c in Comment.objects.filter(document=doc)]) writer.update_document( id=doc.pk, title=doc.title, @@ -110,6 +113,7 @@ def update_document(writer, doc): path=doc.storage_path.name if doc.storage_path else None, path_id=doc.storage_path.id if doc.storage_path else None, has_path=doc.storage_path is not None, + comments=comments, ) @@ -255,7 +259,7 @@ class DelayedFullTextQuery(DelayedQuery): def _get_query(self): q_str = self.query_params["query"] qp = MultifieldParser( - ["content", "title", "correspondent", "tag", "type"], + ["content", "title", "correspondent", "tag", "type", "comments"], self.searcher.ixreader.schema, ) qp.add_plugin(DateParserPlugin()) From b869ad02a1f1fe05f789ac4a7adb917fdd571bc1 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 4 Jan 2023 19:06:51 -0800 Subject: [PATCH 2/2] comment search highlighting --- .../document-card-large.component.html | 8 +++++++- src-ui/src/app/data/paperless-document.ts | 1 + src/documents/views.py | 11 ++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.html b/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.html index 80aedb7f2..0eb965a22 100644 --- a/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.html +++ b/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.html @@ -25,7 +25,13 @@

- + + + + + + + {{contentTrimmed}}

diff --git a/src-ui/src/app/data/paperless-document.ts b/src-ui/src/app/data/paperless-document.ts index 8b038d79e..cdee624af 100644 --- a/src-ui/src/app/data/paperless-document.ts +++ b/src-ui/src/app/data/paperless-document.ts @@ -10,6 +10,7 @@ export interface SearchHit { rank?: number highlights?: string + comment_highlights?: string } export interface PaperlessDocument extends ObjectWithId { diff --git a/src/documents/views.py b/src/documents/views.py index e313ae17e..c65b6f0b4 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -458,10 +458,19 @@ class DocumentViewSet( class SearchResultSerializer(DocumentSerializer): def to_representation(self, instance): doc = Document.objects.get(id=instance["id"]) + commentTerm = instance.results.q.subqueries[0] + comments = ",".join( + [ + str(c.comment) + for c in Comment.objects.filter(document=instance["id"]) + if commentTerm.text in c.comment + ], + ) r = super().to_representation(doc) r["__search_hit__"] = { "score": instance.score, - "highlights": instance.highlights("content", text=doc.content) + "highlights": instance.highlights("content", text=doc.content), + "comment_highlights": instance.highlights("content", text=comments) if doc else None, "rank": instance.rank,