メインコンテンツまでスキップ

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

Zilliz Cloud はオンデマンドのコンピューティングリソースを提供し、類似度検索やクエリを必要なときに実行できます。下図に示すように、リクエストがない場合はコンピューティングリソースが自動的にサスペンドされ、サスペンド中は料金が発生しません。

ZhWHbgOD0o56IpxbQ32ctGaInBe

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

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

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

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

python
# 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 にはデフォルトのデータベースが用意されています。デフォルトのデータベースを使用する場合は、このステップをスキップしてください。以下のように新しいデータベースを作成することもできます。

python
client.create_database(
db_name="my_database"
)

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

データベースの準備ができたら、その中にマネージドコレクションを作成できます。コレクションのカラムを外部データファイルにマッピングする外部コレクションとは異なり、マネージドコレクションではデータをインポートすることで大幅なパフォーマンス向上を実現できます。

次の例では、コレクションスキーマの設定とコレクションの作成方法を示します。

python
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 パラメーターを省略しても問題ありません。

python
client.use_database(
db_name="my_database"
)

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

ステップ 4: インデックスの作成

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

python
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 を参照してください。

python
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

返されたジョブ ID を使って、進捗状況を確認できます。

python
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 を返します。

bash
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: 検索を実行する

検索、クエリ、またはハイブリッド検索を実行する際は、セッションを通じて前のステップで作成したオンデマンドクラスターにアタッチできます。

python
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"]
)

データを探索して最も価値のあるサブセットを見つけたら、サービングクラスターに接続してデータをインポートし、本番環境で利用できるようになります。

Ctrl I