From af4c089e43ddd9a79c1a5a52abec805bebfaa191 Mon Sep 17 00:00:00 2001 From: wangjun Date: Mon, 3 Jul 2023 14:22:27 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0ElasticSearch?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user-guide/zh/infrastructure/batch.md | 2 +- .../user-guide/zh/infrastructure/cap.md | 2 +- .../user-guide/zh/infrastructure/elastic.md | 109 ++++++++++++++++++ docs/mkdocs.yml | 1 + 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 docs/content/user-guide/zh/infrastructure/elastic.md diff --git a/docs/content/user-guide/zh/infrastructure/batch.md b/docs/content/user-guide/zh/infrastructure/batch.md index d02d23f3..775b08b5 100644 --- a/docs/content/user-guide/zh/infrastructure/batch.md +++ b/docs/content/user-guide/zh/infrastructure/batch.md @@ -7,7 +7,7 @@ EFCore7.0 之后,提供了批量更新和批量删除,但是不提供批量新 - 添加以下 NuGet 包到你的项目 - Lion.AbpPro.EntityFrameworkCore.Mysql -- 添加 [DependsOn(typeof(LionAbpProEntityFrameworkCoreMysqlModule))] 到你的项目模块类. +- 添加 [DependsOn(typeof(AbpProEntityFrameworkCoreMysqlModule))] 到你的项目模块类. ## 原理 diff --git a/docs/content/user-guide/zh/infrastructure/cap.md b/docs/content/user-guide/zh/infrastructure/cap.md index 81f378d8..dd21ff33 100644 --- a/docs/content/user-guide/zh/infrastructure/cap.md +++ b/docs/content/user-guide/zh/infrastructure/cap.md @@ -9,7 +9,7 @@ - Lion.AbpPro.CAP.EntityFrameworkCore - DotNetCore.CAP.MySql (如果是其它数据库,请安装对应类型) - DotNetCore.CAP.RabbitMQ (如果是其它中间件,请安装对应类型) -- 添加 [DependsOn(typeof(LionAbpProCapEntityFrameworkCoreModule))] 到你的项目模块类. +- 添加 [DependsOn(typeof(AbpProCapEntityFrameworkCoreModule))] 到你的项目模块类. ## 配置 diff --git a/docs/content/user-guide/zh/infrastructure/elastic.md b/docs/content/user-guide/zh/infrastructure/elastic.md new file mode 100644 index 00000000..0ef59207 --- /dev/null +++ b/docs/content/user-guide/zh/infrastructure/elastic.md @@ -0,0 +1,109 @@ +# ElasticSearch + +## 安装 + +- 添加以下 NuGet 包到你的项目 + - Lion.AbpPro.ElasticSearch +- 添加 [DependsOn(typeof(AbpProElasticSearchModule))] 到你的项目模块类. + +## 配置 + +```json +{ + "ElasticSearch": { + "Host": "http://localhost:9200", + "UserName": "admin", + "Password": "1q2w3E*" + } +} +``` + +## 示例 + +- 实现 Student 的增删查改 + +### 定义学生类 + +- 实现 IElasticSearchEntity 接口,此接口有主键 Id,创建时间字段,也是泛型约束。 + +```csharp +public class Student : IElasticSearchEntity +{ + public Guid Id { get; set; } + + public DateTime CreationTime { get; set; } + + public double Price { get; set; } + + public string Name { get; set; } + + public int Age { get; set; } +} +``` + +### 定义接口 + +```csharp +public interface IStudentElasticSearchRepository : IBasicElasticSearchRepository +{ +} +``` + +### 实现接口 + +```csharp +public class StudentElasticSearchRepository : ElasticSearchRepository, IStudentElasticSearchRepository, ITransientDependency +{ + public StudentElasticSearchRepository(IElasticsearchProvider elasticsearchProvider) : base(elasticsearchProvider) + { + } + + // index 只能是小写 + protected override string IndexName => "Students".ToLower(); +} +``` + +### CURD + +```csharp +// 注入 +private readonly IStudentElasticSearchRepository _studentElasticSearchRepository; +public StudentElasticSearchRepositoryTests(IStudentElasticSearchRepository studentElasticSearchRepository) +{ + _studentElasticSearchRepository = studentElasticSearchRepository; +} + +// var student = new Student +// { +// Id = Guid.NewGuid(), +// Name = "韩立", +// Age = 10, +// CreationTime = DateTime.Now, +// Price = 100.3, +// }; + +// 根据主键Id查询 +var result = await _studentElasticSearchRepository.FindAsync(student.Id); + +// 新增 +await _studentElasticSearchRepository.InsertAsync(student); + +// 批量新增 +await _studentElasticSearchRepository.InsertManyAsync(students); + +// 更新 +await _studentElasticSearchRepository.UpdateAsync(student); + +// 删除 +await _studentElasticSearchRepository.DeleteAsync(student.Id); + +// 分页查询 +var mustFilters = new List, QueryContainer>>(); +mustFilters.Add(e => e.Term(f => f.Field(b => b.Name.Suffix("keyword")).Value("韩立"))); +var result = await _studentElasticSearchRepository.PageAsync(mustFilters); +``` + +### 其它操作 + +- 在 StudentElasticSearchRepository 中使用 Client 即可获取到 es 原生 api 对象。 +- 更多示例请查看单元测试(StudentElasticSearchRepositoryTests.cs) diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index bc4ee404..bbcc2af4 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -80,6 +80,7 @@ nav: - FreeSql: user-guide/zh/infrastructure/freesql.md - 分布式事件: user-guide/zh/infrastructure/cap.md - 批量操作: user-guide/zh/infrastructure/batch.md + - ElasticSearch: user-guide/zh/infrastructure/elastic.md - 应用模块: - 基础模块: user-guide/zh/modules/basic.md - 设置(Setting): user-guide/zh/modules/setting.md