fix: find the exact title/author match before the fuzzy candidate cap #56

Merged
jfro merged 2 commits from jtk/book-matcher-exact into main 2026-10-18 22:39:46 +00:00
Owner

BookMatcher ran its normalized title/author match over the fuzzy candidate set, which is capped at 200 rows ordered by id and pulls in any book that shares the longest author word or the longest title word. In a catalog with lots of "Love …" books, the real match could land past row 200, and the ebook would then create a duplicate book or link a weaker fuzzy match.

Change

  • New exact_candidates/2 query, used first. It returns books with the exact raw title and author, or books whose title contains every word of the normalized title and whose author contains the surname (longest word) of any of the ebook's authors. ANDing the words keeps the result small, so the cap here is a generous 1,000 rather than 200.
  • On the SQL side, words are compared with ilike over translate(...), which folds common Latin accents. That way "bronte" still finds "Brontë", the same as normalize/1 does in Elixir, and no unaccent extension or migration is needed.
  • "and" is left out of the word list because "&" normalizes to it, so "Pride & Prejudice" in the catalog still matches an ebook titled "Pride and Prejudice".
  • The final check is still find_normalized/3 in Elixir, so what counts as a match hasn't changed. Only the set of books it looks at is bigger and narrower.
  • With fuzzy on and no exact hit, it falls back to the capped set as before: normalized match first, then Jaro similarity. With fuzzy off, there's no second query any more.

Tests (210 filler "Love Story N" books by other authors)

  • Exact title/author past the cap, with fuzzy on and off. Failed before the fix.
  • Normalized match past the cap: accents, "Last, First", edition postfix, punctuation. Failed before the fix.
  • One of several authors matches.
  • A wrong author still doesn't match.

mix precommit passes.

Fixes #23

`BookMatcher` ran its normalized title/author match over the fuzzy candidate set, which is capped at 200 rows ordered by id and pulls in any book that shares the longest author word *or* the longest title word. In a catalog with lots of "Love …" books, the real match could land past row 200, and the ebook would then create a duplicate book or link a weaker fuzzy match. **Change** - New `exact_candidates/2` query, used first. It returns books with the exact raw title and author, or books whose title contains **every** word of the normalized title **and** whose author contains the surname (longest word) of **any** of the ebook's authors. ANDing the words keeps the result small, so the cap here is a generous 1,000 rather than 200. - On the SQL side, words are compared with `ilike` over `translate(...)`, which folds common Latin accents. That way "bronte" still finds "Brontë", the same as `normalize/1` does in Elixir, and no `unaccent` extension or migration is needed. - "and" is left out of the word list because "&" normalizes to it, so "Pride & Prejudice" in the catalog still matches an ebook titled "Pride and Prejudice". - The final check is still `find_normalized/3` in Elixir, so what counts as a match hasn't changed. Only the set of books it looks at is bigger and narrower. - With fuzzy on and no exact hit, it falls back to the capped set as before: normalized match first, then Jaro similarity. With fuzzy off, there's no second query any more. **Tests** (210 filler "Love Story N" books by other authors) - Exact title/author past the cap, with fuzzy on and off. Failed before the fix. - Normalized match past the cap: accents, "Last, First", edition postfix, punctuation. Failed before the fix. - One of several authors matches. - A wrong author still doesn't match. `mix precommit` passes. Fixes #23
jfro added this to the 0.6.2 milestone 2026-10-18 21:48:18 +00:00
fix: find the exact title/author match before the fuzzy candidate cap
All checks were successful
CI / test (pull_request) Successful in 1m48s
1953ae61f8
BookMatcher ran its normalized title/author match over the fuzzy
candidate set, which is capped at 200 rows ordered by id. With common
title words the exact match could fall past the cap, so the ebook made a
duplicate book or linked a weaker fuzzy match. Look up normalized
matches first in a narrower query (every title word AND an author
surname, accent-folded), then fall back to the capped fuzzy set.

Fixes #23
Author
Owner

Review from Nye (changes requested)

Blocker:

  1. Ø/ø in the translate() list break matching and create duplicate books with fuzzy matching off. normalize/1 strips only \p{Mn}; Ø/ø have no NFD decomposition, so Elixir keeps "nesbø", while SQL folds the DB value to "Nesbo", so '%nesbø%' never matches. Verified in Postgres 17: translate('Jo Nesbø', ) ILIKE '%nesbø%' = false. Any Nesbø/Søren/Bjørn ebook whose raw title/author isn't byte-identical to the catalog's misses exact_candidates, and with fuzzy off there's no fallback, so match_or_create_book creates a duplicate. Fix: drop Ø/ø from @accented/@unaccented (I checked the other 53 pairs; all match NFD+strip-Mn) and add a test with an ø author past the cap.

