Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly query case-insensitively #115

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crawler/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def search_components(class_name_contains, include_class_names=False):
queryset = Page.objects.prefetch_related("components").filter(
components__class_name__contains=class_name_contains
components__class_name__icontains=class_name_contains
)

values = _page_values
Expand All @@ -26,10 +26,10 @@ def search_components(class_name_contains, include_class_names=False):
def search_links(href_contains, include_hrefs=False, or_urlencoded=True):
queryset = Page.objects.prefetch_related("links")

href_filter = Q(links__href__contains=href_contains)
href_filter = Q(links__href__icontains=href_contains)

if or_urlencoded: # pragma: no branch
href_filter |= Q(links__href__contains=quote_plus(href_contains))
href_filter |= Q(links__href__icontains=quote_plus(href_contains))

queryset = queryset.filter(href_filter)

Expand All @@ -52,16 +52,16 @@ def search_empty():


def search_html(html_contains):
return _search_pages(html__contains=html_contains)
return _search_pages(html__icontains=html_contains)


def search_text(text_contains):
return _search_pages(text__contains=text_contains)
return _search_pages(text__icontains=text_contains)


def search_title(title_contains):
return _search_pages(title__contains=title_contains)
return _search_pages(title__icontains=title_contains)


def search_url(url_contains):
return _search_pages(url__contains=url_contains)
return _search_pages(url__icontains=url_contains)
10 changes: 8 additions & 2 deletions viewer/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ def test_search_by_html(self):
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["title"], "Sample homepage")

def test_search_by_text(self):
results = self.get_pages_api(search_type="text", q="Sample child page")
def check_search_by_text(self, q):
results = self.get_pages_api(search_type="text", q=q)
self.assertEqual(len(results), 2)
self.assertEqual(results[0]["title"], "Sample child page")

def test_search_by_text(self):
self.check_search_by_text("Sample child page")

def test_search_by_text_case_insensitive(self):
self.check_search_by_text("SAMPLE CHILD PAGE")

def test_search_by_title(self):
results = self.get_pages_api(search_type="title", q="Sample child page")
self.assertEqual(len(results), 2)
Expand Down
Loading