Skip to main content

escape_fts5_query

Function escape_fts5_query 

Source
pub fn escape_fts5_query(input: &str) -> String
Expand description

Turn arbitrary user text into an FTS5 MATCH expression that cannot be a syntax error and cannot mean something the user did not write.

FTS5’s match syntax is a language: AND, OR, NOT, NEAR, prefix *, column filters like title:, and quoted phrases. Passing a raw search box through to it has two failure modes, and neither is acceptable as a default. A query containing an unbalanced quote or a bare AND raises SQLITE_ERROR — the user typed a search and got an exception. And a query containing NOT silently means something: searching for cats not dogs quietly excludes documents, which is a wrong answer rather than an error.

So each run of alphanumeric characters becomes one double-quoted term and everything else is dropped, leaving implicit AND between terms. A caller who genuinely wants the query language can pass it through with HybridSearch::raw_match.