Should fix (medium, same root cause):
2. With fuzzy off, the old capped candidates/2 + find_normalized fallback is gone, so anything the SQL fold doesn't cover regresses vs main. Example: catalog "Karel Čapek" / ebook "Karel Capek", title "War with the Newts": main finds it via the "newts" title word; this PR returns nil (Č isn't in the list; verified ILIKE '%capek%' on translate('Karel Čapek') = false) -> duplicate book. Same for Czech/Polish/Hungarian/Romanian/Turkish accents. Simplest: on an exact_candidates miss, always run find_normalized over candidates/2 (fuzzy on or off), then find_similar only when fuzzy. One extra query, only on a miss. Add a Čapek-style test.

Nit:
3. translate()+ILIKE '%..%' is a seq scan per ebook (candidates/2 already is). Fine at catalog sizes.

Review from Nye (changes requested) Blocker: 1. Ø/ø in the translate() list break matching and create duplicate books with fuzzy matching off. normalize/1 strips only \p{Mn}; Ø/ø have no NFD decomposition, so Elixir keeps "nesbø", while SQL folds the DB value to "Nesbo", so '%nesbø%' never matches. Verified in Postgres 17: translate('Jo Nesbø', <your list>) ILIKE '%nesbø%' = false. Any Nesbø/Søren/Bjørn ebook whose raw title/author isn't byte-identical to the catalog's misses exact_candidates, and with fuzzy off there's no fallback, so match_or_create_book creates a duplicate. Fix: drop Ø/ø from @accented/@unaccented (I checked the other 53 pairs; all match NFD+strip-Mn) and add a test with an ø author past the cap. Should fix (medium, same root cause): 2. With fuzzy off, the old capped candidates/2 + find_normalized fallback is gone, so anything the SQL fold doesn't cover regresses vs main. Example: catalog "Karel Čapek" / ebook "Karel Capek", title "War with the Newts": main finds it via the "newts" title word; this PR returns nil (Č isn't in the list; verified ILIKE '%capek%' on translate('Karel Čapek') = false) -> duplicate book. Same for Czech/Polish/Hungarian/Romanian/Turkish accents. Simplest: on an exact_candidates miss, always run find_normalized over candidates/2 (fuzzy on or off), then find_similar only when fuzzy. One extra query, only on a miss. Add a Čapek-style test. Nit: 3. translate()+ILIKE '%..%' is a seq scan per ebook (candidates/2 already is). Fine at catalog sizes.
fix: keep normalized fallback for accents the SQL fold misses
All checks were successful
CI / test (pull_request) Successful in 1m57s
f7248cc9ae
Drop Ø/ø from the translate() list: normalize/1 keeps them (no NFD
decomposition), so folding them in SQL stopped 'nesbø' matching 'Nesbø'.
On an exact_candidates miss, run the normalized check over candidates/2
whether or not fuzzy matching is on, so Čapek/Capek still match as on main.
Author
Owner

Review from Nye (approved) — re-review at f7248cc

The blocker is fixed. Ø/ø are out of the SQL translate list, so the DB fold now matches normalize/1. On an exact_candidates miss, find_normalized now runs over candidates/2 whether fuzzy is on or off, which restores main's behavior for Č/Ł accents. I traced both new tests: they fail without the fix and pass with it. CI run 71 is green.

  • Note (non-blocking): the fallback is still capped at 200 rows by candidates/2, so an unfolded-accent match behind 200 rows sharing its longest word is still missed. Same as main.
  • Suggestion (optional follow-up): fold ø→o, ł→l, đ→d, ß→ss, æ→ae, œ→oe in both normalize/1 and the SQL list, so 'Nesbo' matches 'Nesbø' without fuzzy.
Review from Nye (approved) — re-review at f7248cc The blocker is fixed. Ø/ø are out of the SQL translate list, so the DB fold now matches `normalize/1`. On an exact_candidates miss, `find_normalized` now runs over `candidates/2` whether fuzzy is on or off, which restores main's behavior for Č/Ł accents. I traced both new tests: they fail without the fix and pass with it. CI run 71 is green. - **Note** (non-blocking): the fallback is still capped at 200 rows by `candidates/2`, so an unfolded-accent match behind 200 rows sharing its longest word is still missed. Same as main. - **Suggestion** (optional follow-up): fold ø→o, ł→l, đ→d, ß→ss, æ→ae, œ→oe in both `normalize/1` and the SQL list, so 'Nesbo' matches 'Nesbø' without fuzzy.
jfro merged commit d08d1132dc into main 2026-10-18 22:39:46 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/fuzzy_catalog!56
No description provided.