# es.dll 使用教程 ## 1.创建DLL工具实例 ### 加载DLL文件~~~~ ```gotemplate // EsDLL Elasticsearch工具DLL结构 type esDLL struct { dll *syscall.DLL listAllIndices *syscall.Proc // 查询所有索引 getIndicesInfo *syscall.Proc // 获取所有索引的详细信息 getIndexDetail *syscall.Proc // 获取单个索引的详细信息 createIndex *syscall.Proc // 创建索引 deleteIndex *syscall.Proc // 删除索引 getDocumentCount *syscall.Proc // 获取索引文档数量 createDocument *syscall.Proc // 创建文档 getDocument *syscall.Proc // 根据ID获取文档 updateDocument *syscall.Proc // 更新文档 deleteDocument *syscall.Proc // 删除文档 searchDocuments *syscall.Proc // 搜索文档 freeCString *syscall.Proc // 释放C字符串 } // 初始化esDLL func InitEsDLL() (*esDLL, error) { dllPath := filepath.Join("dll", "es.dll") if _, err := os.Stat(dllPath); os.IsNotExist(err) { return nil, fmt.Errorf("es DLL 不存在: %s", dllPath) } if dll, err := syscall.LoadDLL(dllPath); err != nil { return nil, fmt.Errorf("加载es DLL 失败: %s", err) } else { return &esDLL{ dll: dll, listAllIndices: dll.MustFindProc("ListAllIndices"), getIndicesInfo: dll.MustFindProc("GetIndicesInfo"), getIndexDetail: dll.MustFindProc("GetIndexDetail"), createIndex: dll.MustFindProc("CreateIndex"), deleteIndex: dll.MustFindProc("DeleteIndex"), getDocumentCount: dll.MustFindProc("GetDocumentCount"), createDocument: dll.MustFindProc("CreateDocument"), getDocument: dll.MustFindProc("GetDocument"), updateDocument: dll.MustFindProc("UpdateDocument"), deleteDocument: dll.MustFindProc("DeleteDocument"), searchDocuments: dll.MustFindProc("SearchDocuments"), freeCString: dll.MustFindProc("FreeCString"), }, nil } } dll, err := InitEsDLL() ``` ### 获取C字符串 ```gotemplate // cStr 获取C字符串 func (m *esDLL) cStr(p uintptr) string { if p == 0 { return "" } b := []byte{} for i := uintptr(0); ; i++ { c := *(*byte)(unsafe.Pointer(p + i)) if c == 0 { break } b = append(b, c) } s := string(b) if m.freeCString != nil { m.freeCString.Call(p) } return s } ``` ## 2. 使用dll函数示例 ```gotemplate // 查询所有索引 func (m *esDLL) ListAllIndices() (string, error) { proc, err := m.dll.FindProc("ListAllIndices") if err != nil { return "", fmt.Errorf("找不到函数 ListAllIndices: %v", err) } resultPtr, _, _ := proc.Call() result := m.cStr(resultPtr) return result, nil } ``` # 接口详情 ## 1.查询所有索引--ListAllIndices ### 请求信息 ```gotemplate dll.ListAllIndices() ``` ### 请求参数 无 ### 响应示例 ```json { "success": true, "message": "", "data": ["index1", "index2", "index3"] } ``` ## 2. 获取所有索引的详细信息--GetIndicesInfo ### 请求信息 ```gotemplate dll.GetIndicesInfo() ``` ### 请求参数 无 ### 响应示例 ```json { "success": true, "message": "", "data": [ { "index": "index1", "health": "green", "status": "open", "uuid": "abc123", "pri": "1", "rep": "1", "docs.count": "100", "docs.deleted": "0", "store.size": "1.2kb", "pri.store.size": "600b" } ] } ``` ## 3. 获取单个索引的详细信息--GetIndexDetail ### 请求信息 ```gotemplate dll.GetIndexDetail(indexName) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|----------| | indexName | string | 是 | 索引名称 | ### 响应示例 ```json { "success": true, "message": "", "data": { "index1": { "aliases": {}, "mappings": {}, "settings": { "index": { "creation_date": "1234567890000", "number_of_shards": "1", "number_of_replicas": "1", "uuid": "abc123", "version": { "created": "7080099" } } } } } } ``` ## 4. 创建索引--CreateIndex ### 请求信息 ```gotemplate dll.CreateIndex(indexName, mapping) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|---------------| | indexName | string | 是 | 索引名称 | | mapping | string | 是 | 索引映射的JSON字符串 | ### 响应示例 ```json { "success": true, "message": "", "data": { } } ``` ## 5. 删除索引--DeleteIndex ### 请求信息 ```gotemplate dll.DeleteIndex(indexName) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|---------------| | indexName | string | 是 | 索引名称 | ### 响应示例 ```json { "success": true, "message": "" } ``` ## 6. 获取索引文档数量--GetDocumentCount ### 请求信息 ```gotemplate dll.GetDocumentCount(indexName) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|---------------| | indexName | string | 是 | 索引名称 | ### 响应示例 ```json { "success": true, "message": "", "data": 100 } ``` ## 7. 创建文档--CreateDocument ### 请求信息 ```gotemplate dll.CreateDocument(indexName, id, doc) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|---------------| | indexName | string | 是 | 索引名称 | | id | string | 是 | 文档ID | | doc | string | 是 | 文档内容的JSON字符串 | ### 响应示例 ```json { "success": true, "message": "" } ``` ## 8. 根据ID获取文档--GetDocument ### 请求信息 ```gotemplate dll.GetDocument(indexName, id) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|---------------| | indexName | string | 是 | 索引名称 | | id | string | 是 | 文档ID | ### 响应示例 ```json { "success": true, "message": "", "data": { "_id": "1", "_index": "index1", "_source": { "title": "示例文档", "content": "这是文档内容" } } } ``` ## 9. 更新文档--UpdateDocument ### 请求信息 ```gotemplate dll.UpdateDocument(indexName, id, updateData) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|---------------| | indexName | string | 是 | 索引名称 | | id | string | 是 | 文档ID | | updateData | string | 是 | 更新数据的JSON字符串 | ### 响应示例 ```json { "success": true, "message": "" } ``` ## 10. 删除文档--DeleteDocument ### 请求信息 ```gotemplate dll.DeleteDocument(indexName, id) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|---------------| | indexName | string | 是 | 索引名称 | | id | string | 是 | 文档ID | ### 响应示例 ```json { "success": true, "message": "" } ``` ## 11. 搜索文档--SearchDocuments ### 请求信息 ```gotemplate dll.SearchDocuments(indexName, query) ``` ### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--|--|--|--------------| | indexName | string | 是 | 索引名称 | | query | string | 是 | 查询条件的JSON字符串| #### query示例 ```gotemplate query := map[string]interface{}{ "query": map[string]interface{}{ "term": map[string]interface{}{ fieldName: fieldValue, }, }, "size": size, } ``` ### 响应示例 ```json { "success": true, "message": "", "data": [ { "_id": "1", "title": "示例文档", "content": "这是文档内容" }, { "_id": "2", "title": "另一个文档", "content": "更多内容" } ] } ```