PrefixQuery matches pine at the start of a word: “Pinewood,” not “Alpine.”
dotnet add package RecursiveAI.OneIndex --prereleaseRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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; } = ""; }
Pine forest Trail to Pinewood
The full program
Unzip the example, 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.
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:
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.
var query = new PrefixQuery("Title", "pine") { ScoreTerms = true };
Related recipes
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.
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 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.