comment search highlighting

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

View File

@@ -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,