All Products
Search
Document Center

Vector Retrieval Service:Quick start

Last Updated:Apr 11, 2024

This topic helps you quickly get started with DashVector.

Prerequisites

Note
  1. You must replace YOUR_API_KEY with your API key, and YOUR_CLUSTER_ENDPOINT with the endpoint of your cluster in the following examples.

  2. You can view the endpoint of your cluster on the Cluster Detail page in the DashVector console.

Step 1. Create a client

You can skip this step if you use an HTTP API.

import dashvector

client = dashvector.Client(
    api_key='YOUR_API_KEY',
    endpoint='YOUR_CLUSTER_ENDPOINT'
)
assert client
import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.common.DashVectorException;


DashVectorClient client = new DashVectorClient("YOUR_API_KEY", "YOUR_CLUSTER_ENDPOINT");

Step 2. Create a collection

Create a collection named quickstart with four vector dimensions.

client.create(name='quickstart', dimension=4)

collection = client.get('quickstart')
assert collection
import com.aliyun.dashvector.models.responses.Response;
import com.aliyun.dashvector.DashVectorCollection;


Response<Void> response = client.create("quickstart", 4);
System.out.println(response);
DashVectorCollection collection = client.get("quickstart");

assert collection.isSuccess();
curl -XPOST \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "quickstart", 
    "dimension": 4
  }' https://YOUR_CLUSTER_ENDPOINT/v1/collections
Note
  1. If you do not specify the distance measurement method, the default value Cosine is used.

  2. If you do not specify the vector data type, the default value Float is used.

Step 3. Insert Docs

from dashvector import Doc

# Use the dashvector.Doc object to insert a single Doc.
collection.insert(Doc(id='1', vector=[0.1, 0.2, 0.3, 0.4]))

# Use the dashvector.Doc object to insert two Docs at a time.
collection.insert(
    [
        Doc(id='2', vector=[0.2, 0.3, 0.4, 0.5], fields={'age': 20, 'name': 'zhangsan'}),
        Doc(id='3', vector=[0.3, 0.4, 0.5, 0.6], fields={'anykey': 'anyvalue'})    
    ]
)
import com.aliyun.dashvector.models.Vector;
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.requests.InsertDocRequest;
import com.aliyun.dashvector.models.responses.Response;

import java.util.Arrays;
import java.util.HashMap;


Doc doc1 = Doc.builder()
    .id("1")
    .vector(
        Vector.builder()
            .value(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f))
            .build()
    ).build();

Doc doc2 = Doc.builder()
    .id("2")
    .vector(
        Vector.builder()
            .value(Arrays.asList(0.2f, 0.3f, 0.4f, 0.5f))
            .build()
    ).fields(new HashMap<String, Object>(){{
        put("age", 20);
        put("name", "zhangsan");
    }}).build();

Doc doc3 = Doc.builder()
    .id("3")
    .field("anykey", "anyvalue")
    .vector(
        Vector.builder()
            .value(Arrays.asList(0.3f, 0.4f, 0.5f, 0.6f))
            .build()
    ).build();

InsertDocRequest request = InsertDocRequest.builder()
    .docs(Arrays.asList(doc1, doc2, doc3))
    .build();

Response<Void> response = collection.insert(request);
# Insert three Docs.

curl -XPOST \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "docs": [
      {"id": "1", "vector": [0.1, 0.2, 0.3, 0.4]},
      {"id": "2", "vector": [0.2, 0.3, 0.4, 0.5], "fields": {"age": 20, "name": "zhangsan"}},
      {"id": "3", "vector": [0.3, 0.4, 0.5, 0.6], "fields": {"anykey": "anyvalue"}}
    ]
  }' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs

Step 4. Perform a similarity search

rets = collection.query([0.1, 0.2, 0.3, 0.4], topk=2)

print(rets)
import com.aliyun.dashvector.models.Vector;
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.requests.QueryDocRequest;
import com.aliyun.dashvector.models.responses.Response;

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


Vector vector = Vector.builder().value(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f)).build();
      	
QueryDocRequest request = QueryDocRequest.builder()
    .vector(vector)
    .topk(2)
    .build();

Response<List<Doc>> response = collection.query(request);
curl -XPOST \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "vector": [0.1, 0.2, 0.3, 0.4],
    "topk": 2
  }' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/query

Step 5. Delete a Doc

# Delete a Doc.
collection.delete(ids=['1'])
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.requests.DeleteDocRequest;
import com.aliyun.dashvector.models.responses.Response;


DeleteDocRequest request = DeleteDocRequest.builder()
    .id("1")
    .build();
      
Response<List<Doc>> response = collection.delete(request);
curl -XDELETE \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"ids": ["1"]}' \
  https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs

Step 6. View statistics of a collection

stats = collection.stats()

print(stats)
import com.aliyun.dashvector.models.CollectionStats;
import com.aliyun.dashvector.models.responses.Response;


Response<CollectionStats> response = collection.stats();
curl -H 'dashvector-auth-token: YOUR_API_KEY' \
  https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/stats

Step7. Delete a collection

client.delete('quickstart')
import com.aliyun.dashvector.models.responses.Response;


Response<Void> response = client.delete("quickstart");
curl -XDELETE -H 'dashvector-auth-token: YOUR_API_KEY' \
  https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart