---
title: "Retrieve stored fields from a search result in C#"
url: https://recursiveai.org/oneindex/cookbook/read-stored-fields-from-search-hit/
description: "Read OneIndex stored fields by a search hit’s document ID in C#. Convert the hit ID to a segment-local ID and fetch the title or URL without a database lookup."
version: "0.4.0-preview.2"
---

# Retrieve stored fields from a search result 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/read-stored-fields-from-search-hit/read-stored-fields-from-search-hit.zip)

Read field values by document ID from the search index instead of fetching the original record.

With the typed `SearchIndex<T>` API, just read `hit.Document`; this recipe uses the low-level `IndexSearcher`.

```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 schema = new SchemaBuilder().Id("id")
    .Text("title").Build();
var storage = new MemoryStorage();
using var writer =
    IndexWriter.Open(storage, schema);
writer.Upsert(new Document().Add("id", "a2")
    .Add("title", "Pump Repair"));
writer.Commit();
using var snapshot = writer.OpenSnapshot();
var searcher =
    SnapshotSearch.CreateSearcher(snapshot);
var query = new TermQuery("title", "pump");
var hits = searcher.Search(query, 5).Hits;
foreach (var hit in hits)
{
    var leaf = searcher.Context.Leaves
        .Last(p => p.DocBase <= hit.DocId);
    int localId = hit.DocId - leaf.DocBase;
    var doc = leaf.Reader.StoredFields!
        .GetDocument(localId);
    var title = doc.Get("title").AsText();
    Console.WriteLine(title);
}
```

```text
Pump Repair
```

Unzip [the example](https://recursiveai.org/oneindex/cookbook/read-stored-fields-from-search-hit/read-stored-fields-from-search-hit.zip), then `dotnet run`.

## The full program

The program uses OneIndex 0.4.0-preview.2. It commits “Welcome” and “Pump Repair” separately, then opens a reader on the committed index. Only the second title matches. Its hit ID is 1, but the record is document 0 in its segment. The URL is stored for display but isn’t searchable.

```csharp
using OneIndex.Index;
using OneIndex.Search;
using OneIndex.Storage;

var schema = new SchemaBuilder().Id("id")
    .Text("title")
    .Add(new FieldDefinition("url", FieldType.Keyword) { Indexed = false })
    .Build();
var storage = new MemoryStorage();
using (var writer = IndexWriter.Open(storage, schema))
{
    writer.Upsert(new Document().Add("id", "a1")
        .Add("title", "Welcome")
        .Add("url", "/welcome"));
    writer.Commit();
    writer.Upsert(new Document().Add("id", "a2")
        .Add("title", "Pump Repair")
        .Add("url", "/guide/pump"));
    writer.Commit();
}

using var snapshot = IndexSnapshot.Open(storage);
var searcher = SnapshotSearch.CreateSearcher(snapshot);
var result = searcher.Search(new TermQuery("title", "pump"), 5);
foreach (var hit in result.Hits)
{
    var leaf = searcher.Context.Leaves
        .Last(leaf => leaf.DocBase <= hit.DocId);
    var stored = leaf.Reader.StoredFields
        ?? throw new InvalidOperationException("No stored fields.");
    int localId = hit.DocId - leaf.DocBase;
    var doc = stored.GetDocument(localId);
    Console.WriteLine($"Hit {hit.DocId}, segment-local ID {localId}");
    Console.WriteLine(doc.Get("id").AsText());
    Console.WriteLine(doc.Get("title").AsText());
    var url = doc.Get("url");
    if (url.Kind == FieldValueKind.Text)
        Console.WriteLine(url.AsText());
}
```

```text
Hit 1, segment-local ID 0
a2
Pump Repair
/guide/pump
```

If a field wasn’t stored, `Document.Get` returns an empty value; check its `Kind` before calling a typed accessor such as `AsText()`.

## Related recipes

[Full-text search in C#](https://recursiveai.org/oneindex/cookbook/full-text-search-csharp/) · [Page search results with a cursor](https://recursiveai.org/oneindex/cookbook/paginate-search-results-with-cursor/)

---

## The short answer

With OneIndex’s low-level search API, take the result’s `DocId`, select the segment whose `DocBase` is the last one at or below that ID, and subtract `DocBase`. Pass that segment-local number to `leaf.Reader.StoredFields.GetDocument`. The returned `Document` holds the field values that were stored when the record was indexed. Read the `title` as text, rather than reconstructing it from the terms that matched.

## What is the difference between stored and indexed fields?

An indexed field contributes to search. A stored field keeps its original value for retrieval. `title` in this example does both: the term `pump` matches, but the value read back retains the capitalization of “Pump Repair.” The full program declares `url` with `Indexed = false`. It can still be shown next to a title because `Stored` defaults to `true`. OneIndex also preserves the order of a multi-valued field’s stored values.

Store the values you want at result time, such as a title, display URL or short summary. If you set `Stored = false`, the value is absent from the decoded document even if the field is indexed and can match a query. On a segment with no stored fields at all, `StoredFields` can be null. The [stored-source reference](https://recursiveai.org/oneindex/docs/stored-source/) describes which values OneIndex retains.

## How do you retrieve a document by ID from a search index?

First identify which kind of ID you have. `DocId` on a low-level hit is a number in the searcher’s snapshot, not the record’s key. Use the same search context to locate the segment and convert that number for its stored-fields reader. In the example, the key `a2` is itself a stored value, distinct from both the hit’s ID `1` and the segment-local ID `0`. Use the record key for links or later lookups rather than passing the hit number to a different snapshot after an update or merge.

If your application uses the typed `SearchIndex<T>` API instead, OneIndex does the stored-source lookup for you when it builds `hit.Document`. The manual retrieval in this recipe is for `IndexSearcher`, whose result contains the document number and score. See [mapping](https://recursiveai.org/oneindex/docs/mapping/#the-index) for the typed path.
