Skip to content

Indexes

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

This example assumes using Milvus; is in scope and apiKey contains your Milvus API key.

 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);