Add vectors

更新时间:
复制 MD 格式

Add vectors to a DashVector collection to enable similarity search. You can add vectors through the DashVector console, Python SDK, Java SDK, or the HTTP API.

Prerequisites

Before you begin, make sure that you have:

Quick start (Python)

import dashvector
import os

client = dashvector.Client(
    api_key=os.environ.get('DASHVECTOR_API_KEY'),
    endpoint=os.environ.get('DASHVECTOR_ENDPOINT')
)

collection = client.get(name='example_collection')

# Add a single vector
collection.insert(
    (
        'vec-001',                        # Primary key ID
        [0.1, 0.2, 0.3, 0.4]             # Vector data (must match collection dimensions)
    )
)

# Add a vector with metadata
collection.insert(
    (
        'vec-002',
        [0.5, 0.6, 0.7, 0.8],
        {'price': 100, 'type': 'dress'}   # Optional metadata (fields)
    )
)

For the full Python SDK reference, see Insert documents (Python). For the Java SDK reference, see Insert documents (Java).

Add a vector in the console

  1. Log on to the DashVector console.

  2. In the left-side navigation pane, click Clusters. Find the target collection and click Details in the Operation column.

    Clusters page with Details button

  3. In the left-side bar, click Vector Add.

    Vector Add in the left-side bar

  4. Configure the following parameters and click Confirm.

    Parameter configuration dialog

Parameters

Parameter API parameter Required Description
Vector vector Yes The vector data. The number of dimensions and data type must match the collection. Example: [1.00,2.00,3.00,4.00]
Primary Key ID id Yes A unique identifier for the vector. Supports letters, digits, and the following special characters: _ - ! @ # $ % + = . Maximum length: 64 characters.
Partition partition Yes The target partition. Default value: default. To use a non-default partition, create one first. See Create a partition.
Attribute fields No Metadata in JSON format. Example: {"price":100,"type":"dress"}

Add vectors with SDKs

DashVector provides Python and Java SDKs for adding vectors programmatically.

Python

import dashvector
import os

client = dashvector.Client(
    api_key=os.environ.get('DASHVECTOR_API_KEY'),
    endpoint=os.environ.get('DASHVECTOR_ENDPOINT')
)

collection = client.get(name='example_collection')

# Add a single vector
ret = collection.insert(
    (
        'id-001',
        [0.1, 0.2, 0.3, 0.4]
    )
)
print(ret)
# Output example:
# {"request_id": "...", "code": 0, "message": "success"}

# Add a vector with metadata and a target partition
ret = collection.insert(
    (
        'id-002',
        [0.5, 0.6, 0.7, 0.8],
        {'price': 100, 'type': 'dress'}
    ),
    partition='my_partition'
)

# Batch add vectors
ret = collection.insert(
    [
        ('id-003', [0.1, 0.2, 0.3, 0.4], {'category': 'shoes'}),
        ('id-004', [0.9, 0.8, 0.7, 0.6], {'category': 'bags'}),
        ('id-005', [0.3, 0.4, 0.5, 0.6], {'category': 'hats'}),
    ]
)

For the full Python SDK reference, see Insert documents.

Java

import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.DashVectorCollection;
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.DashVectorResult;

import java.util.*;

public class InsertExample {
    public static void main(String[] args) {
        DashVectorClient client = new DashVectorClient(
            System.getenv("DASHVECTOR_ENDPOINT"),
            System.getenv("DASHVECTOR_API_KEY")
        );

        DashVectorCollection collection = client.get("example_collection");

        // Add a single vector
        Doc doc = Doc.builder()
            .id("id-001")
            .vector(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f))
            .fields(Map.of("price", 100, "type", "dress"))
            .build();

        DashVectorResult result = collection.insert(doc);
    }
}

For the full Java SDK reference, see Insert documents.

Add a vector with the HTTP API

For the full HTTP API reference, see Insert documents.

Limits

Item Limit
Primary key ID length 64 characters maximum
Primary key ID characters Letters, digits, and _ - ! @ # $ % + = .
Vector dimensions Must match the collection configuration
Vector data type Must match the collection configuration
Metadata format JSON