Create a collection

更新时间:
复制 MD 格式

This topic describes how to create a collection using the Java SDK.

Prerequisites

API definition

// class DashVectorClient

// Create a collection by specifying a name and vector dimensions.
public Response<Void> create(String name, int dimension);

// Create a collection using a CreateCollectionRequest object.
public Response<Void> create(CreateCollectionRequest request);

Examples

Note

To run the code, replace YOUR_API_KEY with your API key and YOUR_CLUSTER_ENDPOINT with your cluster endpoint.

Create a single-vector collection

import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.common.DashVectorException;
import com.aliyun.dashvector.models.requests.CreateCollectionRequest;
import com.aliyun.dashvector.models.responses.Response;
import com.aliyun.dashvector.proto.CollectionInfo;
import com.aliyun.dashvector.proto.FieldType;

public class Main {
    public static void main(String[] args) throws DashVectorException {
        DashVectorClient client = new DashVectorClient("YOUR_API_KEY", "YOUR_CLUSTER_ENDPOINT");

        // Create a collection using a CreateCollectionRequest object.
      
        // Create a collection named quickstart with 4 vector dimensions,
        // a vector data type of float (the default),
        // and a distance metric of dotproduct.
        // Predefine multiple fields: name (STRING), weight (FLOAT), age (INT), id (LONG), tags (ARRAY_STRING), numbers (ARRAY_INT), grades (ARRAY_FLOAT), and bankCards (ARRAY_LONG).
        CreateCollectionRequest request = CreateCollectionRequest.builder()
            .name("quickstart")
            .dimension(4)
            .metric(CollectionInfo.Metric.dotproduct)
            .dataType(CollectionInfo.DataType.FLOAT)
            .filedSchema("name", FieldType.STRING)
            .filedSchema("weight", FieldType.FLOAT)
            .filedSchema("age", FieldType.INT)
            .filedSchema("id", FieldType.LONG)
            .filedSchema("tags", FieldType.ARRAY_STRING)
            .filedSchema("numbers", FieldType.ARRAY_INT)
            .filedSchema("grades", FieldType.ARRAY_FLOAT)
            .filedSchema("bankCards", FieldType.ARRAY_LONG)
            .build();
      
        Response<Void> response = client.create(request);

        System.out.println(response);
        // example output:
        // {"code":0,"message":"","requestId":"883716a3-32f8-4220-ae54-245fa9b87bf0"}
    }
}

Create a multi-vector collection

public void createCollection() {
    CreateCollectionRequest createCollectionRequest = CreateCollectionRequest.builder()
      .name("multi_vector_demo")
      .vectors("title", VectorParam.builder().dimension(4).quantizeType("DT_VECTOR_INT8").build())
      .vectors("content", VectorParam.builder().dimension(6).metric(CollectionInfo.Metric.euclidean).build())
      .sparseVectors("abstruct", VectorParam.builder().metric(CollectionInfo.Metric.dotproduct).build())
      .sparseVectors("keywords", VectorParam.builder().metric(CollectionInfo.Metric.dotproduct).build())
      // Sparse vector indexes currently support only the dotproduct metric. You do not need to set the dimension or dtype because the default values are used.
      .filedSchema("author", FieldType.STRING)
      .filedSchema("tags", FieldType.ARRAY_STRING)
      .build();

    Response<Void> createResponse = client.create(createCollectionRequest);
    System.out.println(createResponse);
    assert createResponse.isSuccess();
}

Request parameters

Use CreateCollectionRequestBuilder to construct a CreateCollectionRequest object. The following table describes the available methods.

Method

Required

Default value

Description

name(String name)

Yes

-

The name of the collection to create.

dimension(int dimension)

Yes

-

The vector dimensions.

dataType(CollectionInfo.DataType dataType)

No

DataType.FLOAT

Supported vector data types

  • DataType.INT

  • DataType.FLOAT

fieldsSchema(Map<String, FieldType> fieldsSchem)

No

-

The field definitions. The following field types are supported:

  • FieldType.BOOL

  • FieldType.STRING

  • FieldType.INT

  • FieldType.FLOAT

  • FieldType.LONG

  • FieldType.ARRAY_STRING

  • FieldType.ARRAY_INT

  • FieldType.ARRAY_FLOAT

  • FieldType.ARRAY_LONG

fieldSchema(String key, FieldType value)

No

-

metric(CollectionInfo.Metric metric)

No

Metric.cosine

Supported distance measures

  • Metric.cosine

  • Metric.euclidean

  • Metric.dotproduct

If you set the metric to cosine, the dataType must be FLOAT.

extraParams(Map<String, String> params)

No

-

Optional parameters:

timeout(Integer timeout)

No

-

  • timeout == null: The API call is synchronous. A response is returned after the collection is created.

  • timeout == -1: The API call is asynchronous.

  • timeout >= 0: The API call is synchronous and waits for a response. If the collection is not created within the specified period, a timeout error is returned.

vectorParam(VectorParam)

No

-

Sets advanced parameters for the vector field, such as enabling quantization. For more information, see VectorParam.

vectors(Map<String, VectorParam>)

No

-

Defines a multi-vector collection. For more information, see Multi-vector search.

sparseVectors(Map<String, VectorParam>)

No

-

Defines a sparse multi-vector collection. For more information, see Multi-vector search.

build()

-

-

Constructs a CreateCollectionRequest object.

Note
  • For more information about the benefits of predefining fields when you create a collection, see Schema-free.

  • For more information about quantization policies, see Dynamic vector quantization.

Response parameters

Note

The operation returns a Response<Void> object. The Response<Void> object contains information about the result of the operation, as described in the following table.

Method

Type

Description

Example

getCode()

int

The return value. For more information, see Status codes.

0

getMessage()

String

The returned message.

success

getRequestId()

String

The unique ID of the request.

19215409-ea66-4db9-8764-26ce2eb5bb99

isSuccess()

Boolean

Indicates whether the request was successful.

true