---
title: "Find similar documents in C#"
url: https://recursiveai.org/oneindex/cookbook/find-similar-documents/
description: "Find documents similar to an indexed ID or new text in C# using lexical more-like-this search. No embeddings or model download are required."
version: "0.4.0-preview.2"
---

# Find similar documents 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/find-similar-documents/find-similar-documents.zip)

More-like-this search finds documents with useful shared words, without embeddings.

```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;
using var index = SearchIndex.InMemory<Page>();
index.Upsert([
    new() { Id = "1", Body = "cat kitten" },
    new() { Id = "2", Body = "kitten care" }
]);
index.Commit();
foreach (var hit in index.MoreLikeThis("1"))
    Console.WriteLine(hit.Id);
public class Page
{
    [SearchId]
    public string Id { get; set; } = "";
    [FullText]
    public string Body { get; set; } = "";
}
```

```text
2
```

Unzip [the example](https://recursiveai.org/oneindex/cookbook/find-similar-documents/find-similar-documents.zip), then `dotnet run`.

## The full program

The snippet at the top returns document `2` for source `1`. The full program creates six short documents, searches from an existing ID, then searches from new text. All inputs are inline, and the index is in memory. IDs are sorted for readable output rather than displayed in score order.

This example uses 0.4.0-preview.2; APIs and file formats may change between previews.

```csharp
using OneIndex;

using var index = SearchIndex.InMemory<Page>();
index.Upsert([
    new() { Id = "cats", Body = "feline kitten whiskers purring litter feline grooming" },
    new() { Id = "kitten", Body = "kitten litter whiskers feline playing" },
    new() { Id = "grooming", Body = "grooming feline fur brushing kitten" },
    new() { Id = "rocket", Body = "orbit rocket thrust launch payload" },
    new() { Id = "satellite", Body = "orbit payload satellite launch" },
    new() { Id = "kitchen", Body = "recipe oven flour sugar" }
]);
index.Commit();
Console.WriteLine("Like cats: " + string.Join(", ",
    index.MoreLikeThis("cats", x => x.Body).Execute().Select(h => h.Id).Order()));
Console.WriteLine("Like new text: " + string.Join(", ",
    index.MoreLikeThisText("rocket launch into orbit", x => x.Body)
        .Execute().Select(h => h.Id).Order()));

public class Page
{
    [SearchId] public string Id { get; set; } = "";
    [FullText] public string Body { get; set; } = "";
}
```

`dotnet run` prints:

```text
Like cats: grooming, kitten
Like new text: rocket, satellite
```

A missing source ID throws `SourceDocumentNotFoundException`; a query with no candidate terms returns no hits.

## Variations

Insert this before the full program’s `Page` class. Only terms repeated in the source are eligible; all selected terms must match:

```csharp
var query = OneIndex.Search.MoreLikeThisQuery
    .FromDocument("cats") with
{
    Fields = ["Body"],
    MinTermFrequency = 2,
    MaxQueryTerms = 5,
    MinimumShouldMatchFraction = 1
};
Console.WriteLine("Repeated terms: " + string.Join(", ",
    index.SearchBy(query).Execute().Select(h => h.Id).Order()));
```

The extra line is:

```text
Repeated terms: grooming, kitten
```

## Related recipes

[Full-text search](https://recursiveai.org/oneindex/cookbook/full-text-search-csharp/) · [Vector search](https://recursiveai.org/oneindex/cookbook/vector-search-dotnet/) · [Hybrid search](https://recursiveai.org/oneindex/cookbook/hybrid-search-reranking-csharp/)

---

## The short answer

Use `index.MoreLikeThis(id, x => x.Body)` to find documents resembling an indexed document, or `index.MoreLikeThisText(text, x => x.Body)` for text that is not already indexed. OneIndex selects useful analyzed terms and searches for other documents containing them. The source document is excluded by default.

This is lexical similarity, not vector nearest-neighbor search. The six-document program needs only the core package, not a model, embedding service, or credentials. The [more-like-this guide](https://recursiveai.org/oneindex/docs/more-like-this/) specifies the selection and matching rules.

## How do I find similar documents in C#?

Index full-text fields and commit them before searching. The first full-program query uses `cats` as an identifier, reads its stored source, and selects terms from `Body`. It returns the kitten and grooming documents, not the source itself. Choosing a field explicitly keeps an unrelated title or another full-text property out of the selection.

Without explicit fields, the query uses all indexed full-text fields. Each field’s own analyzer produces its terms. A field configured with stemming or stop-word removal can therefore select different probes from a field with only the standard analyzer. Similarity is measured over those analyzed terms, not over the original strings as opaque values.

For a recommendation panel, keep the source-ID lookup tied to a live document. The missing-source case throws rather than silently substituting a generic search. If the document was deleted or is not visible in the searched snapshot, handle that case in the caller.

## How does a more-like-this query work?

The engine counts terms in the source and weights candidates by source count times inverse document frequency. A word repeated in the source and uncommon in the collection is a stronger probe than a common word that appears once. The best weighted terms become optional clauses, with a required share of those clauses set by `MinimumShouldMatchFraction`.

The documented defaults are a minimum term frequency of one, minimum document frequency of one, at most 25 selected terms, and a matching fraction of 0.3 with at least one required clause. For a document source, a term found only in that source is dropped because it cannot find another document. Equal-weight selections break ties by field and term in ordinal order.

The variation raises the source-frequency threshold to two, caps selection at five terms, and requires every selected term to match. In this corpus only `feline` repeats in the source body, so the same two neighboring documents remain. That output is a result for these six documents, not an accuracy score for a recommendation system.

By default, term clauses are boosted relative to the strongest selected term. Set selection thresholds to suit the collection rather than increasing every threshold together: a short source can easily run out of candidates. A query with no candidates matches nothing.

The second query supplies `rocket launch into orbit` directly and returns the rocket and satellite documents. It does not insert that text into the index. This is useful for a draft or a pasted passage whose related content you want to find before saving it.

New-text and source-ID requests share the same term-selection machinery, but only an ID source has a document to exclude. `IncludeSource` can opt an ID source back in when you intentionally need it among the matches. Supply exactly one source form: an ID or text, not both.

Terms are selected against the snapshot being searched, so the result depends on the indexed collection as well as the source. The guide states that highlights are empty for this query because its words are not known until execution. Build a related-document UI from the returned documents and their stored fields; do not assume ordinary keyword highlight fragments will accompany these hits.
