Skip to content

Improve ingredient search relevance for long names - #2486

Open
MikaelSiidorow wants to merge 4 commits into
wger-project:masterfrom
MikaelSiidorow:improve-ingredient-search
Open

Improve ingredient search relevance for long names#2486
MikaelSiidorow wants to merge 4 commits into
wger-project:masterfrom
MikaelSiidorow:improve-ingredient-search

Conversation

@MikaelSiidorow

@MikaelSiidorow MikaelSiidorow commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Proposed Changes

  • Expand single-term searches with indexable literal substring matches while preserving the existing whole-name trigram candidates for typos.
  • Rank exact names, prefixes, and whole-word matches first, then order the remaining candidates by whole-name trigram similarity.
  • Bound short and punctuation-only searches to avoid unindexed catalog scans.
  • Add PostgreSQL integration tests for long names, multi-word queries, ranking, partial words, typos, Unicode case handling, and short inputs.

This fixes cases where a single search term appears inside a longer ingredient name but falls below the whole-name trigram cutoff, such as lunch not finding Lunch out (placeholder).

One- and two-character searches use exact matching. Punctuation-only searches return no results without querying PostgreSQL. Multi-word searches keep the existing trigram candidate path with the improved ranking.

This PR intentionally leaves the optional exact-match toggle out of scope. That requires separate API and client-side UX decisions and can be considered as a follow-up.

Related Issue(s)

Related to #2340. This improves name-based search, but personalized ranking and catalog quality remain follow-up work.

Discussed on Discord.

Testing

Run the focused tests in the dev-postgres environment:

docker compose exec web python3 manage.py test \
  wger.nutrition.tests.test_search_api.IngredientSearchRankingApiTestCase

All 12 added PostgreSQL integration tests pass. The existing PostgreSQL ingredient search tests also pass.

Query plans and count-plus-top-20 latency were checked against 3,057,932 ingredient names. The indexed paths use the existing trigram GIN index, so no migration is needed. Across 12 common queries, median overhead compared with main was about 5%, with higher cost for searches where the literal arm expands recall.

Please check that the PR fulfills these requirements

  • Tests for the changes have been added
  • Code has been formatted to avoid unnecessary diffs
  • A changelog entry has been added; no manual deployment steps are needed

Comment thread wger/nutrition/tests/test_search_api.py
Comment thread wger/nutrition/api/filtersets.py
@MikaelSiidorow
MikaelSiidorow marked this pull request as draft August 15, 2026 17:37
@MikaelSiidorow
MikaelSiidorow force-pushed the improve-ingredient-search branch from 9b70d1a to 3098c45 Compare August 15, 2026 19:27
@MikaelSiidorow
MikaelSiidorow force-pushed the improve-ingredient-search branch from 3098c45 to 26f80bd Compare August 15, 2026 20:06
@MikaelSiidorow
MikaelSiidorow force-pushed the improve-ingredient-search branch from 4e4333f to 15fbcc2 Compare August 16, 2026 10:15
@MikaelSiidorow

MikaelSiidorow commented Aug 16, 2026

Copy link
Copy Markdown
Contributor Author

Ready for another review. Since the initial draft, I have:

  • preserved typo matches from the original trigram search
  • bounded short and punctuation-only inputs
  • refined exact, prefix, whole-word, and Unicode ranking
  • limited literal expansion to single-term searches after full-catalog performance testing

@MikaelSiidorow
MikaelSiidorow marked this pull request as ready for review August 16, 2026 10:23
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.

2 participants