Skip to content

Commit

Permalink
Fixed #1629 -- Fixed search highlighting not working on stemmed words
Browse files Browse the repository at this point in the history
  • Loading branch information
bmispelon committed Nov 26, 2024
1 parent 2b3b8c3 commit e6987d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,14 @@ def search(self, query_text, release):
search_query,
start_sel="<mark>",
stop_sel="</mark>",
config=models.F("config"),
),
highlight=SearchHeadline(
KeyTextTransform("body", "metadata"),
search_query,
start_sel="<mark>",
stop_sel="</mark>",
config=models.F("config"),
),
breadcrumbs=models.F("metadata__breadcrumbs"),
)
Expand Down
28 changes: 27 additions & 1 deletion docs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from django.conf import settings
from django.contrib.sites.models import Site
from django.db import connection
from django.template import Context, Template
from django.template.loader import render_to_string
from django.test import RequestFactory, TestCase
Expand All @@ -16,7 +17,7 @@
from djangoproject.urls import www as www_urls
from releases.models import Release

from .models import Document, DocumentRelease
from .models import DOCUMENT_SEARCH_VECTOR, Document, DocumentRelease
from .sitemaps import DocsSitemap
from .templatetags.docs import get_all_doc_versions
from .utils import get_doc_path
Expand Down Expand Up @@ -616,6 +617,31 @@ def test_search_update(self):
self.assertEqual(Document.objects.search_update(), 6)
self.assertEqual(Document.objects.exclude(search=None).count(), 6)

def test_search_highlight_stemmed(self):
# The issue only manifests itself when the defaut search config is not english
with connection.cursor() as cursor:
cursor.execute("SET default_text_search_config TO 'simple'", [])

doc = self.release.documents.create(
config="english",
path="/",
title="triaging tickets",
metadata={"body": "text containing the word triaging", "breadcrumbs": []},
)
doc.search = DOCUMENT_SEARCH_VECTOR
doc.save(update_fields=["search"])

self.assertQuerySetEqual(
Document.objects.search("triaging", self.release),
[
(
"<mark>triaging</mark> tickets",
"text containing the word <mark>triaging</mark>",
)
],
transform=attrgetter("headline", "highlight"),
)


class TemplateTestCase(TestCase):
def _assertOGTitleEqual(self, doc, expected):
Expand Down

0 comments on commit e6987d1

Please sign in to comment.