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

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

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

ZhWHbgOD0o56IpxbQ32ctGaInBe

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

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

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

  • オンデマンドコンピューティング用データベース内のマネージドコレクションには、ロード操作は必要ありません。

python
from pymilvus import MilvusClient

# 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_urls=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",
project_id="proj-xxxxxxxxxxxxxxxxxxx",
region_id="aws-us-west-2",
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"]
)

その後、データを探索して最も価値のあるサブセットを見つけることができます。さらに、サービングクラスターに接続し、データをインポートして、本番環境で提供できます。