SearchLite is a lightweight .NET library that provides full-text search capabilities on SQLite and PostgreSQL databases. It offers a simple, unified interface for creating, managing, and querying search indexes without the complexity of dedicated search engines.
- Unified search interface for SQLite and Postgres
- Strongly-typed document indexing and querying
- Flexible filtering with LINQ-style expressions
- Configurable search options including scoring and result limits
- Async-first API design
- Minimal dependencies
dotnet add package SearchLite- First, define your searchable document by implementing
ISearchableDocument:
public class Product : ISearchableDocument
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string GetSearchText() => $"{Id} {Name} {Description}";
}- Create a search manager for your database:
// For PostgreSQL
var searchManager = new PgSearchManager("your_connection_string");
// For SQLite
var searchManager = new SqliteSearchManager("your_connection_string");- Get or create a search index:
var productIndex = await searchManager.Get<Product>("products", CancellationToken.None);- Index some documents:
var product = new Product
{
Id = "1",
Name = "Gaming Laptop",
Description = "High-performance gaming laptop with RTX 4080",
Price = 1999.99m
};
await productIndex.IndexAsync(product);- Search for documents:
var request = new SearchRequest<Product>
{
Query = "gaming laptop",
Options = new SearchOptions
{
Take = 10,
MinScore = 0.5f
}
}.Where(p => p.Price < 2000);
var results = await productIndex.SearchAsync(request);
foreach (var match in results.Matches)
{
Console.WriteLine($"Found: {match.Document.Name} (Score: {match.Score})");
}The SearchOptions class allows you to customize your search behavior:
Take: Maximum number of results to return (default: 100)Skip: Number of results to skip, for paging (default: 0)MinScore: Minimum relevance score for matches (default: 0.0). Not comparable across providers.IncludeRawDocument: Whether to include the full document in results (default: true)IncludePartialMatches: Whether the query matches on individual terms rather than the full query (default: true)
SearchLite supports LINQ-style filtering using the Where method:
var request = new SearchRequest<Product>
{
Query = "laptop"
}
.Where(p => p.Price >= 1000 && p.Price <= 2000);Filters and ordering can reach into nested objects within a document, at any depth:
// Documents can hold nested objects and collections
public class Product : ISearchableDocument
{
public string Id { get; set; }
public string Name { get; set; }
public Manufacturer Maker { get; set; } // nested object
public List<string> Tags { get; set; } // collection field
public string GetSearchText() => $"{Id} {Name}";
}
public class Manufacturer
{
public string Name { get; set; }
public Address HeadOffice { get; set; }
}
// Filter and order by nested fields
var request = new SearchRequest<Product>()
.Where(p => p.Maker.Name == "Acme" && p.Maker.HeadOffice.City == "Oslo")
.OrderByAscending(p => p.Maker.Name);Use Contains on a document's own collection field to match documents whose array
holds a given value:
// Match products tagged "sale"
.Where(p => p.Tags.Contains("sale"))
// Negation works too
.Where(p => !p.Tags.Contains("discontinued"))This is distinct from
In-style filters, where a fixed set is tested against a single field — e.g.var ids = new[] { "1", "2" }; request.Where(p => ids.Contains(p.Id));
On PostgreSQL, equality and collection-membership filters compile to JSONB containment
(document @> '{...}'), which is served by the GIN(jsonb_path_ops) index SearchLite
creates on every index — so these filters stay fast as collections grow. Range and string
comparisons use JSON path extraction. SQLite provides the same query capabilities using
json_extract and json_each.
For better performance when indexing multiple documents:
var products = new List<Product> { /* ... */ };
await productIndex.IndexManyAsync(products);// Delete a single document
await productIndex.DeleteAsync("doc_id");
// Drop the entire index
await productIndex.DropIndexAsync();Contributions are welcome! Please feel free to submit a Pull Request.