hybrid_search - ハイブリッド検索
hybrid_search()は、全文検索とベクトル類似度検索を組み合わせてランキングを生成するために使用されます。
備考
このインターフェースは、Client接続を使用している場合にのみ利用できます。Clientの詳細については、Clientを参照してください。
前提条件
-
pyseekdbがインストール済みであること。pyseekdbのインストールに関する詳細は、クイックスタートを参照してください。
-
データベースに接続されていること。接続操作の詳細については、Clientを参照してください。
-
コレクションを作成し、対応するデータを挿入済みであること。コレクションの作成とデータの挿入に関する詳細な操作については、create_collection - コレクションの作成および add - データの挿入を参照してください。
リクエストパラメータ
hybrid_search(
query={
"where_document": ,
"where": ,
"n_results":
},
knn={
"query_texts":
"where":
"n_results":
},
rank=,
n_results=,
include=
)
-
query:全文検索の設定。以下のパラメータが含まれます:
パラメータ 値の種類 必須 説明 値の例 wheredict オプション Metadataのフィルター条件。 {"category": {"$eq": "AI"}}where_documentdict オプション Documentのフィルター条件。 {"$contains": "machine"}n_resultsint 必須 全文検索の結果数 -
knn:Vector検索の設定。以下のパラメータが含まれます:
パラメータ 値の種類 必須 説明 値の例 query_embeddingsList[float] or List[List[float]] 必須 一括クエリ用の単一ベクトルまたはベクトルのリストです。指定されている場合は、そのまま使用します( embedding_functionは無視されます)。指定されていない場合は、query_textを指定する必要があり、collectionにはembedding_functionが必要です。[1.0, 2.0, 3.0]query_textsstr or List[str] オプション 単一のベクトルまたはベクトルのリストです。指定されている場合は、そのまま使用します( embedding_functionは無視されます)。指定されていない場合は、documentsを指定する必要があり、collectionにはembedding_functionが必要です。["my query text"] wheredict オプション メタデータによるフィルタリング条件です。 {"category": {"$eq": "AI"}}n_resultsint 必須 ベクトル検索の結果数 -
その他のパラメータは以下のとおりです:
パラメータ 値の種類 必須 説明 値の例 rankdict オプション ランキング設定。例: {"rrf": {"rank_window_size": 60, "rank_constant": 60}}{"category": {"$eq": "AI"}}n_resultsint 必須 類似結果の数を返します。デフォルト値は10です 3 includeList[str] オプション 含めるフィールドのリスト: ["documents", "metadatas", "embeddings"]。["documents", "metadatas", "embeddings"]
備考
使用される embedding_function はコレクションに関連付けられています(create_collection() または get_collection() の際に設定)。各操作ごとに上書きすることはできません。
リクエスト例
import pyseekdb
# Create a client
client = pyseekdb.Client()
collection = client.get_collection("my_collection")
collection1 = client.get_collection("my_collection1")
# Hybrid search with query_embeddings (embedding_function not used)
results = collection.hybrid_search(
query={
"where_document": {"$contains": "machine learning"},
"n_results": 10
},
knn={
"query_embeddings": [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], # Used directly
"n_results": 10
},
rank={"rrf": {}},
n_results=5
)
# Hybrid search with both full-text and vector search (using query_texts)
results = collection1.hybrid_search(
query={
"where_document": {"$contains": "machine learning"},
"where": {"category": {"$eq": "science"}},
"n_results": 10
},
knn={
"query_texts": ["AI research"], # Will be embedded automatically
"where": {"year": {"$gte": 2020}},
"n_results": 10
},
rank={"rrf": {}}, # Reciprocal Rank Fusion
n_results=5,
include=["documents", "metadatas", "embeddings"]
)
# Hybrid search with multiple query texts (batch)
results = collection1.hybrid_search(
query={
"where_document": {"$contains": "AI"},
"n_results": 10
},
knn={
"query_texts": ["machine learning", "neural networks"], # Multiple queries
"n_results": 10
},
rank={"rrf": {}},
n_results=5
)
レスポンスパラメータ
ID、distances、metadatas、document、etc を含む検索結果の辞書。