Skip to main content

Weighted Ranker

Weighted Ranker intelligently combines and prioritizes results from multiple search paths by assigning different importance weights to each. Similar to how a skilled chef balances multiple ingredients to create the perfect dish, Weighted Ranker balances different search results to deliver the most relevant combined outcomes. This approach is ideal when searching across multiple vector fields or modalities where certain fields should contribute more significantly to the final ranking than others.

When to use Weighted Ranker

Weighted Ranker is specifically designed for hybrid search scenarios where you need to combine results from multiple vector search paths. It's particularly effective for:

Use CaseExampleWhy Weighted Ranker Works Well
E-commerce searchProduct search combining image similarity and text descriptionAllows retailers to prioritize visual similarity for fashion items while emphasizing text descriptions for technical products
Media content searchVideo retrieval using both visual features and audio transcriptsBalances the importance of visual content versus spoken dialogue based on query intent
Document retrievalEnterprise document search with multiple embeddings for different sectionsGives higher weight to title and abstract embeddings while still considering full-text embeddings

If your hybrid search application requires combining multiple search paths while controlling their relative importance, Weighted Ranker is your ideal choice.

Mechanism of Weighted Ranker

The main workflow of the WeightedRanker strategy is as follows:

  1. Collect Search Scores: Gather the results and scores from each path of vector search (score_1, score_2).

  2. Score Normalization: Each search may use different similarity metrics, resulting in varied score distributions. For instance, using Inner Product (IP) as a similarity type could result in scores ranging from [−∞,+∞], while using Euclidean distance (L2) results in scores ranging from [0,+∞]. Because the score ranges from different searches vary and cannot be directly compared, it is necessary to normalize the scores from each path of search. Typically, arctan function is applied to transform the scores into a range between [0, 1] (score_1_normalized, score_2_normalized). Scores closer to 1 indicate higher similarity.

  3. Assign Weights: Based on the importance assigned to different vector fields, weights (wi) are allocated to the normalized scores (score_1_normalized, score_2_normalized). The weights of each path should range between [0,1]. The resulting weighted scores are score_1_weighted and score_2_weighted.

  4. Merge Scores: The weighted scores (score_1_weighted, score_2_weighted) are ranked from highest to lowest to produce a final set of scores (score_final).

GdmNwbkN8haZO8bpQkOc2NIWnqF

Example of Weighted Ranker

This example demonstrates a multimodal Hybrid Search (topK=5) involving images and text and illustrates how the WeightedRanker strategy reranks the results from two ANN searches.

  • Results of ANN search on images (topK=5):

    IDScore (image)
    1010.92
    2030.88
    1500.85
    1980.83
    1750.8
  • Results of ANN search on texts (topK=5):

    IDScore (text)
    1980.91
    1010.87
    1100.85
    1750.82
    2500.78
  • Use WeightedRanker assign weights to image and text search results. Suppose the weight for the image ANN search is 0.6 and the weight for the text search is 0.4.

    IDScore (image)Score (text)Weighted Score
    1010.920.870.6×0.92+0.4×0.87=0.90
    2030.88N/A0.6×0.88+0.4×0=0.528
    1500.85N/A0.6×0.85+0.4×0=0.51
    1980.830.910.6×0.83+0.4×0.91=0.86
    1750.800.820.6×0.80+0.4×0.82=0.81
    110Not in Image0.850.6×0+0.4×0.85=0.34
    250Not in Image0.780.6×0+0.4×0.78=0.312
  • The final results after reranking(topK=5):

    RankIDFinal Score
    11010.90
    21980.86
    31750.81
    42030.528
    51500.51

Usage of Weighted Ranker

When using the WeightedRanker strategy, it is necessary to input weight values. The number of weight values to input should correspond to the number of basic ANN search requests in the Hybrid Search. The input weight values should fall in the range of [0,1], with values closer to 1 indicating greater importance.

Create a Weighted Ranker

For example, suppose there are two basic ANN search requests in a Hybrid Search: text search and image search. If the text search is considered more important, it should be assigned a greater weight.

python
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
}
)
ParameterRequired?DescriptionValue/Example
nameYesUnique identifier for this Function"weight"
input_field_namesYesList of vector fields to apply the function to (must be empty for Weighted Ranker)[]
function_typeYesThe type of Function to invoke; use RERANK to specify a reranking strategyFunctionType.RERANK
params.rerankerYesSpecifies the reranking method to use.
Must be set to weighted to use Weighted Ranker.
"weighted"
params.weightsYesArray of weights corresponding to each search path; values ∈ [0,1].
For details, refer to Mechanism of Weighted Ranker.
[0.1, 0.9]
params.norm_scoreNoWhether to normalize raw scores (using arctan) before weighting.
For details, refer to Mechanism of Weighted Ranker.
True

Weighted Ranker is designed specifically for hybrid search operations that combine multiple vector fields. When performing hybrid search, you must specify the weights for each search path:

python
from pymilvus import MilvusClient, AnnSearchRequest

# Connect to Milvus server
milvus_client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)

# 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"]
)

For more information on hybrid search, refer to Multi-Vector Hybrid Search.

Ctrl I