comment search highlighting

This commit is contained in:
Michael Shamoon 2023-01-04 19:06:51 -08:00
parent 91d4941438
commit b869ad02a1
3 changed files with 18 additions and 2 deletions

View File

@ -25,7 +25,13 @@
</h5> </h5>
</div> </div>
<p class="card-text"> <p class="card-text">
<span *ngIf="document.__search_hit__" [innerHtml]="document.__search_hit__.highlights"></span> <span *ngIf="document.__search_hit__ && document.__search_hit__.highlights" [innerHtml]="document.__search_hit__.highlights"></span>
<span *ngIf="document.__search_hit__ && document.__search_hit__.comment_highlights">
<svg width="1em" height="1em" fill="currentColor" class="me-2">
<use xlink:href="assets/bootstrap-icons.svg#chat-left-text"/>
</svg>
<span [innerHtml]="document.__search_hit__.comment_highlights"></span>
</span>
<span *ngIf="!document.__search_hit__" class="result-content">{{contentTrimmed}}</span> <span *ngIf="!document.__search_hit__" class="result-content">{{contentTrimmed}}</span>
</p> </p>

View File

@ -10,6 +10,7 @@ export interface SearchHit {
rank?: number rank?: number
highlights?: string highlights?: string
comment_highlights?: string
} }
export interface PaperlessDocument extends ObjectWithId { export interface PaperlessDocument extends ObjectWithId {

View File

@ -458,10 +458,19 @@ class DocumentViewSet(
class SearchResultSerializer(DocumentSerializer): class SearchResultSerializer(DocumentSerializer):
def to_representation(self, instance): def to_representation(self, instance):
doc = Document.objects.get(id=instance["id"]) 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 = super().to_representation(doc)
r["__search_hit__"] = { r["__search_hit__"] = {
"score": instance.score, "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 if doc
else None, else None,
"rank": instance.rank, "rank": instance.rank,