|
| 1 | +using Milvus.Client; |
| 2 | + |
| 3 | +var builder = WebApplication.CreateBuilder(args); |
| 4 | + |
| 5 | +// Add service defaults & Aspire components. |
| 6 | +builder.AddServiceDefaults(); |
| 7 | + |
| 8 | +builder.AddMilvusClient("milvus"); |
| 9 | + |
| 10 | +// Add services to the container. |
| 11 | +builder.Services.AddProblemDetails(); |
| 12 | + |
| 13 | +var app = builder.Build(); |
| 14 | + |
| 15 | +// Configure the HTTP request pipeline. |
| 16 | +app.UseExceptionHandler(); |
| 17 | + |
| 18 | +app.MapDefaultEndpoints(); |
| 19 | + |
| 20 | +app.MapGet("/create", async (MilvusClient milvusClient, ILogger<Program> logger) => |
| 21 | +{ |
| 22 | + string collectionName = "book"; |
| 23 | + MilvusCollection collection = milvusClient.GetCollection(collectionName); |
| 24 | + |
| 25 | + //Check if this collection exists |
| 26 | + var hasCollection = await milvusClient.HasCollectionAsync(collectionName); |
| 27 | + |
| 28 | + if (hasCollection) |
| 29 | + { |
| 30 | + await collection.DropAsync(); |
| 31 | + Console.WriteLine("Drop collection {0}", collectionName); |
| 32 | + } |
| 33 | + |
| 34 | + collection = await milvusClient.CreateCollectionAsync( |
| 35 | + collectionName, |
| 36 | + new[] { |
| 37 | + FieldSchema.Create<long>("book_id", isPrimaryKey:true), |
| 38 | + FieldSchema.Create<long>("word_count"), |
| 39 | + FieldSchema.CreateVarchar("book_name", 256), |
| 40 | + FieldSchema.CreateFloatVector("book_intro", 2) |
| 41 | + } |
| 42 | + ); |
| 43 | + logger.LogInformation("Collection created: book"); |
| 44 | + |
| 45 | + Random ran = new(); |
| 46 | + List<long> bookIds = new(); |
| 47 | + List<long> wordCounts = new(); |
| 48 | + List<ReadOnlyMemory<float>> bookIntros = new(); |
| 49 | + List<string> bookNames = new(); |
| 50 | + for (long i = 0L; i < 2000; ++i) |
| 51 | + { |
| 52 | + bookIds.Add(i); |
| 53 | + wordCounts.Add(i + 10000); |
| 54 | + bookNames.Add($"Book Name {i}"); |
| 55 | + |
| 56 | + float[] vector = new float[2]; |
| 57 | + for (int k = 0; k < 2; ++k) |
| 58 | + { |
| 59 | + vector[k] = ran.Next(); |
| 60 | + } |
| 61 | + bookIntros.Add(vector); |
| 62 | + } |
| 63 | + |
| 64 | + MutationResult result = await collection.InsertAsync( |
| 65 | + new FieldData[] |
| 66 | + { |
| 67 | + FieldData.Create("book_id", bookIds), |
| 68 | + FieldData.Create("word_count", wordCounts), |
| 69 | + FieldData.Create("book_name", bookNames), |
| 70 | + FieldData.CreateFloatVector("book_intro", bookIntros), |
| 71 | + }); |
| 72 | + |
| 73 | + logger.LogInformation("Added vectors"); |
| 74 | + |
| 75 | + // Check result |
| 76 | + logger.LogInformation("Insert status: {0},", result.ToString()); |
| 77 | + |
| 78 | + // Create index |
| 79 | + await collection.CreateIndexAsync( |
| 80 | + "book_intro", |
| 81 | + //MilvusIndexType.IVF_FLAT,//Use MilvusIndexType.IVF_FLAT. |
| 82 | + IndexType.AutoIndex,//Use MilvusIndexType.AUTOINDEX when you are using zilliz cloud. |
| 83 | + SimilarityMetricType.L2); |
| 84 | + |
| 85 | + // Check index status |
| 86 | + IList<MilvusIndexInfo> indexInfos = await collection.DescribeIndexAsync("book_intro"); |
| 87 | + |
| 88 | + foreach (var info in indexInfos) |
| 89 | + { |
| 90 | + logger.LogInformation("FieldName:{0}, IndexName:{1}, IndexId:{2}", info.FieldName, info.IndexName, info.IndexId); |
| 91 | + } |
| 92 | + |
| 93 | + logger.LogInformation("Index created"); |
| 94 | + |
| 95 | + // Then load it |
| 96 | + await collection.LoadAsync(); |
| 97 | + |
| 98 | + logger.LogInformation("Collection loaded"); |
| 99 | + |
| 100 | + return Results.Ok("Collection created"); |
| 101 | +}); |
| 102 | + |
| 103 | +app.MapGet("/search", async (MilvusClient milvusClient, ILogger<Program> logger) => |
| 104 | +{ |
| 105 | + MilvusCollection collection = milvusClient.GetCollection("book"); |
| 106 | + |
| 107 | + // Query |
| 108 | + string expr = "book_id in [2,4,6,8]"; |
| 109 | + |
| 110 | + QueryParameters queryParameters = new(); |
| 111 | + queryParameters.OutputFields.Add("book_id"); |
| 112 | + queryParameters.OutputFields.Add("book_name"); |
| 113 | + queryParameters.OutputFields.Add("word_count"); |
| 114 | + |
| 115 | + IReadOnlyList<FieldData> queryResult = await collection.QueryAsync( |
| 116 | + expr, |
| 117 | + queryParameters); |
| 118 | + |
| 119 | + return queryResult[2]; |
| 120 | +}); |
| 121 | + |
| 122 | +app.Run(); |
0 commit comments