308 lines
5.7 KiB
Markdown
308 lines
5.7 KiB
Markdown
# es.dll 测试说明
|
||
|
||
## 测试文件说明
|
||
|
||
项目中有三种测试方式:
|
||
|
||
### 1. 单元测试文件 (`es_dll_test.go`)
|
||
使用 Go 标准的测试框架,包含完整的单元测试。
|
||
|
||
### 2. 命令行测试工具 (`test_dll.go`)
|
||
交互式命令行工具,可以手动测试各种功能。
|
||
|
||
### 3. 演示测试
|
||
自动化的完整演示流程。
|
||
|
||
---
|
||
|
||
## 方法一:运行单元测试
|
||
|
||
### 运行所有测试
|
||
```bash
|
||
cd es
|
||
go test -v
|
||
```
|
||
|
||
### 运行单个测试
|
||
```bash
|
||
cd es
|
||
go test -v -run TestListAllIndices
|
||
go test -v -run TestCreateDocument
|
||
go test -v -run TestCompleteWorkflow
|
||
```
|
||
|
||
### 查看测试覆盖率
|
||
```bash
|
||
go test -cover
|
||
```
|
||
|
||
---
|
||
|
||
## 方法二:使用命令行测试工具
|
||
|
||
### 基本语法
|
||
```bash
|
||
# 在项目根目录运行
|
||
go run test_es_dll.go <命令> [参数...]
|
||
```
|
||
|
||
### 查看帮助
|
||
```bash
|
||
go run test_es_dll.go
|
||
```
|
||
|
||
### 常用命令示例
|
||
|
||
#### 1. 查询所有索引
|
||
```bash
|
||
go run test_dll.go list
|
||
```
|
||
|
||
#### 2. 获取所有索引详细信息
|
||
```bash
|
||
go run test_dll.go info
|
||
```
|
||
|
||
#### 3. 创建索引
|
||
```bash
|
||
go run test_dll.go create my_index '{"properties":{"title":{"type":"text"},"author":{"type":"keyword"}}}'
|
||
```
|
||
|
||
#### 4. 查看索引详情
|
||
```bash
|
||
go run test_dll.go detail my_index
|
||
```
|
||
|
||
#### 5. 创建文档
|
||
```bash
|
||
go run test_dll.go add my_index doc1 '{"title":"测试文档","author":"张三","content":"这是测试内容"}'
|
||
```
|
||
|
||
#### 6. 获取文档
|
||
```bash
|
||
go run test_dll.go get my_index doc1
|
||
```
|
||
|
||
#### 7. 更新文档
|
||
```bash
|
||
go run test_dll.go update my_index doc1 '{"doc":{"title":"更新后的标题"}}'
|
||
```
|
||
|
||
#### 8. 搜索文档
|
||
```bash
|
||
# 搜索所有文档
|
||
go run test_dll.go search my_index '{"query":{"match_all":{}}}'
|
||
|
||
# 搜索特定内容
|
||
go run test_dll.go search my_index '{"query":{"match":{"title":"测试"}}}'
|
||
|
||
# 带分页的搜索
|
||
go run test_dll.go search my_index '{"query":{"match_all":{}},"from":0,"size":10}'
|
||
```
|
||
|
||
#### 9. 获取文档数量
|
||
```bash
|
||
go run test_dll.go count my_index
|
||
```
|
||
|
||
#### 10. 删除文档
|
||
```bash
|
||
go run test_dll.go del_doc my_index doc1
|
||
```
|
||
|
||
#### 11. 删除索引
|
||
```bash
|
||
go run test_dll.go delete my_index
|
||
```
|
||
|
||
#### 12. 运行完整演示
|
||
```bash
|
||
go run test_dll.go test
|
||
```
|
||
|
||
---
|
||
|
||
## 方法三:在代码中使用
|
||
|
||
### 示例代码
|
||
|
||
```go
|
||
package main
|
||
|
||
import (
|
||
"centerBook/es"
|
||
"encoding/json"
|
||
"fmt"
|
||
"log"
|
||
)
|
||
|
||
func main() {
|
||
// 1. 初始化DLL
|
||
dll, err := es.InitEsDLL()
|
||
if err != nil {
|
||
log.Fatalf("初始化失败: %v", err)
|
||
}
|
||
defer dll.Close()
|
||
|
||
// 2. 查询所有索引
|
||
result, err := dll.ListAllIndices()
|
||
if err != nil {
|
||
log.Fatalf("查询失败: %v", err)
|
||
}
|
||
|
||
// 3. 解析JSON结果
|
||
var data map[string]interface{}
|
||
json.Unmarshal([]byte(result), &data)
|
||
fmt.Printf("所有索引: %v\n", data)
|
||
|
||
// 4. 创建索引
|
||
mapping := `{
|
||
"properties": {
|
||
"title": {"type": "text"},
|
||
"author": {"type": "keyword"}
|
||
}
|
||
}`
|
||
result, _ = dll.CreateIndex("my_index", mapping)
|
||
fmt.Printf("创建索引结果: %s\n", result)
|
||
|
||
// 5. 添加文档
|
||
doc := `{"title": "Go语言编程", "author": "张三"}`
|
||
result, _ = dll.CreateDocument("my_index", "doc1", doc)
|
||
fmt.Printf("添加文档结果: %s\n", result)
|
||
|
||
// 6. 搜索文档
|
||
query := `{"query": {"match": {"title": "Go"}}}`
|
||
result, _ = dll.SearchDocuments("my_index", query)
|
||
fmt.Printf("搜索结果: %s\n", result)
|
||
|
||
// 7. 清理
|
||
dll.DeleteDocument("my_index", "doc1")
|
||
dll.DeleteIndex("my_index")
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## DLL 接口说明
|
||
|
||
所有方法返回的格式都是 JSON 字符串:
|
||
|
||
### 成功响应格式
|
||
```json
|
||
{
|
||
"success": true,
|
||
"message": "",
|
||
"data": {...}
|
||
}
|
||
```
|
||
|
||
### 错误响应格式
|
||
```json
|
||
{
|
||
"success": false,
|
||
"message": "错误信息"
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 注意事项
|
||
|
||
1. **DLL 文件位置**
|
||
- 确保 `dll/es.dll` 文件存在于项目根目录
|
||
- 如果提示找不到 DLL,检查路径是否正确
|
||
|
||
2. **JSON 参数格式**
|
||
- 在命令行中使用时,JSON 参数需要用单引号包裹
|
||
- Windows CMD 可能需要转义引号,建议使用 PowerShell
|
||
|
||
3. **错误处理**
|
||
- 所有方法都返回 `(string, error)`
|
||
- 使用时检查 error 是否为 nil
|
||
|
||
4. **资源释放**
|
||
- 使用 `defer dll.Close()` 确保 DLL 资源被释放
|
||
- C 字符串指针会自动释放
|
||
|
||
---
|
||
|
||
## 快速测试流程
|
||
|
||
### 完整测试流程(推荐新手)
|
||
|
||
```bash
|
||
# 1. 运行自动演示(最简单)
|
||
cd es
|
||
go run test_dll.go test
|
||
|
||
# 2. 手动测试各个功能
|
||
# 查看现有索引
|
||
go run test_dll.go list
|
||
|
||
# 创建测试索引
|
||
go run test_dll.go create test '{"properties":{"title":{"type":"text"}}}'
|
||
|
||
# 添加文档
|
||
go run test_dll.go add test doc1 '{"title":"Hello World"}'
|
||
|
||
# 查询文档
|
||
go run test_dll.go get test doc1
|
||
|
||
# 搜索
|
||
go run test_dll.go search test '{"query":{"match_all":{}}}'
|
||
|
||
# 删除文档
|
||
go run test_dll.go del_doc test doc1
|
||
|
||
# 删除索引
|
||
go run test_dll.go delete test
|
||
```
|
||
|
||
### 运行单元测试(推荐验证功能)
|
||
|
||
```bash
|
||
cd es
|
||
|
||
# 运行所有测试
|
||
go test -v
|
||
|
||
# 运行完整工作流测试
|
||
go test -v -run TestCompleteWorkflow
|
||
```
|
||
|
||
---
|
||
|
||
## 常见问题
|
||
|
||
### Q: 提示找不到 DLL
|
||
**A:** 检查 `dll/es.dll` 文件是否存在,路径是否正确
|
||
|
||
### Q: JSON 格式错误
|
||
**A:** 确保 JSON 字符串格式正确,使用在线工具验证
|
||
|
||
### Q: 命令行中 JSON 参数太复杂
|
||
**A:** 将 JSON 保存到文件,然后读取:
|
||
```bash
|
||
# Linux/Mac
|
||
go run test_dll.go search test "$(cat query.json)"
|
||
|
||
# Windows PowerShell
|
||
go run test_dll.go search test (Get-Content query.json -Raw)
|
||
```
|
||
|
||
---
|
||
|
||
## 测试检查清单
|
||
|
||
- [ ] DLL 能正常加载
|
||
- [ ] 能查询所有索引
|
||
- [ ] 能创建索引
|
||
- [ ] 能创建文档
|
||
- [ ] 能获取文档
|
||
- [ ] 能更新文档
|
||
- [ ] 能搜索文档
|
||
- [ ] 能删除文档
|
||
- [ ] 能删除索引
|
||
- [ ] C 字符串正确释放
|
||
- [ ] 资源正确释放
|