加權排名器
Weighted Ranker 可透過為每個搜尋路徑指定不同的重要性權重,智慧地結合多個搜尋路徑的結果並排定優先順序。與技藝高超的廚師平衡多種材料以創造完美菜餚的方式類似,Weighted Ranker 平衡不同的搜尋結果,以提供最相關的結合結果。當您在多個向量領域或模式之間進行搜尋時,如果某些領域對最終排名的貢獻應該比其他領域更大,那麼這種方法就是最理想的選擇。
何時使用加權排名器
Weighted Ranker 專門設計用於混合搜尋的情況,在這種情況下,您需要結合來自多個向量搜尋路徑的結果。它對以下情況特別有效
使用案例 |
範例 |
為什麼加權排名器運作良好 |
|---|---|---|
電子商務搜尋 |
結合圖片相似度與文字描述的產品搜尋 |
允許零售商優先處理時尚商品的視覺相似性,同時強調技術產品的文字說明 |
媒體內容搜尋 |
同時使用視覺特徵和音訊謄本的視訊檢索 |
根據查詢意圖平衡視覺內容與口語對話的重要性 |
文件檢索 |
針對不同部分使用多重嵌入的企業文件搜尋 |
在考慮全文嵌入的同時,給予標題和摘要嵌入較高的權重 |
如果您的混合搜尋應用需要結合多個搜尋路徑,同時控制它們的相對重要性,Weighted Ranker 就是您的理想選擇。
加權排名機制
WeightedRanker 策略的主要工作流程如下:
收集搜尋分數:收集向量搜尋每條路徑的結果和分數 (score_1, score_2)。
Score Normalization(分數規範化):每次搜尋可能會使用不同的相似度指標,造成不同的分數分佈。例如,使用 Inner Product (IP) 作為相似度類型可能會產生 [-∞,+∞] 的分數範圍,而使用 Euclidean distance (L2) 則會產生 [0,+∞] 的分數範圍。由於不同搜尋的分數範圍各異,無法直接比較,因此有必要將各搜尋路徑的分數歸一化。一般而言,
arctan函數會將分數轉換成 [0, 1] 之間的範圍 (score_1_normalized, score_2_normalized)。分數越接近 1 表示相似度越高。指定權重:根據分配給不同向量領域的重要性,將權重 (wi) 分配給歸一化的分數 (score_1_normalized, score_2_normalized)。每條路徑的權重範圍應該在 [0,1] 之間。所得的加權分數為 score_1_weighted 及 score_2_weighted。
合併分數:將加權分數 (score_1_weighted, score_2_weighted) 由高到低排序,產生最後的一組分數 (score_final)。
加權排名器
加權排名器範例
本範例展示了一個涉及圖片和文字的多模式混合搜尋 (topK=5),並說明 WeightedRanker 策略如何將兩個 ANN 搜尋的結果重新排序。
圖像的 ANN 搜尋結果 (topK=5):
ID
得分 (影像)
101
0.92
203
0.88
150
0.85
198
0.83
175
0.8
文本的 ANN 檢索結果(topK=5): ID
ID
分數 (文字)
198
0.91
101
0.87
110
0.85
175
0.82
250
0.78
使用 WeightedRanker 為圖片和文字搜尋結果指定權重。假設影像 ANN 搜尋的權重為 0.6,文字搜尋的權重為 0.4。
ID
得分 (影像)
得分(文字)
加權得分
101
0.92
0.87
0.6×0.92+0.4×0.87=0.90
203
0.88
不適用
0.6×0.88+0.4×0=0.528
150
0.85
不適用
0.6×0.85+0.4×0=0.51
198
0.83
0.91
0.6×0.83+0.4×0.91=0.86
175
0.80
0.82
0.6×0.80+0.4×0.82=0.81
110
不在影像中
0.85
0.6×0+0.4×0.85=0.34
250
不在影像中
0.78
0.6×0+0.4×0.78=0.312
重排後的最終結果(topK=5):
排名
ID
最終得分
1
101
0.90
2
198
0.86
3
175
0.81
4
203
0.528
5
150
0.51
使用加權排名器
使用 WeightedRanker 策略時,必須輸入權重值。輸入的權重值數量應對應於混合搜索中基本 ANN 搜索請求的數量。輸入的權重值應在 [0,1] 的範圍內,值越接近 1 表示重要性越高。
建立加權排名器
例如,假設混合搜尋中有兩個基本 ANN 搜尋請求:文字搜尋和圖像搜尋。如果文字搜尋被視為更重要,就應該給予較大的權重。
Milvus 2.6.x 及以後的版本可讓您直接透過Function API 配置重排策略。如果您使用的是較早的版本(v2.6.0 之前),請參閱Reranking文件以取得設定指示。
from pymilvus import Function, FunctionType
rerank = Function(
name="weight",
input_field_names=[], # Must be an empty list
function_type=FunctionType.RERANK,
params={
"reranker": "weighted",
"weights": [0.1, 0.9],
"norm_score": True # Optional
}
)
import io.milvus.common.clientenum.FunctionType;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
CreateCollectionReq.Function rerank = CreateCollectionReq.Function.builder()
.name("weight")
.functionType(FunctionType.RERANK)
.param("reranker", "weighted")
.param("weights", "[0.1, 0.9]")
.param("norm_score", "true")
.build();
import { FunctionType } from '@zilliz/milvus2-sdk-node';
const rerank = {
name: "weight",
input_field_names: [],
function_type: FunctionType.RERANK,
params: {
reranker: "weighted",
weights: [0.1, 0.9],
norm_score: true
}
};
// Go
# Restful
參數 |
需要嗎? |
說明 |
值/範例 |
|---|---|---|---|
|
是 |
此功能的唯一識別碼 |
|
|
是 |
要應用函式的向量欄位清單(對於加權排序器必須為空) |
[] |
|
是 |
要調用的函數類型;使用 |
|
|
是 |
指定要使用的排序方法。 必須設定為 |
|
|
是 |
每個搜尋路徑對應的權重陣列;值∈ [0,1]。 如需詳細資訊,請參閱Weighted Ranker 的機制。 |
|
|
否 |
是否在加權之前對原始分數進行規範化(使用 arctan)。 詳情請參閱加權排名機制。 |
|
應用於混合搜尋
Weighted Ranker 專為結合多向量領域的混合搜尋作業而設計。執行混合搜尋時,您必須指定每條搜尋路徑的權重:
from pymilvus import MilvusClient, AnnSearchRequest
# Connect to Milvus server
milvus_client = MilvusClient(uri="http://localhost:19530")
# Assume you have a collection setup
# Define text vector search request
text_search = AnnSearchRequest(
data=["modern dining table"],
anns_field="text_vector",
param={},
limit=10
)
# Define image vector search request
image_search = AnnSearchRequest(
data=[image_embedding], # Image embedding vector
anns_field="image_vector",
param={},
limit=10
)
# Apply Weighted Ranker to product hybrid search
# Text search has 0.8 weight, image search has 0.3 weight
hybrid_results = milvus_client.hybrid_search(
collection_name,
[text_search, image_search], # Multiple search requests
ranker=rerank, # Apply the weighted ranker
limit=10,
output_fields=["product_name", "price", "category"]
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.AnnSearchReq;
import io.milvus.v2.service.vector.request.HybridSearchReq;
import io.milvus.v2.service.vector.response.SearchResp;
import io.milvus.v2.service.vector.request.data.EmbeddedText;
import io.milvus.v2.service.vector.request.data.FloatVec;
MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
.uri("http://localhost:19530")
.build());
List<AnnSearchReq> searchRequests = new ArrayList<>();
searchRequests.add(AnnSearchReq.builder()
.vectorFieldName("text_vector")
.vectors(Collections.singletonList(new EmbeddedText("\"modern dining table\"")))
.limit(10)
.build());
searchRequests.add(AnnSearchReq.builder()
.vectorFieldName("image_vector")
.vectors(Collections.singletonList(new FloatVec(imageEmbedding)))
.limit(10)
.build());
HybridSearchReq hybridSearchReq = HybridSearchReq.builder()
.collectionName(COLLECTION_NAME)
.searchRequests(searchRequests)
.ranker(ranker)
.limit(10)
.outputFields(Arrays.asList("product_name", "price", "category"))
.build();
SearchResp searchResp = client.hybridSearch(hybridSearchReq);
import { MilvusClient, FunctionType } from "@zilliz/milvus2-sdk-node";
const milvusClient = new MilvusClient({ address: "http://localhost:19530" });
const text_search = {
data: ["modern dining table"],
anns_field: "text_vector",
param: {},
limit: 10,
};
const image_search = {
data: [image_embedding],
anns_field: "image_vector",
param: {},
limit: 10,
};
const rerank = {
name: "weight",
input_field_names: [],
function_type: FunctionType.RERANK,
params: {
reranker: "weighted",
weights: [0.1, 0.9],
norm_score: true,
},
};
const search = await milvusClient.search({
collection_name: collection_name,
limit: 10,
data: [text_search, image_search],
rerank: rerank,
output_fields = ["product_name", "price", "category"],
});
// go
# restful
如需混合搜尋的詳細資訊,請參閱多向量混合搜尋。