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
{
MaxResults = 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:
MaxResults
: Maximum number of results to return (default: 100)MinScore
: Minimum relevance score for matches (default: 0.0)IncludeRawDocument
: Whether to include the full document in results (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);
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.