Select snippet(notes, 0, '[', ']', '...', 4) in your MATCH query to return a short excerpt with matches in brackets.
dotnet add package RecursiveAI.OneLiteRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneLite; await using var db = await Database.OpenAsync(":memory:"); await db.ExecuteAsync($""" CREATE VIRTUAL TABLE notes USING fts5(body) """); string body = "Check the bicycle tires " + "and tighten the brakes"; await db.ExecuteAsync( $"INSERT INTO notes VALUES ({body})"); Console.WriteLine( await db.ScalarAsync<string>($""" SELECT snippet(notes, 0, '[', ']', '...', 4) FROM notes WHERE notes MATCH {"bicycle"} """));
OneLite sends the interpolated {…} values as SQL parameters; it doesn’t paste them into the SQL text.
Unzip the example, then dotnet run.
Check the [bicycle] tires...The full program
The download adds a second note and reads all matching excerpts as strings. Only the bicycle note matches. A valid search with no matches returns an empty list; malformed FTS5 query syntax throws OneLiteException even when the search string is bound as a parameter.
using RecursiveAI.OneLite; await using var db = await Database.OpenAsync(":memory:"); await db.ExecuteAsync($""" CREATE VIRTUAL TABLE notes USING fts5(body) """); await db.ExecuteAsync($""" INSERT INTO notes VALUES ('Before riding check the bicycle tires and tighten the brakes'), ('Pack lunch before leaving') """); var excerpts = await db.QueryAsync<string>($""" SELECT snippet(notes, 0, '[', ']', '...', 4) FROM notes WHERE notes MATCH {"bicycle"} """); foreach (var excerpt in excerpts) Console.WriteLine(excerpt);
...the [bicycle] tires and...Variations
Replace the SELECT line in the full program: -1 lets FTS5 choose the excerpt column, so it still picks body in this one-column table.
SELECT snippet(notes, -1, '[', ']', '...', 4)To return the whole body with the same markers, replace that line with:
SELECT highlight(notes, 0, '[', ']')Before riding check the [bicycle] tires and tighten the brakesRelated recipes
The short answer
Select snippet(notes, 0, '[', ']', '...', 4) in the same query that filters with WHERE notes MATCH {"bicycle"}. The MATCH expression finds rows; snippet() extracts part of column 0 from each of those rows. In C#, read the result as a string, just as you would a selected text column.
How do you use the SQLite FTS5 snippet function?
The SQLite FTS5 documentation lists six arguments. Pass the FTS5 table itself first, not the column name or a quoted string. Next comes the zero-based column number. The third and fourth arguments bracket a match; the fifth marks where text was omitted at either end. Use 1 to 64 for the last argument, the excerpt length in tokens. A token is a unit of text produced by the table’s tokenizer, such as a word with the default tokenizer. The limit is not a character count, so a long word can still make a wide result.
What’s the difference between snippet and highlight in FTS5?
highlight() marks matches in the entire column. Use it when the field is short or the reader has already opened the result. snippet() selects a window from the column, which is useful in a search result list. OneLite’s FTS5 notes describe how it favors windows containing more distinct query phrases and gives a bonus to certain sentence starts. The window need not start at the first match, and other occurrences can fall outside the excerpt. If your index uses an external content table, keep that content available and in sync: the index can identify a row, but the excerpt needs its original text.
How do you escape SQLite FTS5 snippets for HTML?
The markers are inserted into the original text; they don’t make that text safe to interpret as HTML. If you only need a preview, render the result as text, as the console example does. If you render styled match spans on a web page, encode the document text and add only markup generated by your application. Don’t render the database string as raw HTML just because the opening and closing markers are fixed.