メインコンテンツまでスキップ
バージョン: User Guides (Cloud)

オンデマンド検索のクイックスタート
Public Preview

Zilliz Cloud はオンデマンドのコンピュートリソースを提供し、必要なときに類似検索やクエリを実行できます。使用したリソース分のみ課金され、不要なときはクラスターを停止してコストを抑えられます。

ステップ 1: プロジェクトエンドポイントに接続する。

データベースを操作する前に、プロジェクトエンドポイントへ接続します。プロジェクトエンドポイントは、Zilliz Cloud コンソールでオンデマンドコンピュートを有効化した後、クイックスタートページで確認できます。

📘Notes
  • マネージドコレクションの操作には認証用の API キー が必要です。このフローでは username:password 認証はサポートされません。

  • オンデマンドコンピュート用データベース内のマネージドコレクションでは、ロード操作は不要です。

# connect to database
client = MilvusClient(
# a project-specific on-demand compute endpoint
uri="https://{project-id}.{region}.api.zillizcloud.com",
token="YOUR_API_KEY"
)

ステップ 2: (任意)データベースを作成する。

Zilliz Cloud にはデフォルトデータベースが用意されています。デフォルトを使う場合はこの手順をスキップできます。必要に応じて以下のように作成してください。

client.create_database(
db_name="my_database"
)

ステップ 3: マネージドコレクションを作成する。

データベースの準備ができたら、マネージドコレクションを作成できます。外部データファイルにカラムをマッピングする外部コレクションとは異なり、マネージドコレクションはデータを取り込むことで高いパフォーマンスを得られます。

以下は、コレクションスキーマを定義してコレクションを作成する例です。

from pymilvus import MilvusClient, DataType

schema = MilvusClient.create_schema()

schema.add_field(
field_name="product_id",
datatype=DataType.INT64,
is_primary=True
)

schema.add_field(
field_name="product_name",
datatype=DataType.VARCHAR,
max_length=512
)

schema.add_field(
field_name="embedding",
datatype=DataType.FLOAT_VECTOR,
dim=768
)

続いて、上記スキーマでコレクションを作成します。デフォルトデータベースを使用する場合は、db_name パラメータを省略できます。

client.use_database(
db_name="my_database"
)

# create the collection
client.create_collection(
collection_name="prod_collection",
schema=schema
)

ステップ 4: インデックスを作成する。

すべてのベクトルフィールドに対してインデックスを作成する必要があります。必要に応じて、スカラーフィールドにもインデックスを作成できます。

index_params = client.prepare_index_params()

# Add indexes
index_params.add_index(
field_name="embedding",
index_type="AUTOINDEX",
metric_type="COSINE"
)

index_params.add_index(
field_name="product_name",
index_type="AUTOINDEX"
)

client.create_index(
db_name="my_database",
collection_name="prod_collection",
index_params=index_params
)

ステップ 5: データをインポートする。

準備が整ったら、処理済みデータをインポートします。以下の例では、処理済みデータが外部ストレージバケットに保存されていることを前提としています。

バケット内データ形式やストレージ連携については、Format Options を参照してください。

from pymilvus.bulk_writer import bulk_import

# The path should be relative to the root
# of a zilliz cloud volume or an external storage
OBJECT_URLS = [[
"https://s3.us-west-2.amazonaws.com/your-bucket/path/in/external/storage.json"
]]

ACCESS_KEY = "YOUR_STORAGE_ACCESS_KEY"
SECRET_KEY = "YOUR_STORAGE_SECRET_KEY"

res = bulk_import(
api_key="YOUR_ZILLIZ_API_KEY",
url="https://api.cloud.zilliz.com",
project_id="proj-xxxxxxxxxxxxxxxxxxx",
region_id="aws-us-west-2",
db_name="my_database",
collection_name="prod_collection",
object_url=OBJECT_URLS,
access_key=ACCESS_KEY,
secret_key=SECRET_KEY
)

# job-xxxxxxxxxxxxxxxxxxxxx

返却された job ID を使って進捗を確認できます。

import json
from pymilvus.bulk_writer import get_import_progress

# Get bulk-insert job progress
resp = get_import_progress(
api_key="YOUR_ZILLIZ_API_KEY",
url="https://api.cloud.zilliz.com",
cluster_id="inxx-xxxxxxxxxxxxxxxxxxx",
job_id="job-xxxxxxxxxxxxxxxxxxxxx",
)

print(json.dumps(resp.json(), indent=4))

ステップ 6: オンデマンドクラスターを作成する

コレクションの準備ができたら、オンデマンド検索用のオンデマンドクラスターをアタッチします。次のコマンドでクラスターを作成し、クラスター ID を取得できます。

export CONTROL_PLANE_ENDPOINT="https://api.cloud.zilliz.com"

curl --request POST \
--url "${CONTROL_PLANE_ENDPOINT}/v2/clusters/createOnDemandCluster" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"projectId": "proj-xxxxxxxxxxxxxxxxxxx",
"regionId": "aws-us-west-2",
"clusterName": "my-on-demand",
"cuSize": 8,
"autoSuspend": 60
}'

# inxx-xxxxxxxxxxxxx

デフォルトでは、最後のリクエストから 60 秒後にクラスターが自動停止します。ユースケースに応じてこの値は調整できます。

ステップ 7: 検索を実行する。

検索、クエリ、ハイブリッド検索を行う場合は、前の手順で作成したオンデマンドクラスターにセッション経由で接続します。

from pymilvus import MilvusClient

client = MilvusClient(
uri="https://{project-id}.{region}.api.zillizcloud.com",
token="YOUR_API_KEY"
)

session = client.session(cluster_id="inxx-xxxxxxxxxxxxxxx")

# Must match collection vector dimension (example: 768)
query_vector = [0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, ..., 0.9029438446296592]

res = session.search(
db_name="my_database",
collection_name="prod_collection",
anns_field="embedding",
data=[query_vector],
limit=3,
output_fields=["product_id", "product_name"],
search_params={"metric_type": "COSINE"}
)

これでデータを探索して価値の高いサブセットを見つけられます。その後、サービングクラスターに接続し、データを取り込んで本番環境向けに提供できます。