オンデマンド DQL 操作
オンデマンドコンピューティング向けのコレクションでの DQL 操作(search、query、get、hybrid search など)では、オンデマンドクラスターのコンピューティングリソースをアタッチする必要があります。Zilliz Cloud では、セッションを作成してオンデマンドコンピューティングのニーズに対応できます。
この記事では、プロジェクトエンドポイントを使用してデータベースにコレクションを作成済みであることを前提としています。詳細については、外部コレクションの作成 を参照してください。
プロジェクトエンドポイントに接続する
プロジェクトエンドポイントは、オンデマンドコンピューティングリソースへのアクセスを提供するために設計されています。これを使用すると、オンデマンドクラスターとデータベースを管理したり、コレクションに保存されているデータを操作したりできます。
以下のコード例では、デフォルトデータベースに my_collection という名前の外部コレクションがあることを前提としています。また、接続を確立するには、十分な権限を持つ有効な API キーを常に使用してください。
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
client = MilvusClient(
uri="https://{project-id}.{region}.vectordb.zillizcloud.com",
token="YOUR_API_KEY"
)
client.has_collection(
collection_name="my_collection"
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.collection.request.HasCollectionReq;
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("https://{project-id}.{region}.vectordb.zillizcloud.com")
.token("YOUR_API_KEY")
.build());
client.hasCollection(HasCollectionReq.builder().collectionName("my_collection").build());
// Note: the cluster session is not yet supported in milvus-sdk-go.
use milvus::v2::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
let client = ClientV2::new(
&ConnectConfig::new()
.uri("https://{project-id}.{region}.vectordb.zillizcloud.com")
.token("YOUR_API_KEY"),
)
.await?;
let has = client
.has_collection(HasCollectionRequest::builder().collection_name("my_collection").build()?)
.await?;
Ok(())
}
#include "milvus/MilvusClientV2.h"
auto client = milvus::MilvusClientV2::Create();
auto status = client->Connect(milvus::ConnectParam("https://{project-id}.{region}.vectordb.zillizcloud.com", "YOUR_API_KEY"));
bool has = client->HasCollection("my_collection");
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
const client = new MilvusClient({
address: "https://{project-id}.{region}.vectordb.zillizcloud.com",
token: "YOUR_API_KEY"
});
client.hasCollection({
collection_name: "my_collection"
});
export PROJECT_ENDPOINT='https://{project-id}.{region}.vectordb.zillizcloud.com'
export TOKEN="YOUR_API_KEY"
curl --request POST \
--url "${PROJECT_ENDPOINT}/v2/vectordb/collections/has" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"collectionName": "my_collection"
}'
セッションを作成する
プロジェクトエンドポイントへの接続を確立したら、セッションを作成して、指定したオンデマンドクラスターのコンピューティングリソースをアタッチします。
以下の例では、ID が inxx-xxxxxxxxxxxxxxxxx のオンデマンドクラスターをすでに作成済みであることを前提としています。
RESTful リクエストの場合は、セッションを作成する代わりに、クラスター ID をクエリパラメーターとして DQL 呼び出しに渡す必要があります。
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
session = client.session(
cluster_id="inxx-xxxxxxxxxxxxxxxxx"
)
import io.milvus.v2.client.MilvusClientV2Session;
MilvusClientV2Session session = client.session("inxx-xxxxxxxxxxxxxxxxx");
// Note: the cluster session is not yet supported in milvus-sdk-go.
let session = client.session("inxx-xxxxxxxxxxxxxxxxx")?;
milvus::MilvusClientV2SessionPtr session;
status = client->Session("inxx-xxxxxxxxxxxxxxxxx", session);
const session = client.session("inxx-xxxxxxxxxxxxxxxxx");
export CLUSTER_ID="inxx-xxxxxxxxxxxxxxxxx"
DQL 操作を実行する
セッションの準備ができたら、検索を実行できます。以下の例では、基本的なベクトル検索を例として使用します。これは、query、get、hybrid search にも当てはまります。
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
query_vector = [0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592]
res = session.search(
db_name="my_database",
collection_name="my_collection",
anns_field="vector",
data=[query_vector],
limit=3,
output_fields=["product_id", "title", "main_category", "price", "average_rating", "rating_number"]
)
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.request.data.FloatVec;
import io.milvus.v2.service.vector.response.SearchResp;
import java.util.*;
FloatVec queryVector = new FloatVec(new float[]{0.3580376395471989f, -0.6023495712049978f, 0.18414012509913835f, -0.26286205330961354f, 0.9029438446296592f});
SearchReq searchReq = SearchReq.builder()
.databaseName("my_database")
.collectionName("my_collection")
.data(Collections.singletonList(queryVector))
.annsField("vector")
.topK(3)
.outputFields(Arrays.asList("product_id", "title", "main_category", "price", "average_rating", "rating_number"))
.build();
SearchResp searchResp = session.search(searchReq);
// Note: the cluster session is not yet supported in milvus-sdk-go.
use milvus::v2::prelude::*;
let query_vector = vec![0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592];
let res = session
.search(
SearchRequest::builder()
.database_name("my_database")
.collection_name("my_collection")
.vector_field("vector")
.vectors(SearchVectors::Float(vec![query_vector]))
.limit(3)
.output_fields(["product_id", "title", "main_category", "price", "average_rating", "rating_number"])
.build()?,
)
.await?;
std::vector<float> queryVector = {
0.35803764F, -0.60234958F, 0.18414013F, -0.26286206F, 0.90294385F
};
auto request = milvus::SearchRequest()
.WithDatabaseName("my_database")
.WithCollectionName("my_collection")
.WithAnnsField("vector")
.WithLimit(3)
.AddOutputField("product_id")
.AddOutputField("title")
.AddFloatVector(queryVector);
milvus::SearchResponse response;
status = session->Search(request, response);
const query_vector = [0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592];
const res = session.search({
db_name: "my_database",
collection_name: "my_collection",
anns_field: "vector",
data: [query_vector],
limit: 3,
output_fields: ["product_id", "title", "main_category", "price", "average_rating", "rating_number"],
});
curl --request POST \
--url "${PROJECT_ENDPOINT}/v2/vectordb/entities/search?cluster_id=inxx-xxxxxxxxxxxxxxxxx" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"dbName": "my_database",
"collectionName": "my_collection",
"data": [
[
0.3580376395471989,
-0.6023495712049978,
0.18414012509913835,
-0.26286205330961354,
0.9029438446296592
]
],
"annsField": "vector",
"limit": 3,
"outputFields": [
"product_id",
"title",
"main_category",
"price",
"average_rating",
"rating_number"
]
}'
セッションを閉じる
オンデマンドコンピューティングのタスクが完了したら、セッションを閉じることができます。閉じたセッションは、以降の DQL 操作には使用できません。
RESTful 呼び出しにはこれは必要ありません。
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
session.close()
session.close();
// Note: the cluster session is not yet supported in milvus-sdk-go.
session.close();
session->Close();
session.close();
# Note: the RESTful API does not maintain a persistent session; no explicit close is required.