fix: wire advanced FTS5 parser into local keyword search - #67
Conversation
The operator-aware ftsQuery parser (AND/OR/NOT, column filters, prefix terms, NEAR, syntax validation) was only exercised by unit tests; the runtime keyword path used the simpler localFtsQuery, so documented advanced syntax was silently ignored and syntax errors never surfaced. - local-backend: route searchLocalKeywordExact through ftsQuery so advanced operators, column filters, and syntax validation work as documented; delete the now-dead localFtsQuery - local-backend: fix NEAR passthrough — tokenizeFtsQuery now captures NEAR(...) as a unit and normalizes the forgiving comma form (NEAR(a, b, 5)) to FTS5's space-separated form (NEAR(a b, 5)) - local-backend: treat a leading hyphen as punctuation in simple mode (e.g. "sqlite -wal" searches for both terms) instead of rejecting it - local-backend: correct the FTS5 syntax hint (NEAR uses space-separated terms, not commas) - server: surface FtsQuerySyntaxError as text content instead of re-throwing, so MCP clients can read the hint and retry - index: special-case FtsQuerySyntaxError in the CLI catch blocks with a clearer "Invalid search syntax" header - search-solutions.md: show the canonical NEAR(token nft, 5) form
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
bernoussama
left a comment
There was a problem hiding this comment.
Nightly review — clankeroverflow#67 (fix: wire advanced FTS5 parser into local keyword search)
Verdict: Approve with nits. Good change: kills the ad-hoc localFtsQuery string munging and routes exact search through the validated ftsQuery tokenizer (consistent syntax + quoting), normalizes NEAR(a, b, 5) → NEAR(a b, 5), and surfaces FtsQuerySyntaxError cleanly in both CLI and MCP. Tests cover the new paths.
Nits:
captureNear()backtracks a hardcoded 4 chars (query.slice(parenIndex - 4, ...)) for theNEARword.NEAR (a b)(space before paren) isn't captured by the/^NEAR$/guard path and can produce a confusing error or wrong tokens — compute the start fromcurrent.lengthinstead.normalizeNear()passes inner terms through raw without quoting/escaping —NEAR(a"b)can yield an unbalanced FTS string and a raw SQLite error instead of your clean syntax error. Quote terms like the rest of the pipeline does.- Behavior change to flag: simple mode now silently strips a leading dash (
-wal→"wal"), which inverts user intent for anyone typing an exclusion. Old error message was arguably more correct; consider stripping only when the dash is followed by a digit (negative numbers / version strings), erroring otherwise.
Summary
Resolves the deferred issue from #66: the operator-aware
ftsQueryparser (AND/OR/NOT, column filters, prefix terms, NEAR, syntax validation) was only exercised by unit tests — the runtime keyword path used the simplerlocalFtsQuery, so documented advanced syntax was silently ignored and syntax errors never surfaced.This PR wires
ftsQueryinto the runtime keyword-search path and fixes three latent bugs that wiring exposed.Changes
Core
local-backend.ts: routesearchLocalKeywordExactthroughftsQueryso advanced operators, column filters, and syntax validation work as documented; delete the now-deadlocalFtsQuery.Latent bugs fixed (discovered during implementation)
tokenizeFtsQuerysplit(into a separate token, soNEAR(token nft, 5)could never survive as a unit. Now capturesNEAR(...)as a single token and normalizes the forgiving comma form (NEAR(a, b, 5)) to FTS5's real space-separated form (NEAR(a b, 5)) — verified againstbetter-sqlite3.search-solutions.mdand theftsSyntaxErrormessage to the canonicalNEAR(term term [, N])form.buildSimpleQuerycaptured-walthen rejected it as bare negation, which would have broken the existing"sqlite -wal"integration test. Now treats a leading hyphen as punctuation (strips it), matching the oldlocalFtsQuerybehavior.Error surfacing
server.ts:FtsQuerySyntaxErrornow returns its message as text content instead of re-throwing, so MCP clients can read the hint and retry.index.ts: bothsearchandlocal searchcatch blocks show a clearer "Invalid search syntax:" header forFtsQuerySyntaxError.Tests
Notes
tags : "auth") was verified valid against real SQLite — no change needed there.Validation