Learn how to create a collection, insert vectors, and perform similarity searches with DashVector.
Prerequisites
-
A cluster is created. Create a cluster
-
An API key is obtained. API key management
-
The latest DashVector SDK is installed. For more information, see Install the DashVector SDK.
Note
-
Replace YOUR_API_KEY and YOUR_CLUSTER_ENDPOINT with your actual values.
-
Find your cluster endpoint on the Cluster Detail page in the DashVector console.
Step 1. Create a client
You can skip this step if you use the HTTP API.
import dashvector
client = dashvector.Client(
api_key='YOUR_API_KEY',
endpoint='YOUR_CLUSTER_ENDPOINT'
)
assert clientimport 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 collectionimport 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/collectionsNote
-
The default distance metric is Cosine.
-
The default vector data type is
Float.
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/docsStep 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/queryStep 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/docsStep 6. View collection statistics
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/statsStep 7. 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该文章对您有帮助吗?