Generic Repository implementations for Elasticsearch.
$ dotnet add package Foundatio.RepositoriesA production-grade repository pattern library for .NET with Elasticsearch implementation. Built on Foundatio building blocks, it provides a clean abstraction over data access with powerful features like caching, messaging, soft deletes, and versioning.
dotnet add package Foundatio.Repositories.Elasticsearch
using Foundatio.Repositories.Models;
public class Employee : IIdentity, IHaveDates, ISupportSoftDeletes
{
public string Id { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public int Age { get; set; }
public DateTime CreatedUtc { get; set; }
public DateTime UpdatedUtc { get; set; }
public bool IsDeleted { get; set; }
}
using Foundatio.Repositories.Elasticsearch.Configuration;
public sealed class EmployeeIndex : VersionedIndex<Employee>
{
public EmployeeIndex(IElasticConfiguration configuration)
: base(configuration, "employees", version: 1) { }
public override TypeMappingDescriptor<Employee> ConfigureIndexMapping(
TypeMappingDescriptor<Employee> map)
{
return map
.Dynamic(false)
.Properties(p => p
.SetupDefaults()
.Text(f => f.Name(e => e.Name).AddKeywordAndSortFields())
.Text(f => f.Name(e => e.Email).AddKeywordAndSortFields())
.Number(f => f.Name(e => e.Age).Type(NumberType.Integer))
);
}
}
using Foundatio.Repositories;
using Foundatio.Repositories.Elasticsearch;
public interface IEmployeeRepository : ISearchableRepository<Employee> { }
public class EmployeeRepository : ElasticRepositoryBase<Employee>, IEmployeeRepository
{
public EmployeeRepository(MyElasticConfiguration configuration)
: base(configuration.Employees) { }
}
// Add
var employee = await repository.AddAsync(new Employee
{
Name = "John Doe",
Email = "john@example.com",
Age = 30
});
// Query
var results = await repository.FindAsync(q => q
.FilterExpression("age:>=25")
.SortExpression("name"));
// Update
employee.Age = 31;
await repository.SaveAsync(employee);
// Soft delete
employee.IsDeleted = true;
await repository.SaveAsync(employee);
// Hard delete
await repository.RemoveAsync(employee);
IReadOnlyRepository<T> - Read operations (Get, Find, Count, Exists)IRepository<T> - Write operations (Add, Save, Remove, Patch)ISearchableRepository<T> - Dynamic querying with filters, sorting, and aggregationsEntityChanged messages)ChangeType.Removed)IsDeletedActiveOnly, DeletedOnly, AllIndex<T> - Basic index configurationVersionedIndex<T> - Schema versioning with migrationsDailyIndex<T> - Time-series with daily partitioningMonthlyIndex<T> - Time-series with monthly partitioningDocumentsAdding / DocumentsAddedDocumentsSaving / DocumentsSavedDocumentsRemoving / DocumentsRemovedDocumentsChanging / DocumentsChangedBeforeQuery - Query interceptionBeforePublishEntityChanged - Notification interceptionVisit the full documentation for detailed guides:
See the sample Blazor application for a complete working example.
We welcome contributions! Please see our contributing guidelines for details.
Licensed under the Apache License, Version 2.0. See LICENSE.txt for details.