---
title: "Full-text search by word prefix in C#"
url: https://recursiveai.org/oneindex/cookbook/search-documents-by-word-prefix/
description: "Match document titles by the start of any indexed word in C#. Use OneIndex PrefixQuery and choose between a constant score and relevance ranking."
version: "0.4.0-preview.2"
---

# Full-text search by word prefix 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/search-documents-by-word-prefix/search-documents-by-word-prefix.zip)

`PrefixQuery` matches `pine` at the start of a word: “Pinewood,” not “Alpine.”

```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 OneIndex.Search;

using var index = SearchIndex.InMemory<Note>();
index.Upsert(new Note
    { Id = "1", Title = "Pine forest" });
index.Upsert(new Note
    { Id = "2", Title = "Trail to Pinewood" });
index.Upsert(new Note
    { Id = "3", Title = "Alpine trail" });
index.Commit();
var query = new PrefixQuery("Title", "pine");
foreach (var hit in index.SearchBy(query))
    Console.WriteLine(hit.Document.Title);

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

```text
Pine forest
Trail to Pinewood
```

## The full program

Unzip [the example](https://recursiveai.org/oneindex/cookbook/search-documents-by-word-prefix/search-documents-by-word-prefix.zip), then `dotnet run`.

The download runs the same `pine` query twice: once with the default score and once with term relevance. This index stores words in lowercase, so pass a lowercase prefix: `pine` matches, but `PINE` doesn’t. `PrefixQuery` compares the supplied prefix directly against the indexed terms. A field name that isn’t indexed for text or keywords raises `QueryException`.

```csharp
using System.Globalization;
using OneIndex;
using OneIndex.Search;

using var index = SearchIndex.InMemory<Note>();
index.Upsert(new Note
    { Id = "1", Title = "Pine forest" });
index.Upsert(new Note
    { Id = "2", Title = "Trail to Pinewood" });
index.Upsert(new Note
    { Id = "3", Title = "Alpine trail" });
index.Commit();
var query = new PrefixQuery("Title", "pine");
Print("Default", query);
Print("Ranked", query with { ScoreTerms = true });
Print("Uppercase input", new PrefixQuery("Title", "PINE"));

void Print(string label, PrefixQuery prefix)
{
    Console.WriteLine(label + ":");
    foreach (var hit in index.SearchBy(prefix))
    {
        string score = hit.Score.ToString("0.000", CultureInfo.InvariantCulture);
        Console.WriteLine($"  {hit.Document.Title}: {score}");
    }
}

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

`dotnet run` prints:

```text
Default:
  Pine forest: 1.000
  Trail to Pinewood: 1.000
Ranked:
  Pine forest: 0.531
  Trail to Pinewood: 0.490
Uppercase input:
```

## Variations

Rank the matching terms by relevance. In the snippet at the top, replace the `query` line with these lines. The download’s `Ranked:` block shows the resulting scores.

```csharp
var query = new PrefixQuery("Title", "pine")
    { ScoreTerms = true };
```

## Related recipes

- [Add full-text search to a C# app](https://recursiveai.org/oneindex/cookbook/full-text-search-csharp/)
- [Search despite typos](https://recursiveai.org/oneindex/cookbook/typo-tolerant-search/)

## The short answer

Use `SearchBy(new PrefixQuery("Title", "pine"))` to find documents with at least one indexed `Title` term beginning with `pine`. Pass the prefix itself, without `*`. The default query gives every matching document the same score; set `ScoreTerms = true` if you want the expanded terms to affect the ranking.

## What is the difference between prefix search and substring search?

Prefix search starts at a word boundary, not at the start of the title. The index contains separate terms for “Trail,” “to,” and “Pinewood,” so `pine` finds “Trail to Pinewood.” It doesn’t find “Alpine trail,” because `pine` starts inside `alpine`. If you need arbitrary fragments of product codes or names, a word-prefix query is the wrong match rule. If you need completed words only, a `TermQuery` does an exact lookup of an already analyzed term. See [the query semantics](https://recursiveai.org/oneindex/docs/queries/).

## Why doesn’t full-text search find some prefixes?

Check what the field actually indexed. The default analyzer splits text into words and folds case, so the title “Pine forest” contributes `pine`, not `Pine`. `PrefixQuery` is a low-level query: it compares UTF-8 bytes and does not analyze the text you pass it. For general user input, keep normalization consistent with the field’s analysis rather than relying on the casing in the original title. A language analyzer can also change words by stemming; a prefix of the original spelling isn’t necessarily a prefix of the term that was stored. For unfinished text in a search box, see [OneIndex’s user-text search rules](https://recursiveai.org/oneindex/docs/user-text/) before building your own query parser.

## How do you rank prefix search results by relevance?

With the default query, a document with two matching terms doesn’t outrank a document with one: both score the query’s boost, which is `1` here. Ties go to the lower internal document IDs. That makes sense for a filter where the only question is whether a title matched. It doesn’t mean the first hit is the best completion or that an exact `pine` outranks `pinewood`.

With `ScoreTerms = true`, each expanded term contributes its own BM25F score, based on statistics from the current index. Those scores are added when a document matches more than one term. Exact-prefix equality doesn’t get a special bonus. Keep the constant score if you don’t need term relevance, especially for very short prefixes: the scored query has a default expansion limit of 1,024 distinct terms and throws `TooManyTermsException` on overflow by default.
