HMENetCore.Elasticsearch.V2
8.0.19
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Standard 2.1
This package targets .NET Standard 2.1. The package is compatible with this framework or higher.
dotnet add package HMENetCore.Elasticsearch.V2 --version 8.0.19
NuGet\Install-Package HMENetCore.Elasticsearch.V2 -Version 8.0.19
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HMENetCore.Elasticsearch.V2" Version="8.0.19" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMENetCore.Elasticsearch.V2" Version="8.0.19" />
<PackageReference Include="HMENetCore.Elasticsearch.V2" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HMENetCore.Elasticsearch.V2 --version 8.0.19
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HMENetCore.Elasticsearch.V2, 8.0.19"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HMENetCore.Elasticsearch.V2@8.0.19
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HMENetCore.Elasticsearch.V2&version=8.0.19
#tool nuget:?package=HMENetCore.Elasticsearch.V2&version=8.0.19
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HMENetCore.Elasticsearch.V2
简介
HMENetCore.Elasticsearch.V2
是一个基于 Elastic.Clients.Elasticsearch (Elasticsearch 8.0) 的封装库,提供了对 Elasticsearch 的常用操作封装,包括索引管理、文档增删改查、批量操作、分页查询、滚动查询、SQL查询等功能.
功能特性
- 完整的CRUD操作封装
- 支持批量操作(插入、更新、删除)
- 丰富的查询方式(精确匹配、模糊匹配、分页查询等)
- 支持SQL查询
- 支持滚动查询(Scroll API)
- 支持聚合查询(Aggregations)
- 索引管理功能
- 依赖注入友好设计
- 线程安全
.NET支持
- 跨平台支持: Supports .NET Standard 2.1, .NET 6, .NET 8, and .NET 9.
安装
dotnet add package HMENetCore.Elasticsearch.V2 --version 8.0.12
配置数据库连接
添加配置到 appsettings.json
:
{
"ElasticSearchConfig": {
"Uris ": ["http://server1:9200","http://server2:9200"],
"UserName": "user",
"PassWord": "pwd",
"DefaultIndex": "default_index",
"EnableTls": false
}
}
服务注册
builder.Services.Configure<ElasticSearchConfig>(builder.Configuration.GetSection("ElasticSearchConfig"));
builder.Services.AddElasticSearchSetup(builder.Configuration.Get<ElasticSearchConfig>()!);
使用案例:
1.定义实体
[Table("person_index")]
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Description { get; set; }
public DateTime CreateTime { get; set; }
}
2.创建仓储
public class PersonRepository : BaseElasticSearchRepository<Person>
{
public PersonRepository(IElasticSearchContext context) : base(context)
{
}
}
3.业务服务
public class PersonService : BaseElasticSearchService<Person>
{
public PersonService(IElasticSearchCRUD<Person> repostory) : base(repostory)
{
}
}
4. 索引操作
// 创建索引
var success = await _personService.CreateIndexAsync(numberOfReplicas: 1, numberOfShards: 3);
// 判断索引是否存在
var exists = await _personService.IndexExistsAsync("person_index");
// 删除索引
var deleted = await _personService.DeleteIndexAsync("person_index");
// 清空索引数据
var cleared = await _personService.ClearIndexAsync("person_index");
文档CRUD案例
1. 插入文档
// 单条插入
var person = new Person { Id = "1", Name = "张三", Age = 25 };
var response = await _personService.InsertAsync(person);
// 批量插入
var persons = new List<Person>
{
new Person { Id = "2", Name = "李四", Age = 30 },
new Person { Id = "3", Name = "王五", Age = 28 }
};
var bulkResponse = await _personService.InsertBulkAsync(persons);
2. 更新文档
// 单条更新
person.Age = 26;
var updateResponse = await _personService.UpdateAsync(person.Id, person);
// 批量更新
persons.ForEach(p => p.Age += 1);
var bulkUpdateResponse = await _personService.UpdateBulkAsync(persons);
// 条件更新
var updateByQueryResponse = await _personService.UpdateByQueryAsync(
q => q.Query(q => q.Term(t => t.Field(f => f.Age).Value(30)),
"person_index");
3. 删除文档
// 按ID删除
var deleteResponse = await _personService.DeleteAsync("1");
// 按实体删除
var deleteResponse = await _personService.DeleteAsync(person);
// 批量删除
var ids = persons.Select(p => new Id(p.Id)).ToList();
var bulkDeleteResponse = await _personService.DeleteBulkAsync(ids);
// 条件删除
var deleteByQueryResponse = await _personService.DeleteByQueryAsync(
q => q.Query(q => q.Range(r => r.Field(f => f.Age).GreaterThan(30))),
"person_index");
4. 查询文档
// 按ID查询
var person = await _personService.GetByIdAsync("1");
// 批量按ID查询
var persons = await _personService.GetByIdAsync(new List<Id> { "1", "2" });
// 判断文档是否存在
var exists = await _personService.ExistsAsync("1");
// 查询所有
var allPersons = await _personService.GetByListAsync(q => q.MatchAll());
// 条件查询
var results = await _personService.GetByListAsync(
q => q.Bool(b => b.Must(
m => m.Term(t => t.Field(f => f.Name).Value("张三"),
m => m.Range(r => r.Field(f => f.Age).GreaterThan(20)
)),
sort: s => s.Field(f => f.Age, SortOrder.Desc),
size: 100
);
5. 分页查询
// 简单分页
var pageResult = await _personService.GetByPageAsync(
"张*",
pageIndex: 1,
pageSize: 10,
fields: p => p.Name
);
// 高级分页
var advancedPageResult = await _personService.GetByPageAsync(
q => q.Bool(b => b.Must(
m => m.Wildcard(w => w.Field(f => f.Name).Value("张*")),
m => m.Range(r => r.Field(f => f.Age).GreaterThan(20))
)),
pageIndex: 1,
pageSize: 10,
sort: s => s.Field(f => f.Age, SortOrder.Desc)
);
6. 滚动查询
// 获取初始scrollId
var scrollId = await _personService.SearchScrollByIdAsync(
q => q.MatchAll(),
size: 100,
times: "1m"
);
// 使用scrollId获取数据
var scrollResponse = await _personService.SearchScrollAsync(scrollId, "1m");
// 清除scroll
var cleared = await _personService.ClearScrollAsync(scrollId);
7. 多条件组合查询
var results = await _personService.SearchAsync(s => s
.Indices("person_index")
.Query(q => q
.Bool(b => b
.Must(m => m.Term(t => t.Field("name").Value("张三")))
.MustNot(m => m.Range(r => r.Field("age").LessThan(18)))
.Should(s => s
.Match(m => m.Field("description").Query("工程师")),
s => s.Match(m => m.Field("description").Query("经理"))
)
.Filter(f => f.DateRange(r => r.Field("createTime").GreaterThan("2023-01-01")))
)
)
.Sort(s => s.Field(f => f.Age, SortOrder.Desc))
.From(0)
.Size(10)
.Source(src => src
.Includes(i => i.Fields("id", "name", "age"))
.Excludes(e => e.Field("description"))
)
);
8. 聚合查询
var response = await _personService.SearchAsync(s => s
.Indices("person_index")
.Size(0)
.Aggregations(a => a
.Terms("age_groups", t => t
.Field(f => f.Age)
.Size(10)
)
.Average("avg_age", avg => avg
.Field(f => f.Age)
)
)
);
var ageGroups = response.Aggregations.GetTerms("age_groups");
var avgAge = response.Aggregations.GetAverage("avg_age");
9. SQL查询
// 使用SQL查询
var sqlResponse = await _personService.SqlQueryAsync("SELECT * FROM person_index WHERE age > 25 LIMIT 10");
// 使用SQL参数化查询
var sqlResponse = await _personService.SqlQueryAsync(q => q
.Query("SELECT * FROM person_index WHERE age > @age LIMIT @limit")
.Parameters(p => p
.Add("age", 25)
.Add("limit", 10)
)
);
核心组件
1. IElasticSearchContext
提供对 ElasticSearchContext 的基本操作接口。
2. BaseElasticSearchRepository
仓储层基类,实现了 IElasticSearchCRUD 接口,提供对特定实体的 CRUD 操作。
3. BaseElasticSearchService
服务层基类,封装了仓储层的操作,可以在此基础上添加业务逻辑。
版本兼容性
基于 Elastic.Clients.Elasticsearch v8.x 兼容 .NET 6.0 及以上版本 兼容 Elasticsearch 7.x 和 8.x 版本
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Elastic.Clients.Elasticsearch (>= 8.19.4)
- HMENetCore (>= 6.0.49)
- Microsoft.Extensions.Options (>= 9.0.8)
-
net6.0
- Elastic.Clients.Elasticsearch (>= 8.19.4)
- HMENetCore (>= 6.0.49)
- Microsoft.Extensions.Options (>= 8.0.2)
-
net8.0
- Elastic.Clients.Elasticsearch (>= 8.19.4)
- HMENetCore (>= 6.0.49)
- Microsoft.Extensions.Options (>= 9.0.8)
-
net9.0
- Elastic.Clients.Elasticsearch (>= 8.19.4)
- HMENetCore (>= 6.0.49)
- Microsoft.Extensions.Options (>= 9.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.