Skip to content

Milvus

Nuget package dotnet License: MIT Discord

Features 🔥

  • Fully generated C# SDK based on official Milvus OpenAPI specification using AutoSDK
  • Same day update to support new features
  • Updated and supported automatically if there are no breaking changes
  • All modern .NET features - nullability, trimming, NativeAOT, etc.
  • Supports .NET 10.0

Usage

1
2
3
using Milvus;

using var client = new MilvusClient(apiKey);

Generate

Basic example showing how to create a client and make a request.

1
2
3
4
5
6
var client = Client;

// List collections to verify the client is connected.

await client.CollectionOperationsV2.CreateVectordbCollectionsListAsync(
    dbName: "default");

Collections

Manage Milvus collections -- create, list, describe, and drop.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
var client = Client;
var collectionName = $"test_collection_{Guid.NewGuid():N}";

// Create a new collection with 4-dimensional vectors using Cosine distance.

var createResponse = await client.CollectionOperationsV2.CreateVectordbCollectionsCreateAsync(
    requestTimeout: 30,
    collectionName: collectionName,
    dimension: 4,
    metricType: "COSINE");

Console.WriteLine($"Collection '{collectionName}' created successfully.");

// Check that the collection exists using the Has endpoint.

var hasResponse = await client.CollectionOperationsV2.CreateVectordbCollectionsHasAsync(
    dbName: "default",
    collectionName: collectionName);

Console.WriteLine($"Collection '{collectionName}' exists: {hasResponse.Data.Has}");

// Describe the collection to get detailed information.

var describeResponse = await client.CollectionOperationsV2.CreateVectordbCollectionsDescribeAsync(
    dbName: "default",
    collectionName: collectionName);

Console.WriteLine($"Collection name: {describeResponse.Data.CollectionName}");
Console.WriteLine($"Fields count: {describeResponse.Data.Fields.Count}");
Console.WriteLine($"Load status: {describeResponse.Data.Load}");

// Drop the collection.

var dropResponse = await client.CollectionOperationsV2.CreateVectordbCollectionsDropAsync(
    collectionName1: collectionName);

Console.WriteLine($"Collection '{collectionName}' dropped.");

// Verify the collection no longer exists.

var hasAfterDrop = await client.CollectionOperationsV2.CreateVectordbCollectionsHasAsync(
    dbName: "default",
    collectionName: collectionName);

Vectors

Insert, search, query, and delete vectors in a Milvus collection.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var client = Client;
var collectionName = $"test_vectors_{Guid.NewGuid():N}";

// Create a collection with 4-dimensional vectors using Cosine distance.

await client.CollectionOperationsV2.CreateVectordbCollectionsCreateAsync(
    requestTimeout: 30,
    collectionName: collectionName,
    dimension: 4,
    metricType: "COSINE",
    autoId: false,
    primaryFieldName: "id",
    vectorFieldName: "vector");

// Insert vectors with associated data into the collection.
// Note: Row-based insert with mixed types (int, float[]) requires raw JSON
// because the source-generated serializer cannot handle polymorphic object arrays.

using var insertHttpResponse = await client.HttpClient.PostAsJsonAsync(
    "/v2/vectordb/entities/insert",
    new
    {
        collectionName,
        data = new[]
        {
            new { id = 1, vector = new[] { 0.05f, 0.61f, 0.76f, 0.74f } },
            new { id = 2, vector = new[] { 0.19f, 0.81f, 0.75f, 0.11f } },
            new { id = 3, vector = new[] { 0.36f, 0.55f, 0.47f, 0.94f } },
            new { id = 4, vector = new[] { 0.18f, 0.01f, 0.85f, 0.80f } },
            new { id = 5, vector = new[] { 0.24f, 0.18f, 0.22f, 0.44f } },
        },
    });
insertHttpResponse.EnsureSuccessStatusCode();
var insertJson = JsonDocument.Parse(await insertHttpResponse.Content.ReadAsStringAsync());
var insertCode = insertJson.RootElement.GetProperty("code").GetInt32();
var insertCount = insertJson.RootElement.GetProperty("data").GetProperty("insertCount").GetInt32();

Console.WriteLine($"Inserted {insertCount} vectors.");

// Search for the 3 nearest neighbors using a query vector.
// Note: Milvus v2.5+ uses "data" field for search vectors (v2.4 used "vector").

using var searchHttpResponse = await client.HttpClient.PostAsJsonAsync(
    "/v2/vectordb/entities/search",
    new
    {
        collectionName,
        data = new[] { new[] { 0.2f, 0.1f, 0.9f, 0.7f } },
        limit = 3,
        outputFields = new[] { "id" },
    });
searchHttpResponse.EnsureSuccessStatusCode();
var searchJson = JsonDocument.Parse(await searchHttpResponse.Content.ReadAsStringAsync());
var searchCode = searchJson.RootElement.GetProperty("code").GetInt32();
var searchData = searchJson.RootElement.GetProperty("data");

