r/dotnet • u/Living-Temporary1133 • 5h ago
Promotion TantivyDotNet: embedded full-text search eninge for .NET on top of Tantivy (Rust), 1.0 RC
For a while I've wanted something between "LIKE '%foo%'" and running Elasticsearch next to my app, and the main in-process option in .NET is still Lucene.NET 4.8, which has been in beta for years. Tantivy is a Rust search library in the same spirit as Lucene, so I wrote a .NET binding for it.
It ships as one NuGet package with the native library for Windows x64, Linux x64/arm64 and macOS x64/arm64, so there's no Rust toolchain on your side. It targets .NET 10+ and works with trimming and NativeAOT.
using var index = SearchIndex.Create("./index", new SchemaBuilder()
.AddTextField("title", stored: true)
.AddTextField("body", stored: true)
.AddI64Field("year", fast: true)
.Build());
using (var writer = index.CreateWriter())
{
writer.AddDocument(new { title = "Of Mice and Men", body = "...", year = 1937 });
writer.Commit();
}
using var searcher = index.CreateSearcher();
var results = searcher.Search("mice AND men", new SearchOptions
{
OrderBy = [SortBy.Field("year", descending: true)],
Snippets = new SnippetOptions("body"),
});
You can use query strings or build queries in code, and there are facets, snippets, sorting with search-after paging, and Elasticsearch-style aggregations. For servers, a SearcherManager hands out one shared searcher and refreshes it after commits.
I benchmarked it against Lucene.NET on 215k Wikipedia articles (setup and caveats in the repo):
Indexing is 2-3x faster and most queries are 4-7x faster. Phrase queries are about 4% slower, and that's Tantivy itself: called directly from Rust it takes about the same time.
The API is frozen as of the release candidate, and 1.0 ships once it's gone a few weeks without changes. The first feedback already turned up a real bug (paging cursors misbehaved after the index merged), fixed in rc.2. If something in the API looks off to you, now is when I can still change it.
dotnet add package TantivyDotNet --prerelease
Repo: https://github.com/aminparsa18/TantivyDotNet
Docs: https://aminparsa18.github.io/TantivyDotNet/



