Create a vector index (Java SDK V2)

Updated at:

A vector index organizes and retrieves vector data. You can call the PutVectorIndex operation by using the OSS Java SDK V2 to create a vector index in a vector bucket, or call the PutVectorIndexFusion operation to create a fusion index that supports combined vector and text search.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. The Alibaba Cloud account owner or an administrator must grant the required permissions by using a RAM policy or a bucket policy.

API

Actions

Description

PutVectorIndex

oss:PutVectorIndex

Creates a vector index.

Sample code

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

Create a fusion index

A fusion index extends a vector index with field-level capabilities such as text analysis and exact match, and supports queries that combine vector search with text search. To create a fusion index, set mode to FUSION and describe the field structure in the fields of SchemaConfiguration: VECTOR fields are used for vector search, and STRING fields can either enable text analysis (with a specific analyzer and its parameters) by using the text configuration or be declared as the partition key by using isPartitionKey.

Note

The fusion index feature requires alibabacloud-oss-v2 (Java SDK V2) 0.6.1 or later.

Sample code

The following code creates a fusion index that contains a vector field, a partition key field, a text field, and a LONG array field:

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.*;

import java.util.Arrays;
import java.util.List;

public class PutVectorIndexFusionSample {
    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()) {
            // Vector field
            FieldSchema vector = FieldSchema.newBuilder()
                    .name("vector_field")
                    .type(FieldType.VECTOR)
                    .dataType(VectorDataType.fromString("float32"))
                    .dimension(3)
                    .distanceMetric(DistanceMetricType.fromString("cosine"))
                    .build();

            // Partition key field
            FieldSchema partitionKey = FieldSchema.newBuilder()
                    .name("partition_key")
                    .type(FieldType.STRING)
                    .isPartitionKey(true)
                    .build();

            // Text field with text analysis enabled
            FieldSchema title = FieldSchema.newBuilder()
                    .name("title")
                    .type(FieldType.STRING)
                    .exactMatch(true)
                    .text(TextSchema.newBuilder()
                            .enabled(true)
                            .analyzer(AnalyzerType.STANDARD)
                            .analyzerParameters(AnalyzerParameters.newBuilder()
                                    .caseSensitive(false)
                                    .delimitWord(false)
                                    .build())
                            .build())
                    .build();

            // LONG array field
            FieldSchema timestamps = FieldSchema.newBuilder()
                    .name("timestamps")
                    .type(FieldType.LONG)
                    .isArray(true)
                    .build();

            List<FieldSchema> fields = Arrays.asList(vector, partitionKey, title, timestamps);

            PutVectorIndexFusionRequest request = PutVectorIndexFusionRequest.newBuilder()
                    .bucket("examplebucket")
                    .indexName("exampleindex")
                    .mode(IndexModeType.FUSION)
                    .schemaConfiguration(SchemaConfiguration.newBuilder().fields(fields).build())
                    .build();

            PutVectorIndexFusionResult result = client.putVectorIndexFusion(request);
            System.out.printf("status code: %d, request id: %s%n", result.statusCode(), result.requestId());
        }
    }
}

Related documents