fix(fts): fix multi-word field-restricted search query building

Fixes a critical bug in FTS query building where multi-word searches
with field restrictions incorrectly used OR between all word+field
combinations instead of requiring ALL words to match within at least
one field.

Example: searching "cute cat" in {title, tags} previously produced:
  title:cute* OR title:cat* OR tags:cute* OR tags:cat*
Which matched recipes with ANY word in ANY field.

Now produces:
  (title:cute* title:cat*) OR (tags:cute* tags:cat*)
Which requires ALL words to match within at least one field.

Also adds fallback to fuzzy search when FTS returns empty results,
improving search reliability.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Will Miao
2026-01-22 10:25:03 +08:00
parent 9150718edb
commit 17c5583297
3 changed files with 86 additions and 7 deletions

View File

@@ -632,7 +632,12 @@ class RecipeScanner:
fields = None
try:
return self._fts_index.search(search, fields)
result = self._fts_index.search(search, fields)
# Return None if empty to trigger fuzzy fallback
# Empty FTS results may indicate query syntax issues or need for fuzzy matching
if not result:
return None
return result
except Exception as exc:
logger.debug("FTS search failed, falling back to fuzzy search: %s", exc)
return None