Recursive AI

Split German compound words for 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.

Make “Schirm” find “Regenschirm.” This preview needs a word list; the example supplies two words inline.

Terminal
dotnet add package RecursiveAI.OneIndex --prerelease

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

C#
using OneIndex;
using OneIndex.Analysis;
 
var words = CompoundVocabulary.FromWords(
    ["regen", "schirm"]);
var splitter = new CompoundSplitter(words);
AnalyzerRegistry.Register("catalog-de", () =>
    new CustomAnalyzer("catalog-de",
        () => new StandardTokenizer(),
        input => new CaseFoldFilter(input),
        input => new DecompoundFilter(
            input, splitter)));
 
using var index =
    SearchIndex.InMemory<Product>();
index.Upsert(new Product
    { Name = "Regenschirm" });
index.Commit();
foreach (var hit in index.Search("Schirm"))
    Console.WriteLine(hit.Document.Name);
 
public class Product
{
    [SearchId]
    public string Id { get; set; } = "1";
    [FullText(Analyzer = "catalog-de")]
    public string Name { get; set; } = "";
}
Text
Regenschirm

index.Search("Regenschirm") doesn’t match here: OneIndex treats its multiple terms as a phrase. Use the TermQuery in the full program to match the whole word.

Unzip the example, then dotnet run.

The full program

OneIndex 0.4.0-preview.2’s NuGet build doesn’t include the packed German word list. The program uses regen and schirm instead and prints the terms from plain German analysis and from the analyzer used to index Name. The number after @ is the token position.

C#
using OneIndex;
using OneIndex.Analysis;
using OneIndex.Search;
 
const string word = "Regenschirm";
var terms = CompoundVocabulary.FromWords(
    ["regen", "schirm"]);
var splitter = new CompoundSplitter(terms);
AnalyzerRegistry.Register("catalog-de", () =>
    new CustomAnalyzer("catalog-de",
        () => new StandardTokenizer(),
        input => new CaseFoldFilter(input),
        input => new DecompoundFilter(input, splitter)));
 
foreach (string name in new[] { "de", "catalog-de" })
{
    var analyzer = AnalyzerRegistry.Create(name);
    var tokens = analyzer.Analyze(word)
        .Select(token => $"{token.Term}@{token.Position}");
    Console.WriteLine($"{name}: {string.Join(", ", tokens)}");
}
 
using var index = SearchIndex.InMemory<Product>();
index.Upsert(new Product { Name = word });
index.Commit();
 
foreach (var hit in index.Search("Schirm"))
    Console.WriteLine($"Schirm -> {hit.Document.Name}");
 
var exact = new TermQuery(nameof(Product.Name),
    "regenschirm");
foreach (var hit in index.SearchBy(exact))
    Console.WriteLine($"regenschirm -> {hit.Document.Name}");
 
public class Product
{
    [SearchId]
    public string Id { get; set; } = "umbrella";
 
    [FullText(Analyzer = "catalog-de")]
    public string Name { get; set; } = "";
}
Text
de: regenschirm@0
catalog-de: regenschirm@0, regen@0, schirm@0
Schirm -> Regenschirm
regenschirm -> Regenschirm

If a word can’t be split using the configured vocabulary, the filter keeps the whole word without adding parts.

Full-text search in C# · Search with synonyms


The short answer

In C#, build a CompoundSplitter from a German word list, put DecompoundFilter after CaseFoldFilter, and register the analyzer before you open the index. Assign the registered name to the full-text field. The example indexes regenschirm, regen and schirm together, so the single-word query “Schirm” returns the original product name.

What is decompounding?

Decompounding adds recognized parts of a word to the search index. A product name might say “Regenschirm” (umbrella) with no space even though the shopper enters “Schirm.” A search that matches only whole terms will not find schirm inside regenschirm. The filter emits both the original token and the recognized parts, without rewriting the displayed product name.

The parts share the compound’s position. They are not two consecutive words, so don’t use a phrase query for consecutive “Regen Schirm” to stand in for the compound. To require the field to contain the whole term, use the TermQuery in the full program. Unlike index.Search(text), it looks up the term as-is, so pass the lowercase form the analyzer indexed.

How do you search languages with compound words?

For a narrow catalog, start with the parts you want to match and check the analyzer’s tokens on real product names. The two-word list here illustrates the setup, not a general-purpose German dictionary. This custom analyzer folds case and splits compounds; it doesn’t include the stemming or stop words of the built-in de analyzer.

For a larger vocabulary, CompoundVocabulary.Read accepts one word per line, optionally followed by a tab and a count. OneIndex tries splits that cover the whole word, not arbitrary substrings. It prefers fewer parts by default, then uses the parts’ counts to choose between splits with the same number of parts. A frequent whole word can be left unsplit. German linking letters such as the s in “Liebesbrief” can be allowed with CompoundOptions.LinkingElements when you build the splitter. See decompounding for the defaults and dictionary format.

After you change a vocabulary, rebuild the documents indexed with the old analyzer if you want older entries to match newly recognized parts. Reopening an existing index with the new word list won’t add those terms to the existing index entries.

All oneindex recipes