跳到主要内容

hybrid_search - 混合搜索

hybrid_search() 用于将全文搜索和向量相似度搜索与排名相结合。

信息

仅支持在使用 Client 连接时,才能使用该接口。关于 Client 的详细介绍,参见 Client

前提条件

请求参数

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_textcollection 必须具有 embedding_function[1.0, 2.0, 3.0]
    query_textsstr or List[str]可选单个 vectors 或 vectors 列表;如果提供,则直接使用(忽略 embedding_function);如果没有提供,则必须提供 documents,同时 collection 必须具有 embedding_function["my query text"]
    wheredict可选Metadata 筛选条件。{"category": {"$eq": "AI"}}
    n_resultsint必须vector 搜索的结果数
  • 其他参数如下:

    |参数|取值类型|是否必选|描述|取值示例| |rank|dict |可选|排名配置,例如:{"rrf": {"rank_window_size": 60, "rank_constant": 60}}|{"category": {"$eq": "AI"}}| |n_results|int|必须|返回相似的结果数,默认值为 10|3| |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")

# 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 等的搜索结果字典。

相关操作