![]() |
VOOZH | about |
dotnet add package HMENetCore.Elasticsearch --version 8.1.17
NuGet\Install-Package HMENetCore.Elasticsearch -Version 8.1.17
<PackageReference Include="HMENetCore.Elasticsearch" Version="8.1.17" />
<PackageVersion Include="HMENetCore.Elasticsearch" Version="8.1.17" />Directory.Packages.props
<PackageReference Include="HMENetCore.Elasticsearch" />Project file
paket add HMENetCore.Elasticsearch --version 8.1.17
#r "nuget: HMENetCore.Elasticsearch, 8.1.17"
#:package HMENetCore.Elasticsearch@8.1.17
#addin nuget:?package=HMENetCore.Elasticsearch&version=8.1.17Install as a Cake Addin
#tool nuget:?package=HMENetCore.Elasticsearch&version=8.1.17Install as a Cake Tool
HMENetCore.Elasticsearch 8.0 是一个基于 Elastic.Clients.Elasticsearch (Elasticsearch 8.0) 的封装库,提供了对 Elasticsearch 的常用操作封装,包括索引管理、文档增删改查、批量操作、分页查询、滚动查询、SQL查询等功能.
dotnet add package HMENetCore.Elasticsearch
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>()!);
[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; }
}
public class PersonRepository : BaseElasticSearchRepository<Person>
{
public PersonRepository(IElasticSearchContext context) : base(context)
{
}
}
public class PersonService : BaseElasticSearchService<Person>
{
public PersonService(IElasticSearchCRUD<Person> repostory) : base(repostory)
{
}
}
// 创建索引
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");
// 单条插入
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);
// 单条更新
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");
// 按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");
// 按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
);
// 简单分页
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)
);
// 获取初始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);
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"))
)
);
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");
// 使用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)
)
);
提供对 ElasticSearchContext 的基本操作接口。
仓储层基类,实现了 IElasticSearchCRUD 接口,提供对特定实体的 CRUD 操作。
服务层基类,封装了仓储层的操作,可以在此基础上添加业务逻辑。
基于 Elastic.Clients.Elasticsearch v8.x 兼容 .NET 6.0 及以上版本 兼容 Elasticsearch 8.x 和 9.x 版本
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 8.1.17 | 103 | 5/18/2026 |
| 8.1.16 | 156 | 3/17/2026 |
| 8.1.15 | 126 | 2/28/2026 |
| 8.1.13 | 113 | 2/24/2026 |
| 8.1.12 | 129 | 1/14/2026 |
| 8.1.11 | 462 | 12/10/2025 |
| 8.1.10 | 208 | 11/24/2025 |
| 8.1.5 | 196 | 11/7/2025 |
| 8.1.3 | 213 | 10/20/2025 |
| 8.1.2 | 218 | 9/24/2025 |
| 7.1.17 | 89 | 5/18/2026 |
| 7.1.15 | 126 | 3/17/2026 |
| 7.1.13 | 111 | 2/24/2026 |
| 7.1.12 | 119 | 1/14/2026 |
| 7.1.11 | 454 | 12/10/2025 |
| 7.1.10 | 208 | 11/24/2025 |
| 7.1.2 | 192 | 10/20/2025 |
| 7.1.1 | 220 | 9/24/2025 |