A vector index stores and manages vector data in a vector bucket. Each index defines the dimension, distance metric, and metadata structure, which forms the foundation for vector retrieval.
Create vector index
A single vector bucket can contain a maximum of 100 vector indexes.
The request rate for the PutVectorIndex API operation is limited to 5 requests per second.
Console
On the Vector Bucket page, click your vector bucket.
On the Vector Indexes page, click Create Index Table.
Configure the index parameters:
Index Table Name: Must be 1 to 63 characters long, contain only letters and digits, start with a letter, and be unique within the vector bucket.
Vector Data Type: The system default is
float32(floating-point).Dimension: 1 to 4,096. All vectors added to this index must have the same dimension.
Distance Metric: Select a distance calculation method based on your use case.
Euclidean Distance: The straight-line distance between two points in space. This metric is suitable for measuring differences in numerical values.
Cosine Distance: Measures the difference in direction between two vectors. This metric is suitable for high-dimensional semantic similarity searches, such as with text or image embeddings.
Metadata Configuration: Configure non-filterable metadata fields to store additional information about vector data. This information is not used in search filters. The following limits apply:
Number of metadata fields: 1 to 100
Length of each metadata key: 1 to 63 bytes
Click OK to create the index.
ossutil
Create an index named index for the examplebucket bucket. The index has 512 vector dimensions, uses the float32 data type, and the euclidean distance measure.
ossutil vectors-api put-vector-index --bucket examplebucket --index-name index --data-type float32 --dimension 512 --distance-metric euclidean
SDK
Python
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="vector put vector index sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain name that other services can use to access OSS')
parser.add_argument('--index_name', help='The name of the vector index.', required=True)
parser.add_argument('--account_id', help='The account ID.', required=True)
def main():
args = parser.parse_args()
# Load credentials from environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
cfg.account_id = args.account_id
cfg.use_internal_endpoint = True # To access the service over the public network, set this parameter to False or remove this line.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
vector_client = oss_vectors.Client(cfg)
result = vector_client.put_vector_index(oss_vectors.models.PutVectorIndexRequest(
bucket=args.bucket,
index_name=args.index_name,
dimension=512,
data_type='float32',
distance_metric='euclidean',
metadata={"nonFilterableMetadataKeys": ["key1", "key2"]}
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main()Go
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/vectors"
)
var (
region string
bucketName string
accountId string
indexName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the vector bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
flag.StringVar(&accountId, "account-id", "", "The ID of the Alibaba Cloud account.")
flag.StringVar(&indexName, "index", "", "The name of the vector index.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, accountId required")
}
if len(indexName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, index required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId).
// To access the service over the public internet, set this parameter to false or remove this line.
WithUseInternalEndpoint(true)
client := vectors.NewVectorsClient(cfg)
request := &vectors.PutVectorIndexRequest{
Bucket: oss.Ptr(bucketName),
DataType: oss.Ptr("float32"),
Dimension: oss.Ptr(128),
DistanceMetric: oss.Ptr("cosine"),
IndexName: oss.Ptr(indexName),
Metadata: map[string]any{
"nonFilterableMetadataKeys": []string{"foo", "bar"},
},
}
result, err := client.PutVectorIndex(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put vector index: %v", err)
}
log.Printf("put vector index result: %#v\n", result)
}Java
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;
import com.aliyun.sdk.service.oss2.vectors.models.PutVectorIndexRequest;
import com.aliyun.sdk.service.oss2.vectors.models.PutVectorIndexResult;
public class PutVectorIndexSample {
public static void main(String[] args) throws Exception {
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
.region("cn-hangzhou")
.accountId("1234567890")
.credentialsProvider(provider)
.build()) {
PutVectorIndexRequest request = PutVectorIndexRequest.newBuilder()
.bucket("examplebucket")
.indexName("exampleindex")
.dataType("float32")
.dimension(3)
.distanceMetric("cosine")
.build();
PutVectorIndexResult result = client.putVectorIndex(request);
System.out.printf("status code: %d, request id: %s%n", result.statusCode(), result.requestId());
}
}
}API
Call the PutVectorIndex operation to create a vector index.
Get vector index
Console
On the Vector Bucket page, click the vector bucket that you created. On the Vector Indexes page, you can view the vector index information.
ossutil
Retrieve the properties of the vector index named index in the vector bucket examplebucket.
ossutil vectors-api get-vector-index --bucket examplebucket --index-name index
SDK
Python
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="vector get vector index sample")
parser.add_argument('--region', help='The region of the bucket.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain name for accessing OSS.')
parser.add_argument('--index_name', help='The name of the vector index.', required=True)
parser.add_argument('--account_id', help='The account ID.', required=True)
def main():
args = parser.parse_args()
# Load credentials from environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
cfg.account_id = args.account_id
cfg.use_internal_endpoint = True # To access OSS over the public network, set this to False or remove this line.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
vector_client = oss_vectors.Client(cfg)
result = vector_client.get_vector_index(oss_vectors.models.GetVectorIndexRequest(
bucket=args.bucket,
index_name=args.index_name,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if result.index:
print(f'index name: {result.index}')
if __name__ == "__main__":
main()Go
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/vectors"
)
var (
region string
bucketName string
indexName string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region where the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&indexName, "index", "", "The name of the vector index.")
flag.StringVar(&accountId, "account-id", "", "The ID of the Alibaba Cloud account.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(indexName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, index required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, accountId required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId).
// To access the service over a public endpoint, set this to false or remove this line.
WithUseInternalEndpoint(true)
client := vectors.NewVectorsClient(cfg)
request := &vectors.GetVectorIndexRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr(indexName),
}
result, err := client.GetVectorIndex(context.TODO(), request)
if err != nil {
log.Fatalf("failed to get vector index %v", err)
}
log.Printf("get vector index result:%#v\n", result)
}Java
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;
import com.aliyun.sdk.service.oss2.vectors.models.GetVectorIndexRequest;
import com.aliyun.sdk.service.oss2.vectors.models.GetVectorIndexResult;
public class GetVectorIndexSample {
public static void main(String[] args) throws Exception {
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
.region("cn-hangzhou")
.accountId("1234567890")
.credentialsProvider(provider)
.build()) {
GetVectorIndexRequest request = GetVectorIndexRequest.newBuilder()
.bucket("examplebucket")
.indexName("exampleindex")
.build();
GetVectorIndexResult result = client.getVectorIndex(request);
System.out.printf("status code: %d, request id: %s%n", result.statusCode(), result.requestId());
}
}
}API
Call the GetVectorIndex operation to retrieve information about a vector index.
List vector indexes
The ListVectorIndexes API operation returns a maximum of 500 indexes per request. You can use pagination to retrieve subsequent batches of the index list. The concurrency limit for this operation is 16.
Console
On the Vector Bucket page, click your vector bucket. The Vector Indexes page opens and lists all vector indexes in the current vector bucket.
ossutil
List all vector indexes in the vector bucket named examplebucket.
ossutil vectors-api list-vector-indexes --bucket examplebucket
SDK
Python
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="list vector indexes sample")
parser.add_argument('--region', help='The region where the bucket is located.', required=True)
parser.add_argument('--endpoint', help='The OSS access endpoint.')
parser.add_argument('--account_id', help='The account ID.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
def main():
args = parser.parse_args()
# Load credential values from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the SDK's default configuration.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
cfg.account_id = args.account_id
cfg.use_internal_endpoint = True # To connect over the public network, set this to False or remove this line.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss_vectors.Client(cfg)
# Create the paginator for the ListVectorIndexes operation.
paginator = client.list_vector_indexes_paginator()
# Iterate through the pages of vector indexes.
for page in paginator.iter_page(oss_vectors.models.ListVectorIndexesRequest(
bucket=args.bucket
)
):
for o in page.indexes:
print(f'Index: {o.get("indexName")}, {o.get("dataType")}, {o.get("dimension")}, {o.get("status")}')
if __name__ == "__main__":
main()Go
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/vectors"
)
var (
region string
bucketName string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the vector bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
flag.StringVar(&accountId, "account-id", "", "The ID of your Alibaba Cloud account.")
}
func main() {
flag.Parse()
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, accountId required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId).
// To access the service over the public internet, set this parameter to false or remove this line.
WithUseInternalEndpoint(true)
client := vectors.NewVectorsClient(cfg)
request := &vectors.ListVectorIndexesRequest{
Bucket: oss.Ptr(bucketName),
}
p := client.NewListVectorIndexesPaginator(request)
var i int
log.Println("Vector Indexes:")
for p.HasNext() {
i++
page, err := p.NextPage(context.TODO())
if err != nil {
log.Fatalf("failed to get page %v, %v", i, err)
}
// Log the found indexes.
for _, index := range page.Indexes {
log.Printf("index:%v, %v, %v, %v\n", oss.ToString(index.IndexName), oss.ToTime(index.CreateTime), oss.ToString(index.DataType), oss.ToString(index.Status))
}
}
}Java
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;
import com.aliyun.sdk.service.oss2.vectors.models.ListVectorIndexesRequest;
import com.aliyun.sdk.service.oss2.vectors.models.ListVectorIndexesResult;
public class ListVectorIndexesSample {
public static void main(String[] args) throws Exception {
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
.region("cn-hangzhou")
.accountId("1234567890")
.credentialsProvider(provider)
.build()) {
ListVectorIndexesRequest request = ListVectorIndexesRequest.newBuilder()
.bucket("examplebucket")
.build();
ListVectorIndexesResult result = client.listVectorIndexes(request);
System.out.printf("status code: %d, request id: %s%n", result.statusCode(), result.requestId());
}
}
}API
Call the ListVectorIndexes operation to list all vector indexes in a vector bucket.
Delete vector index
Deleting a vector index also deletes all vector data it contains. This operation is irreversible. Proceed with caution and back up any important data.
Console
On the Vector Bucket page, click the vector bucket that you created. On the Vector Indexes page, select the index to delete and confirm the deletion.
ossutil
Delete the vector index named index from the bucket named examplebucket.
ossutil vectors-api delete-vector-index --bucket examplebucket --index-name index
SDK
Python
import argparse
import alibabacloud_oss_v2 as oss
import alibabacloud_oss_v2.vectors as oss_vectors
parser = argparse.ArgumentParser(description="vector delete vector index sample")
parser.add_argument('--region', help='The region where the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The endpoint to access OSS.')
parser.add_argument('--index_name', help='The name of the vector index.', required=True)
parser.add_argument('--account_id', help='Your Alibaba Cloud account ID.', required=True)
def main():
args = parser.parse_args()
# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
cfg.account_id = args.account_id
cfg.use_internal_endpoint = True # To access OSS over the public network, set this parameter to False or remove this line.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
vector_client = oss_vectors.Client(cfg)
result = vector_client.delete_vector_index(oss_vectors.models.DeleteVectorIndexRequest(
bucket=args.bucket,
index_name=args.index_name,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
if __name__ == "__main__":
main()Go
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/vectors"
)
var (
region string
bucketName string
indexName string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the vector bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the vector bucket.")
flag.StringVar(&indexName, "index", "", "The name of the vector index.")
flag.StringVar(&accountId, "account-id", "", "The ID of the Alibaba Cloud account.")
}
func main() {
flag.Parse()
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(indexName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, index required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, accountId required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).WithAccountId(accountId).
// To access OSS over the Internet, set this to false or remove this line.
WithUseInternalEndpoint(true)
client := vectors.NewVectorsClient(cfg)
request := &vectors.DeleteVectorIndexRequest{
Bucket: oss.Ptr(bucketName),
IndexName: oss.Ptr(indexName),
}
result, err := client.DeleteVectorIndex(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete vector index %v", err)
}
log.Printf("delete vector index result:%#v\n", result)
}Java
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.vectors.OSSVectorsClient;
import com.aliyun.sdk.service.oss2.vectors.models.DeleteVectorIndexRequest;
import com.aliyun.sdk.service.oss2.vectors.models.DeleteVectorIndexResult;
public class DeleteVectorIndexSample {
public static void main(String[] args) throws Exception {
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
try (OSSVectorsClient client = OSSVectorsClient.newBuilder()
.region("cn-hangzhou")
.accountId("1234567890")
.credentialsProvider(provider)
.build()) {
DeleteVectorIndexRequest request = DeleteVectorIndexRequest.newBuilder()
.bucket("examplebucket")
.indexName("exampleindex")
.build();
DeleteVectorIndexResult result = client.deleteVectorIndex(request);
System.out.printf("status code: %d, request id: %s%n", result.statusCode(), result.requestId());
}
}
}API
Call the DeleteVectorIndex operation to delete a vector index.