---
title: "Search uncommitted index changes in C#"
url: https://recursiveai.org/oneindex/cookbook/preview-uncommitted-search-edits/
description: "Refresh a OneIndex near-real-time reader to search a draft before committing it. Compare its results with a reader that only sees committed changes."
version: "0.4.0-preview.2"
---

# Search uncommitted index changes in C#

**OneIndex** is a free, open-source search library for .NET from Recursive AI. MIT or Apache-2.0, preview, runs in-process.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneIndex) · [GitHub](https://github.com/recursive-ai-oss/oneindex-csharp) · [Docs](https://recursiveai.org/oneindex/docs/) · [Download example (.zip)](https://recursiveai.org/oneindex/cookbook/preview-uncommitted-search-edits/preview-uncommitted-search-edits.zip)

Refresh a near-real-time reader to search drafts without committing them.

```shell
dotnet add package RecursiveAI.OneIndex --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/oneindex/cookbook/preview-uncommitted-search-edits/preview-uncommitted-search-edits.zip), then `dotnet run`.

```text
Preview: 1
Committed: 0
```

## The full program

```csharp
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;
}
```

```text
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:

```csharp
writer.DeleteById("draft");
```

```text
Preview: 0
Committed: 0
```

## Related recipes

- [Full-text search in C#](https://recursiveai.org/oneindex/cookbook/full-text-search-csharp/)
- [Rebuild a search index](https://recursiveai.org/oneindex/cookbook/rebuild-search-index/)

## 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](https://recursiveai.org/oneindex/docs/processes/#near-real-time-reader), 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.
