Skip to content

Fix fallback search ordering and add search view tests#665

Open
Anayo-Anyafulu wants to merge 3 commits intowagtail:mainfrom
Anayo-Anyafulu:fix/preserve-search-ordering-663
Open

Fix fallback search ordering and add search view tests#665
Anayo-Anyafulu wants to merge 3 commits intowagtail:mainfrom
Anayo-Anyafulu:fix/preserve-search-ordering-663

Conversation

@Anayo-Anyafulu
Copy link
Copy Markdown
Contributor

@Anayo-Anyafulu Anayo-Anyafulu commented Mar 15, 2026

Fixes #663. Closes search app portion of #623.

Changes

Fix: Page.objects.filter(id__in=page_ids) does not preserve the order of the provided ID list, causing fallback search results to appear in database default order instead of the intended merged order.

Added Case/When expression to preserve merged ID ordering in the fallback search queryset. Pagination behaviour is unchanged.

Tests: Added bakerydemo/search/tests/test_search_view.py with 8 tests covering:

  • HTTP 200 response for search with and without query
  • Correct template usage (search/search_results.html)
  • Empty query returns no results
  • Fallback search finds BlogPage, BreadPage, and LocationPage results
  • Fallback ordering preservation via Case/When

Follows the WagtailPageTestCase pattern from bakerydemo/base/tests/.

AI usage

I used Claude (claude.ai) to help understand the bug and navigate the codebase. The fix and tests were identified and verified by reading the source code directly.

Page.objects.filter(id__in=page_ids) does not preserve the order of
the provided ID list. This caused fallback search results to appear in
database default order rather than the intended merged order.

Fixed by applying explicit ordering using a Case/When expression so
that the final queryset preserves the merged ID order deterministically.
Pagination behavior is unchanged.

Fixes wagtail#663
Cover HTTP 200 rendering, correct template usage, empty query
behaviour, per-model result discovery, and fallback ordering
preservation via Case/When (related to wagtail#665).
@Anayo-Anyafulu Anayo-Anyafulu changed the title Fix fallback search losing merged ordering due to id__in queryset Fix fallback search ordering and add search view tests Mar 21, 2026
@Pra26nav
Copy link
Copy Markdown
Contributor

Hi @Anayo-Anyafulu I found a related bug that RecipePage is missing from the hardcoded model list in the search path. Currently search/views.py imports only BlogPage, BreadPage and LocationPage whereas RecipePage is missing and is invisible from web search.
Reproduction:

  1. Search for "hot cross bun" at /search/ → "No results found"
  2. Navigate to /recipes/ directly → "Hot Cross Bun" recipe is live and accessible Since you were already modifying this file, so I thought it would be good to include RecipePage in same PR. The fix would be adding it to the imports and the page_ids union with three other models.
    Screenshot 2026-03-31 103129 Screenshot 2026-03-31 103059

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fallback search may lose merged ordering due to id__in queryset

2 participants