Skip to content

Commit

Permalink
fix(search): Avoid splitting § on non-phrase queries
Browse files Browse the repository at this point in the history
  • Loading branch information
albertisfu committed Jan 1, 2025
1 parent 9b37bec commit 336be50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cl/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def cleanup_main_query(query_string: str) -> str:
cleaned_items = []
# Replace smart quotes with standard double quotes for consistency.
query_string = re.sub(r"[“”]", '"', query_string)
for item in re.split(r'([^a-zA-Z0-9_\-^~":]+)', query_string):
for item in re.split(r'([^a-zA-Z0-9_\-^~":§]+)', query_string):
if not item:
continue

Expand Down
32 changes: 29 additions & 3 deletions cl/search/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,17 +858,43 @@ def test_raise_forbidden_error_on_depth_pagination(self) -> None:
)
self.assertEqual(r.status_code, HTTPStatus.FORBIDDEN)

async def test_avoid_splitting_terms_on_phrase_queries(self) -> None:
"""Can we avoid splitting words in phrase queries such as "§247"?"""
async def test_avoid_splitting_terms_with_section_mark(self) -> None:
"""Can we avoid splitting words in queries such as §247 and phrases
like "§247"?
"""

# A search for "Lorem §247" shouldn't match "Lorem 247"
# A search for phrase "Lorem §247" shouldn't match "Lorem 247"
r = await self.async_client.get(
reverse("show_results"), {"q": '"Lorem §247"'}
)
actual = self.get_article_count(r)
self.assertEqual(actual, 1)
self.assertIn("1:21-cv-1234", r.content.decode())

# A search for phrase "Lorem 247" shouldn't match "Lorem §247"
r = await self.async_client.get(
reverse("show_results"), {"q": '"Lorem 247"'}
)
actual = self.get_article_count(r)
self.assertEqual(actual, 1)
self.assertIn("34-2535", r.content.decode())

# A search for Lorem §247 shouldn't match Lorem 247
r = await self.async_client.get(
reverse("show_results"), {"q": "Lorem §247"}
)
actual = self.get_article_count(r)
self.assertEqual(actual, 1)
self.assertIn("1:21-cv-1234", r.content.decode())

# A search for Lorem 247 shouldn't match Lorem §247
r = await self.async_client.get(
reverse("show_results"), {"q": "Lorem 247"}
)
actual = self.get_article_count(r)
self.assertEqual(actual, 1)
self.assertIn("34-2535", r.content.decode())


class SearchAPIV4CommonTest(ESIndexTestCase, TestCase):
"""Common tests for the Search API V4 endpoints."""
Expand Down

0 comments on commit 336be50

Please sign in to comment.