I ran into an issue with the Hymns for Worship app - I was trying to find I’m Not Ashamed to Own My Lord. When searching “I’m not ashamed,” it doesn’t come up, but when searching “own my Lord,” it does.
Root Cause
In packages/hfw_core/lib/src/services/powersync/queries.dart, formatSearchQuery strips all non-word punctuation (replaceAll(RegExp(r"[^\w\s]"), "")), turning "I’m not ashamed" (or even "I'm not ashamed") into %im%not%ashamed%, which fails SQLite LIKE against "I'm Not Ashamed to Own My Lord" because the stored title still contains the apostrophe between I and m.
In lyrics search (watchHymns / getHymnLyricsSnippets), raw cleanQ and tokens keep iOS smart/curly apostrophes (’, U+2019), so hp.lyrics LIKE '%i’m not ashamed%' and hp.lyrics LIKE '%i’m%' only match lyrics stored with curly ’ (like #22231) and miss hymns stored with straight ASCII ' (like #273).
SearchRanker in packages/hfw_flutter/lib/services/search/search_ranker.dart compares lowerRaw directly against title / lyricSnippet without normalizing smart apostrophes/quotes or punctuation.
Expected Behavior
Searching "I’m not ashamed", "I'm not ashamed", or "im not ashamed" should consistently find and rank #273 I'm Not Ashamed to Own My Lord as well as hymns stored with either straight or curly apostrophes.
Implemented apostrophe- and punctuation-insensitive search normalization across SQLite queries, snippet extraction, and ranking:
Added packages/hfw_core/lib/src/utils/search_text_utils.dart (normalizeSmartQuotes, normalizeSearchText, tokenizeSearchQuery, formatSearchQuery, sqlStripApostrophes, findNormalizedMatchSpan) and exported it from hfw_core.
Updated packages/hfw_core/lib/src/services/powersync/queries.dart (watchHymns, getHymnLyricsSnippets, _extractSnippet, and catalog/list/index search queries) to strip straight and curly apostrophes (', ’, ‘) in SQLite LIKE comparisons and normalize queries/tokens.
Updated SearchRanker (packages/hfw_flutter/lib/services/search/search_ranker.dart) and list search filtering (packages/hfw_flutter/lib/src/native_bridge/hfw_headless_coordinator.dart) to use normalizeSearchText and tokenizeSearchQuery.
Added unit & SQLite integration tests in packages/hfw_core/test/utils/search_text_utils_test.dart and packages/hfw_flutter/test/services/search/search_ranker_test.dart covering i’m not ashamed, i'm not ashamed, im not ashamed, I’m not ashamed,, and comma-separated titles.
Verified the uncommitted local search normalization changes via read-only test runs:
packages/hfw_core: dart test test/utils/search_text_utils_test.dart — 10/10 passed (covers normalizeSmartQuotes, normalizeSearchText, tokenizeSearchQuery, formatSearchQuery, findNormalizedMatchSpan, and SQLite PowerSyncQueryX integration for i’m not ashamed, i'm not ashamed, im not ashamed, I’m not ashamed,, and comma-separated titles).
packages/hfw_flutter: flutter test test/services/search/search_ranker_test.dart — 12/12 passed (including smart, straight, and omitted apostrophe ranking parity).