Browse Source

docs: 添加ElasticSearch说明文档

pull/126/head
wangjun 3 years ago
parent
commit
af4c089e43
  1. 2
      docs/content/user-guide/zh/infrastructure/batch.md
  2. 2
      docs/content/user-guide/zh/infrastructure/cap.md
  3. 109
      docs/content/user-guide/zh/infrastructure/elastic.md
  4. 1
      docs/mkdocs.yml

2
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))] 到你的项目模块类.
## 原理

2
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))] 到你的项目模块类.
## 配置

109
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<Student>
{
}
```
### 实现接口
```csharp
public class StudentElasticSearchRepository : ElasticSearchRepository<Student>, 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<Func<QueryContainerDescriptor<Student>, 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)

1
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

Loading…
Cancel
Save