Skip to content

Commit

Permalink
docs: 添加ElasticSearch说明文档
Browse files Browse the repository at this point in the history
  • Loading branch information
WangJunZzz committed Jul 3, 2023
1 parent 0d1d9da commit af4c089
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/content/user-guide/zh/infrastructure/batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ EFCore7.0 之后,提供了批量更新和批量删除,但是不提供批量新

- 添加以下 NuGet 包到你的项目
- Lion.AbpPro.EntityFrameworkCore.Mysql
- 添加 [DependsOn(typeof(LionAbpProEntityFrameworkCoreMysqlModule))] 到你的项目模块类.
- 添加 [DependsOn(typeof(AbpProEntityFrameworkCoreMysqlModule))] 到你的项目模块类.

## 原理

Expand Down
2 changes: 1 addition & 1 deletion docs/content/user-guide/zh/infrastructure/cap.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- Lion.AbpPro.CAP.EntityFrameworkCore
- DotNetCore.CAP.MySql (如果是其它数据库,请安装对应类型)
- DotNetCore.CAP.RabbitMQ (如果是其它中间件,请安装对应类型)
- 添加 [DependsOn(typeof(LionAbpProCapEntityFrameworkCoreModule))] 到你的项目模块类.
- 添加 [DependsOn(typeof(AbpProCapEntityFrameworkCoreModule))] 到你的项目模块类.

## 配置

Expand Down
109 changes: 109 additions & 0 deletions docs/content/user-guide/zh/infrastructure/elastic.md
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit af4c089

Please sign in to comment.