package main // //import ( // "centerBook/es" // "encoding/json" // "fmt" // "log" // "os" //) // //func printUsage() { // fmt.Println("es.dll 测试工具") // fmt.Println("================") // fmt.Println("使用方法:") // fmt.Println(" go run test_dll.go <命令> [参数...]") // fmt.Println() // fmt.Println("可用命令:") // fmt.Println(" list - 查询所有索引") // fmt.Println(" info - 获取所有索引的详细信息") // fmt.Println(" detail <索引名> - 获取单个索引的详细信息") // fmt.Println(" create <索引名> <映射JSON> - 创建索引") // fmt.Println(" delete <索引名> - 删除索引") // fmt.Println(" count <索引名> - 获取索引文档数量") // fmt.Println(" get <索引名> <文档ID> - 获取文档") // fmt.Println(" add <索引名> <文档ID> - 创建文档") // fmt.Println(" update <索引名> <文档ID> - 更新文档") // fmt.Println(" del_doc <索引名> <文档ID> - 删除文档") // fmt.Println(" search <索引名> <查询JSON> - 搜索文档") // fmt.Println() // fmt.Println("示例:") // fmt.Println(" go run test_dll.go list") // fmt.Println(" go run test_dll.go create my_index '{\"properties\":{\"title\":{\"type\":\"text\"}}}'") // fmt.Println(" go run test_dll.go add my_index doc1 '{\"title\":\"测试文档\"}'") // fmt.Println(" go run test_dll.go search my_index '{\"query\":{\"match_all\":{}}}'") //} // //func main() { // if len(os.Args) < 2 { // printUsage() // os.Exit(1) // } // // // 初始化DLL // dll, err := es.InitEsDLL() // if err != nil { // log.Fatalf("初始化DLL失败: %v", err) // } // defer dll.Close() // // command := os.Args[1] // // switch command { // case "list": // handleListAllIndices(dll) // case "info": // handleGetIndicesInfo(dll) // case "detail": // if len(os.Args) < 3 { // fmt.Println("错误: 请提供索引名") // os.Exit(1) // } // handleGetIndexDetail(dll, os.Args[2]) // case "create": // if len(os.Args) < 4 { // fmt.Println("错误: 请提供索引名和映射JSON") // os.Exit(1) // } // handleCreateIndex(dll, os.Args[2], os.Args[3]) // case "delete": // if len(os.Args) < 3 { // fmt.Println("错误: 请提供索引名") // os.Exit(1) // } // handleDeleteIndex(dll, os.Args[2]) // case "count": // if len(os.Args) < 3 { // fmt.Println("错误: 请提供索引名") // os.Exit(1) // } // handleGetDocumentCount(dll, os.Args[2]) // case "get": // if len(os.Args) < 4 { // fmt.Println("错误: 请提供索引名和文档ID") // os.Exit(1) // } // handleGetDocument(dll, os.Args[2], os.Args[3]) // case "add": // if len(os.Args) < 5 { // fmt.Println("错误: 请提供索引名、文档ID和文档JSON") // os.Exit(1) // } // handleCreateDocument(dll, os.Args[2], os.Args[3], os.Args[4]) // case "update": // if len(os.Args) < 5 { // fmt.Println("错误: 请提供索引名、文档ID和更新数据JSON") // os.Exit(1) // } // handleUpdateDocument(dll, os.Args[2], os.Args[3], os.Args[4]) // case "del_doc": // if len(os.Args) < 4 { // fmt.Println("错误: 请提供索引名和文档ID") // os.Exit(1) // } // handleDeleteDocument(dll, os.Args[2], os.Args[3]) // case "search": // if len(os.Args) < 4 { // fmt.Println("错误: 请提供索引名和查询JSON") // os.Exit(1) // } // handleSearchDocuments(dll, os.Args[2], os.Args[3]) // case "test": // runDemo(dll) // default: // fmt.Printf("未知命令: %s\n", command) // printUsage() // os.Exit(1) // } //} // //func printJSONResult(result string) { // // 尝试格式化JSON输出 // var data interface{} // if err := json.Unmarshal([]byte(result), &data); err == nil { // formatted, _ := json.MarshalIndent(data, "", " ") // fmt.Println(string(formatted)) // } else { // fmt.Println(result) // } //} // //func handleListAllIndices(dll *es.EsDLL) { // result, err := dll.ListAllIndices() // if err != nil { // log.Fatalf("查询失败: %v", err) // } // fmt.Println("=== 查询所有索引 ===") // printJSONResult(result) //} // //func handleGetIndicesInfo(dll *es.EsDLL) { // result, err := dll.GetIndicesInfo() // if err != nil { // log.Fatalf("查询失败: %v", err) // } // fmt.Println("=== 所有索引详细信息 ===") // printJSONResult(result) //} // //func handleGetIndexDetail(dll *es.EsDLL, indexName string) { // result, err := dll.GetIndexDetail(indexName) // if err != nil { // log.Fatalf("查询失败: %v", err) // } // fmt.Printf("=== 索引 '%s' 详情 ===\n", indexName) // printJSONResult(result) //} // //func handleCreateIndex(dll *es.EsDLL, indexName, mapping string) { // result, err := dll.CreateIndex(indexName, mapping) // if err != nil { // log.Fatalf("创建索引失败: %v", err) // } // fmt.Printf("=== 创建索引 '%s' ===\n", indexName) // printJSONResult(result) //} // //func handleDeleteIndex(dll *es.EsDLL, indexName string) { // result, err := dll.DeleteIndex(indexName) // if err != nil { // log.Fatalf("删除索引失败: %v", err) // } // fmt.Printf("=== 删除索引 '%s' ===\n", indexName) // printJSONResult(result) //} // //func handleGetDocumentCount(dll *es.EsDLL, indexName string) { // result, err := dll.GetDocumentCount(indexName) // if err != nil { // log.Fatalf("获取文档数量失败: %v", err) // } // fmt.Printf("=== 索引 '%s' 文档数量 ===\n", indexName) // printJSONResult(result) //} // //func handleGetDocument(dll *es.EsDLL, indexName, docID string) { // result, err := dll.GetDocument(indexName, docID) // if err != nil { // log.Fatalf("获取文档失败: %v", err) // } // fmt.Printf("=== 获取文档 %s/%s ===\n", indexName, docID) // printJSONResult(result) //} // //func handleCreateDocument(dll *es.EsDLL, indexName, docID, doc string) { // result, err := dll.CreateDocument(indexName, docID, doc) // if err != nil { // log.Fatalf("创建文档失败: %v", err) // } // fmt.Printf("=== 创建文档 %s/%s ===\n", indexName, docID) // printJSONResult(result) //} // //func handleUpdateDocument(dll *es.EsDLL, indexName, docID, updateData string) { // result, err := dll.UpdateDocument(indexName, docID, updateData) // if err != nil { // log.Fatalf("更新文档失败: %v", err) // } // fmt.Printf("=== 更新文档 %s/%s ===\n", indexName, docID) // printJSONResult(result) //} // //func handleDeleteDocument(dll *es.EsDLL, indexName, docID string) { // result, err := dll.DeleteDocument(indexName, docID) // if err != nil { // log.Fatalf("删除文档失败: %v", err) // } // fmt.Printf("=== 删除文档 %s/%s ===\n", indexName, docID) // printJSONResult(result) //} // //func handleSearchDocuments(dll *es.EsDLL, indexName, query string) { // result, err := dll.SearchDocuments(indexName, query) // if err != nil { // log.Fatalf("搜索文档失败: %v", err) // } // fmt.Printf("=== 搜索索引 '%s' ===\n", indexName) // printJSONResult(result) //} // //func runDemo(dll *es.EsDLL) { // fmt.Println("=== 运行演示测试 ===") // // // 1. 查询所有索引 // fmt.Println("\n1. 查询所有索引") // result, _ := dll.ListAllIndices() // printJSONResult(result) // // // 创建测试索引 // testIndex := "demo_test_index" // // Elasticsearch 8.x 需要 mappings 包装 // mapping := `{ // "mappings": { // "properties": { // "title": {"type": "text"}, // "author": {"type": "keyword"}, // "price": {"type": "double"}, // "content": {"type": "text"} // } // } // }` // // // 2. 创建索引 // fmt.Printf("\n2. 创建索引 '%s'\n", testIndex) // result, _ = dll.CreateIndex(testIndex, mapping) // printJSONResult(result) // // // 3. 添加文档 // fmt.Println("\n3. 添加文档") // docs := []struct { // id string // json string // }{ // {"book1", `{"title": "Go语言编程", "author": "张三", "price": 89.9, "content": "Go语言入门教程"}`}, // {"book2", `{"title": "Python实战", "author": "李四", "price": 79.9, "content": "Python项目实战"}`}, // {"book3", `{"title": "Go语言高级编程", "author": "王五", "price": 99.9, "content": "Go进阶内容"}`}, // } // // for _, doc := range docs { // result, _ = dll.CreateDocument(testIndex, doc.id, doc.json) // fmt.Printf("添加文档 %s: ", doc.id) // printJSONResult(result) // } // // // 4. 获取文档数量 // fmt.Printf("\n4. 获取索引 '%s' 文档数量\n", testIndex) // result, _ = dll.GetDocumentCount(testIndex) // printJSONResult(result) // // // 5. 获取单个文档 // fmt.Println("\n5. 获取文档 book1") // result, _ = dll.GetDocument(testIndex, "book1") // printJSONResult(result) // // // 6. 搜索文档 // fmt.Println("\n6.搜索包含'Go'的文档") // query := `{"query": {"match": {"title": "Go"}}}` // result, _ = dll.SearchDocuments(testIndex, query) // printJSONResult(result) // // // 7. 更新文档 // fmt.Println("\n7. 更新文档 book1 的价格") // updateData := `{"doc": {"price": 85.0}}` // result, _ = dll.UpdateDocument(testIndex, "book1", updateData) // printJSONResult(result) // // // 8. 获取索引详情 // fmt.Printf("\n8. 获取索引 '%s' 详情\n", testIndex) // result, _ = dll.GetIndexDetail(testIndex) // printJSONResult(result) // // // 9. 删除文档 // fmt.Println("\n9. 删除文档 book3") // result, _ = dll.DeleteDocument(testIndex, "book3") // printJSONResult(result) // // // 10. 再次查询文档数量 // fmt.Printf("\n10. 再次获取文档数量\n") // result, _ = dll.GetDocumentCount(testIndex) // printJSONResult(result) // // // 11. 清理:删除测试索引 // fmt.Printf("\n11. 清理:删除测试索引 '%s'\n", testIndex) // result, _ = dll.DeleteIndex(testIndex) // printJSONResult(result) // // fmt.Println("\n=== 演示测试完成 ===") //}