query - ベクトルクエリ
query()は、ベクトル類似性検索を実行し、クエリベクトルと最も類似するドキュメントを見つけるために使用されます。
備考
このインターフェースは、Client接続を使用する場合にのみ利用できます。Clientの詳細については、Clientを参照してください。
前提条件
-
pyseekdbがインストール済みであること。pyseekdbのインストールに関する詳細は、クイックスタートを参照してください。
-
データベースに接続されていること。接続の詳細な操作については、Clientを参照してください。
-
collectionを作成し、対応するデータを挿入済みであること。collectionの作成とデータの挿入の詳細な操作については、create_collection - Collectionの作成および add - データの挿入を参照してください。
リクエストパラメータ
query()
| パラメータ | 値の種類 | 必須 | 説明 | 値の例 |
|---|---|---|---|---|
query_embeddings | List[float] or List[List[float]] | 必須 | 一括検索に使用する単一ベクトルまたはベクトルのリスト。指定されている場合は、そのまま使用します(embedding_functionは無視されます)。指定されていない場合は、query_textを指定する必要があり、collectionにはembedding_functionが必要です。[1.0, 2.0, 3.0] | |
query_texts | str or List[str] | オプション | 単一ベクトルまたはベクトルのリスト。指定されている場合は、そのまま使用します(embedding_functionは無視されます)。指定されていない場合は、documentsを指定する必要があり、collectionにはembedding_functionが必要です。 | ["my query text"] |
n_results | int | 必須 | 類似結果の数。デフォルト値は10 | 3 |
where | dict | オプション | メタデータのフィルター条件。 | {"category": {"$eq": "AI"}} |
where_document | dict | オプション | ドキュメントのフィルター条件。 | {"$contains": "machine"} |
include | List[str] | オプション | 含めるフィールドのリスト:["documents", "metadatas", "embeddings"]。 | ["documents", "metadatas", "embeddings"] |
備考
使用される embedding_function は、collectionに関連付けられたものです(create_collection()またはget_collection()時に設定)。各操作ごとに上書きすることはできません。
リクエスト例
import pyseekdb
# Create a client
client = pyseekdb.Client()
collection = client.get_collection("my_collection")
collection1 = client.get_collection("my_collection1")
# Basic vector similarity query (embedding_function not used)
results = collection.query(
query_embeddings=[1.0, 2.0, 3.0],
n_results=3
)
# Iterate over results
for i in range(len(results["ids"][0])):
print(f"ID: {results['ids'][0][i]}, Distance: {results['distances'][0][i]}")
if results.get("documents"):
print(f"Document: {results['documents'][0][i]}")
if results.get("metadatas"):
print(f"Metadata: {results['metadatas'][0][i]}")
# Query by texts - vectors auto-generated by embedding_function
# Requires: collection must have embedding_function set
results = collection1.query(
query_texts=["my query text"],
n_results=10
)
# The collection's embedding_function will automatically convert query_texts to query_embeddings
# Query by multiple texts (batch query)
results = collection1.query(
query_texts=["query text 1", "query text 2"],
n_results=5
)
# Returns dict with lists of lists, one list per query text
for i in range(len(results["ids"])):
print(f"Query {i}: {len(results['ids'][i])} results")
# Query with metadata filter (using query_texts)
results = collection1.query(
query_texts=["AI research"],
where={"category": {"$eq": "AI"}},
n_results=5
)
# Query with comparison operator (using query_texts)
results = collection1.query(
query_texts=["machine learning"],
where={"score": {"$gte": 90}},
n_results=5
)
# Query with document filter (using query_texts)
results = collection1.query(
query_texts=["neural networks"],
where_document={"$contains": "machine learning"},
n_results=5
)
# Query with combined filters (using query_texts)
results = collection1.query(
query_texts=["AI research"],
where={"category": {"$eq": "AI"}, "score": {"$gte": 90}},
where_document={"$contains": "machine"},
n_results=5
)
# Query with multiple vectors (batch query)
results = collection.query(
query_embeddings=[[1.0, 2.0, 3.0], [2.0, 3.0, 4.0]],
n_results=2
)
# Returns dict with lists of lists, one list per query vector
for i in range(len(results["ids"])):
print(f"Query {i}: {len(results['ids'][i])} results")
# Query with specific fields
results = collection.query(
query_embeddings=[1.0, 2.0, 3.0],
include=["documents", "metadatas", "embeddings"],
n_results=3
)
実引数
| パラメータ | 値の種類 | 必須 | 説明 | 値の例 |
|---|---|---|---|---|
ids | List[List[str]] | 必須 | 追加または変更するID。単一でも配列でもかまいません。 | item1 |
embeddings | [List[List[List[float]]]] | オプション | ベクトル。指定されている場合は、そのまま使用します(embedding_functionは無視されます)。指定しない場合は、ドキュメントを提供することでベクトルを自動生成できます。[0.1, 0.2, 0.3] | |
documents | [List[List[Dict]]] | オプション | ドキュメント。vectorsが指定されていない場合、documentsはコレクションのembedding_functionを使用してベクトルに変換されます。"Document text" | |
metadatas | [List[List[Dict]]] | オプション | メタデータ。 | {"category": "AI"} |
distances | [List[List[Dict]]] | オプション | {"category": "AI"} |
戻り値の例
ID: vec1, Distance: 0.0
Document: None
Metadata: {}
ID: vec2, Distance: 0.025368153802923787
Document: None
Metadata: {}
Query 0: 4 results
Query 1: 4 results
Query 0: 2 results
Query 1: 2 results