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

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

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

ZhWHbgOD0o56IpxbQ32ctGaInBe

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

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

📘注意
  • マネージドコレクションの操作では、認証のために API キー が必要です。このフローでは 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"
)

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

Zilliz Cloud にはデフォルトのデータベースが用意されています。それを使用する場合は、このステップをスキップしてください。以下のようにデータベースを作成することもできます。

python
client.create_database(
db_name="my_database"
)

Step 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
)

Step 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: データをインポートする。

すべての設定が完了したら、処理済みデータをインポートできます。以下の例では、処理済みデータを外部ストレージバケットに保存していることを前提としています。

バケット内のデータ形式またはストレージ統合については、形式オプションを参照してください。

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