Refresh a near-real-time reader to search drafts without committing them.
dotnet add package RecursiveAI.OneIndex --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneIndex.Index; using OneIndex.Search; using OneIndex.Storage; var storage = new MemoryStorage(); var schema = new SchemaBuilder() .Id("id").Text("body").Build(); using var writer = IndexWriter.Open(storage, schema); writer.Commit(); using var published = IndexReader.Open(storage); using var preview = writer.OpenNearRealTimeReader(); writer.Upsert(new Document().Add("id", "draft") .Add("body", "coffee")); preview.Refresh(); published.Refresh(); Console.WriteLine($"Preview: {Count(preview)}"); Console.WriteLine($"Committed: {Count(published)}"); static long Count(IndexReader reader) { using var view = reader.Acquire(); var search = SnapshotSearch.CreateSearcher(view); var query = new TermQuery("body", "coffee"); return search.Search(query, 5).TotalHits; }
Unzip the example, then dotnet run.
Preview: 1 Committed: 0
The full program
using OneIndex.Index; using OneIndex.Search; using OneIndex.Storage; var storage = new MemoryStorage(); var schema = new SchemaBuilder() .Id("id").Text("body").Build(); using var writer = IndexWriter.Open(storage, schema); writer.Commit(); using var published = IndexReader.Open(storage); using var preview = writer.OpenNearRealTimeReader(); writer.Upsert(new Document().Add("id", "draft") .Add("body", "coffee")); preview.Refresh(); published.Refresh(); Console.WriteLine($"Preview: {Count(preview)}"); Console.WriteLine($"Committed: {Count(published)}"); writer.Commit(); published.Refresh(); Console.WriteLine($"After commit: {Count(published)}"); static long Count(IndexReader reader) { using var view = reader.Acquire(); var search = SnapshotSearch.CreateSearcher(view); var query = new TermQuery("body", "coffee"); return search.Search(query, 5).TotalHits; }
Preview: 1 Committed: 0 After commit: 1
Both readers are refreshed after inserting draft, but only the preview finds coffee before the second commit. After committing, refresh the published reader again to pick up the new commit.
A preview can show work that won’t survive a writer crash or disposal without Commit; dispose the preview reader before disposing its writer.
Variations
Preview a deletion. Insert this before preview.Refresh() to remove the draft you just added:
writer.DeleteById("draft");Preview: 0 Committed: 0
Related recipes
The short answer
Call writer.OpenNearRealTimeReader(), apply your edits, then call Refresh() on that reader. Acquire a new snapshot from it and build the searcher from that snapshot. The preview includes the writer’s additions and deletions without publishing a commit. A reader opened with IndexReader.Open(storage) still sees only committed data, even if you refresh it at the same point.
What is near-real-time search?
In OneIndex, a near-real-time reader is a search view tied to one open writer. Refreshing it flushes buffered documents into a segment and reopens the view if something changed. It includes the deletes as of that refresh, too. This is useful for a preview within the process that owns the writer: an editor can check search results before publishing the edits. A reader in a separate process can’t open that writer’s preview.
A snapshot acquired before a refresh doesn’t update under your feet. Acquire a fresh snapshot after the refresh, and build the searcher from that snapshot rather than reusing one bound to the older view. Dispose snapshots when you’re done with them so the view doesn’t keep old files pinned unnecessarily.
What is the difference between refresh and commit?
Refresh() changes what one reader can see. On the preview reader, it exposes writer changes that might still be uncommitted. On a committed reader, it checks for a newer commit and switches to it only if there is one. Commit() is the writer operation that publishes changes to committed readers; a reader still needs a fresh open or refresh to see them. The example uses memory so there’s no disk index to clean up. With disk storage, a preview still doesn’t provide the durability of a commit.
If an editor should see drafts but public search should not, use the preview reader only for the editor path and use a committed reader for the public path. If you get the old result after a search edit, check which reader made the snapshot before assuming the edit failed.