Recursive AI

Accent-insensitive full-text search in C#

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

Set the title’s analyzer to fr so cafe finds “Café” without changing the displayed text.

Terminal
dotnet add package RecursiveAI.OneIndex --prerelease

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneIndex;
 
using var index = SearchIndex.InMemory<Note>();
index.Upsert(new Note
    { Id = "1", Title = "Café à Paris" });
index.Upsert(new Note
    { Id = "2", Title = "Le café du matin" });
index.Upsert(new Note
    { Id = "3", Title = "Thé à Lyon" });
index.Commit();
// The trailing space ends the word.
foreach (var hit in index.Search("cafe "))
    Console.WriteLine(hit.Document.Title);
 
public class Note
{
    [SearchId]
    public string Id { get; set; } = "";
    [FullText(Language = "fr")]
    public string Title { get; set; } = "";
}
Text
Café à Paris
Le café du matin

The full program

Unzip the example, then dotnet run.

The download indexes the same three titles twice, once with [FullText(Language = "fr")] and once with plain [FullText]. Search analyzes each query using the field’s analyzer. The trailing space marks the input as a completed word so the example doesn’t test prefix search. The default analyzer folds case but keeps accents: cafe gives no hits there, while café gives two.

C#
using OneIndex;
 
Note[] notes = [
    new() { Id = "1", Title = "Café à Paris" },
    new() { Id = "2", Title = "Le café du matin" },
    new() { Id = "3", Title = "Thé à Lyon" },
];
using var french = SearchIndex.InMemory<Note>();
using var standard = SearchIndex.InMemory<StandardNote>();
foreach (var note in notes)
{
    french.Upsert(note);
    standard.Upsert(new StandardNote { Id = note.Id, Title = note.Title });
}
french.Commit();
standard.Commit();
 
foreach (string word in new[] { "cafe", "café" })
{
    // A trailing space marks the word as complete.
    string query = word + " ";
    Console.WriteLine($"fr, {word}:");
    foreach (var hit in french.Search(query))
        Console.WriteLine($"  {hit.Document.Title}");
    Console.WriteLine($"standard, {word}: {standard.Search(query).Execute().Count}");
}
 
public class Note
{
    [SearchId]
    public string Id { get; set; } = "";
    [FullText(Language = "fr")]
    public string Title { get; set; } = "";
}
 
public class StandardNote
{
    [SearchId]
    public string Id { get; set; } = "";
    [FullText]
    public string Title { get; set; } = "";
}

dotnet run prints:

Text
fr, cafe:
  Café à Paris
  Le café du matin
standard, cafe: 0
fr, café:
  Café à Paris
  Le café du matin
standard, café: 2

Variations

Readers can type the accent too. Replace the foreach loop in the snippet at the top with:

C#
foreach (var hit in index.Search("café "))
    Console.WriteLine(hit.Document.Title);
Text
Café à Paris
Le café du matin

The short answer

Put [FullText(Language = "fr")] on the French title property and pass the original query text to index.Search. OneIndex uses the field’s analyzer for both the title and the query, so cafe and café both find the same accented titles here. Keep the original title for display.

How do you search text without accents in C#?

An analyzer turns text into searchable terms. The French analyzer splits the text into words, folds case, and applies French language rules including elision, stop words, and stemming. Stemming reduces related word forms to shared search terms. Accent folding runs after stemming because the stemmer can use accented letters. The source documents this order in Analyzers.cs.

Set the language for the text you actually index. The French analyzer is not a switch that only ignores accents: it can also remove common French words and change the terms that represent other words. Since stemming happens first, don’t assume that every accented word and every unaccented spelling will end up with an identical term. Try representative names and words from your own collection before changing a field that readers already search.

OneIndex has other language analyzers, including Spanish (es) and Italian (it) with accent folding, but no built-in accent-only analyzer.

No. Under the standard analyzer, case folding makes Café and café searchable as the same word, but cafe lacks the accent that is still present in the indexed term. Lowercasing the input, or removing accents just from the query, doesn’t make an index built with the standard analyzer accent-insensitive. The download gives the standard and French indexes the same titles and both spellings, so the difference comes from the analyzer, not the data.

Can you make an existing full-text field accent-insensitive without reindexing?

Not by changing only the query. The old field still contains terms made by the original analyzer. OneIndex’s mapping guide says opening an existing index with a different schema fails and schema evolution isn’t built in. Create a new index with the updated mapping and populate it with your original documents. Run sample queries against that index before switching your app to it, including both accent spellings and any words whose treatment by the language analyzer matters to readers.

All oneindex recipes