Console.WriteLine($"Search returned {searchData.GetArrayLength()} results.");

// Query entities by filter expression.

var queryResponse = await client.VectorOperationsV2.CreateVectordbEntitiesQueryAsync(
    collectionName: collectionName,
    filter: "id in [1, 2, 3]",
    outputFields: ["id"]);

Console.WriteLine($"Query returned {queryResponse.Data!.Count} entities.");

// Delete specific entities by filter.

var deleteResponse = await client.VectorOperationsV2.CreateVectordbEntitiesDeleteAsync(
    collectionName: collectionName,
    filter: "id in [1, 2]");

Console.WriteLine("Deleted entities with id in [1, 2].");

// Wait for delete to propagate (Milvus deletes are eventually consistent).

await Task.Delay(TimeSpan.FromSeconds(2));

// Verify the remaining entity count.

var queryAfterDelete = await client.VectorOperationsV2.CreateVectordbEntitiesQueryAsync(
    collectionName: collectionName,
    filter: "id >= 0",
    outputFields: ["id"]);

Console.WriteLine($"Remaining entities: {queryAfterDelete.Data!.Count}");

// Cleanup: drop the collection.

await client.CollectionOperationsV2.CreateVectordbCollectionsDropAsync(
    collectionName1: collectionName);

Indexes

Create, list, describe, and drop indexes on a Milvus collection.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var client = Client;
var collectionName = $"test_indexes_{Guid.NewGuid():N}";
var indexName = "vector_index";
var vectorFieldName = "vector";

// Create a collection with quick-setup (auto-creates an index).

await client.CollectionOperationsV2.CreateVectordbCollectionsCreateAsync(
    requestTimeout: 30,
    collectionName: collectionName,
    dimension: 4,
    metricType: "COSINE",
    autoId: false,
    primaryFieldName: "id",
    vectorFieldName: vectorFieldName);

// List all indexes for the collection.

var listResponse = await client.IndexOperationsV2.CreateVectordbIndexesListAsync(
    dbName: "default",
    collectionName: collectionName);

Console.WriteLine($"Found {listResponse.Data.Count} index(es): {string.Join(", ", listResponse.Data)}");

// Describe the auto-created index to get detailed information.

var autoIndexName = listResponse.Data[0];

var describeResponse = await client.IndexOperationsV2.CreateVectordbIndexesDescribeAsync(
    collectionName: collectionName,
    indexName: autoIndexName);

var indexDetail = describeResponse.Data[0];

Console.WriteLine($"Index name: {indexDetail.IndexName}");
Console.WriteLine($"Index type: {indexDetail.IndexType}");
Console.WriteLine($"Field name: {indexDetail.FieldName}");
Console.WriteLine($"Metric type: {indexDetail.MetricType}");
Console.WriteLine($"Index state: {indexDetail.IndexState}");

// Release the collection before dropping the index (required by Milvus).

await client.CollectionOperationsV2.CreateVectordbCollectionsReleaseAsync(
    collectionName: collectionName);

Console.WriteLine($"Collection '{collectionName}' released.");

// Drop the auto-created index.

await client.IndexOperationsV2.CreateVectordbIndexesDropAsync(
    collectionName: collectionName,
    indexName1: autoIndexName);

Console.WriteLine($"Index '{autoIndexName}' dropped.");

// Create a new index with explicit parameters.

await client.IndexOperationsV2.CreateVectordbIndexesCreateAsync(
    collectionName: collectionName,
    indexParams:
    [
        new IndexParam
        {
            MetricType = "L2",
            FieldName = vectorFieldName,
            IndexName = indexName,
            IndexConfig = new IndexConfig
            {
                IndexType = "AUTOINDEX",
            },
        },
    ]);

Console.WriteLine($"Index '{indexName}' created.");

// Verify the new index exists.

var listAfterCreate = await client.IndexOperationsV2.CreateVectordbIndexesListAsync(
    dbName: "default",
    collectionName: collectionName);

// Describe the newly created index.

var describeNew = await client.IndexOperationsV2.CreateVectordbIndexesDescribeAsync(
    collectionName: collectionName,
    indexName: indexName);

Console.WriteLine($"New index metric type: {describeNew.Data[0].MetricType}");

// Cleanup: drop the collection.

await client.CollectionOperationsV2.CreateVectordbCollectionsDropAsync(
    collectionName1: collectionName);

Support

Priority place for bugs: https://github.com/tryAGI/Milvus/issues
Priority place for ideas and general questions: https://github.com/tryAGI/Milvus/discussions
Discord: https://discord.gg/Ca2xhfBf3v

Acknowledgments

JetBrains logo

This project is supported by JetBrains through the Open Source Support Program.