76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MilvusDemo
|
|
{
|
|
class Program
|
|
{
|
|
static async Task Main(string[] args)
|
|
{
|
|
Console.WriteLine("Milvus 演示程序");
|
|
Console.WriteLine("1. 运行实际的 Milvus 操作");
|
|
Console.WriteLine("2. 运行模拟测试");
|
|
Console.Write("请选择 (1/2): ");
|
|
|
|
string choice = Console.ReadLine();
|
|
|
|
if (choice == "2")
|
|
{
|
|
await RunTests();
|
|
}
|
|
else
|
|
{
|
|
await RunActualMilvusOperations();
|
|
}
|
|
}
|
|
|
|
static async Task RunActualMilvusOperations()
|
|
{
|
|
// 初始化MilvusService
|
|
const string password = "123456789";
|
|
const string host = "localhost";
|
|
const int port = 19530;
|
|
const string collectionName = "csharp_demo_collection";
|
|
const int dim = 8; // 向量维度
|
|
|
|
// 创建MilvusService实例
|
|
var milvusService = new MilvusService(host, port, collectionName, dim);
|
|
|
|
try
|
|
{
|
|
// 初始化集合(如果存在则删除并重新创建)
|
|
await milvusService.InitializeCollectionAsync();
|
|
|
|
// 创建索引并加载集合
|
|
await milvusService.CreateIndexAndLoadAsync();
|
|
|
|
// 插入随机数据
|
|
await milvusService.InsertRandomDataAsync(1000);
|
|
|
|
// 执行向量搜索
|
|
var searchResults = await milvusService.SearchVectorAsync(5);
|
|
|
|
// 显示搜索结果
|
|
Console.WriteLine("\nSearch results:");
|
|
foreach (var result in searchResults.Ids.LongIds)
|
|
{
|
|
Console.WriteLine($"- ID: {result}");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error: {ex.Message}");
|
|
if (ex.InnerException != null)
|
|
{
|
|
Console.WriteLine($"Inner error: {ex.InnerException.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
static async Task RunTests()
|
|
{
|
|
await TestRunner.RunTests();
|
|
}
|
|
}
|
|
}
|