• 關於 Milvus
  • 開始使用
  • 概念
  • 使用者指南
  • 資料匯入
  • AI 工具
  • 管理指南
  • 工具
  • 整合
  • 教學
  • 常見問題
  • API Reference

提升排名器Compatible with Milvus v2.6.2+

Boost Ranker 不僅依賴根據向量距離計算的語意相似性,還能讓您以有意義的方式影響搜尋結果。它是使用元資料過濾快速調整搜尋結果的理想選擇。

當搜尋請求包含 Boost Ranker 函式時,Milvus 會使用函式中的選用篩選條件來尋找搜尋結果候選項目中的匹配項目,並透過應用指定的權重來提升這些匹配項目的得分,幫助提升或降低匹配實體在最終結果中的排名。

何時使用 Boost Ranker

與其他依賴交叉編碼器模型或融合演算法的排名器不同,Boost Ranker 直接將可選的元資料驅動規則注入排名過程,因此更適用於下列情況。

使用案例

範例

為什麼 Boost Ranker 運作良好

商業驅動的內容優先排序

  • 在電子商務搜尋結果中突顯優質產品

  • 提高具有高使用者參與度指標(如觀看、讚好和分享)的內容的能見度

  • 在時間敏感的搜尋應用中提升最新內容

  • 優先處理來自經驗證或可信來源的內容

  • 提升符合精確短語或高相關度關鍵字的結果

您無需重建索引或修改向量嵌入模型 (這些作業可能很花時間),即可即時套用選用的 metadata 過濾器,在搜尋結果中提升或降低特定項目的排名。此機制可實現彈性、動態的搜尋排名,輕鬆適應不斷變化的業務需求。

策略性內容降級

  • 降低庫存量低的項目的顯著性,而不會完全移除它們

  • 在不進行審查的情況下,降低含有潛在不良用語的內容的排名

  • 降低舊文件的排名,同時保持其在技術搜尋中的可得性

  • 巧妙地降低競爭產品在市場搜尋中的能見度

  • 降低具有較低品質指標 (例如格式問題、篇幅較短等) 的內容的相關性

您也可以結合多個 Boost Ranker 來實施更動態、更強大的權重式排名策略。

Boost Ranker 的機制

下圖說明 Boost Ranker 的主要工作流程。

Boost Ranker Mechanism Boost Ranker 機制

當您插入資料時,Milvus 會將資料分散到不同的區段。在搜尋過程中,每個區段會回傳一組候選人,Milvus 會將這些來自所有區段的候選人排序,產生最後的結果。當搜尋請求包含提升排名器時,Milvus 會將其應用於每個區段的候選結果,以防止潛在的精確度損失,並提高召回率。

在最後完成結果之前,Milvus 會以 Boost Ranker 處理這些候選結果,如下所示:

  1. 套用 Boost Ranker 中指定的可選過濾表達式,以識別符合該表達式的實體。

  2. 套用提升排名器中指定的權重,以提升已識別實體的得分。

您無法使用 Boost Ranker 作為多向量混合搜尋的排名器。但是,您可以在任何子要求 (AnnSearchRequest) 中使用它作為排名器。

Boost Ranker 的範例

以下範例說明如何在單向量搜尋中使用 Boost Ranker,該搜尋要求返回前五個最相關的實體,並為具有抽象文件類型的實體的得分加上權重。

  1. 分段收集搜尋結果候選項目。

    下表假定 Milvus 將實體分為兩個區段(00010002),每個區段返回五個候選實體。

    ID

    文件類型

    得分

    等級

    區段

    117

    抽象

    0.344

    1

    0001

    89

    摘要

    0.456

    2

    0001

    257

    身體

    0.578

    3

    0001

    358

    標題

    0.788

    4

    0001

    168

    身體

    0.899

    5

    0001

    46

    身體

    0.189

    1

    0002

    48

    身體

    0265

    2

    0002

    561

    摘要

    0.366

    3

    0002

    344

    摘要

    0.444

    4

    0002

    276

    摘要

    0.845

    5

    0002

  2. 套用 Boost Ranker (doctype='abstract') 中指定的篩選表達式

    如下表中DocType 欄位所示,Milvus 將標示所有doctype 設定為abstract 的實體,以便進一步處理。

    ID

    文件類型

    得分

    排名

    區段

    117

    抽象

    0.344

    1

    0001

    89

    摘要

    0.456

    2

    0001

    257

    身體

    0.578

    3

    0001

    358

    標題

    0.788

    4

    0001

    168

    身體

    0.899

    5

    0001

    46

    身體

    0.189

    1

    0002

    48

    身體

    0265

    2

    0002

    561

    摘要

    0.366

    3

    0002

    344

    摘要

    0.444

    4

    0002

    276

    摘要

    0.845

    5

    0002

  3. 套用 Boost Ranker (weight=0.5) 中指定的權重

    上一步中所有已識別的實體將乘以 Boost Ranker 中指定的權重,導致其排名改變。

    ID

    文件類型

    得分

    加權得分

    (= 得分 x 權重)

    等級

    區段

    117

    抽象

    0.344

    0.172

    1

    0001

    89

    抽象

    0.456

    0.228

    2

    0001

    257

    身體

    0.578

    0.578

    3

    0001

    358

    標題

    0.788

    0.788

    4

    0001

    168

    身體

    0.899

    0.899

    5

    0001

    561

    抽象

    0.366

    0.183

    1

    0002

    46

    身體

    0.189

    0.189

    2

    0002

    344

    抽象

    0.444

    0.222

    3

    0002

    48

    身體

    0.265

    0.265

    4

    0002

    276

    抽象

    0.845

    0.423

    5

    0002

    權重必須是您選擇的浮點數。在類似上述範例的情況中,分數越小表示相關性越高,請使用小於1 的權重。

  4. 根據加權分數匯集所有區段的候選人,以確定結果。

    ID

    文件類型

    分數

    加權得分

    等級

    區段

    117

    抽象

    0.344

    0.172

    1

    0001

    561

    抽象

    0.366

    0.183

    2

    0002

    46

    身體

    0.189

    0.189

    3

    0002

    344

    抽象

    0.444

    0.222

    4

    0002

    89

    抽象

    0.456

    0.228

    5

    0001

Boost Ranker 的使用

在本節中,您將看到如何使用 Boost Ranker 來影響單向量搜尋結果的範例。

建立 Boost Ranker

在傳送 Boost Ranker 作為搜尋請求的排名函式之前,您應該先將 Boost Ranker 正確地定義為排名函式,如下所示:

from pymilvus import Function, FunctionType

ranker = Function(
    name="boost",
    input_field_names=[], # Must be an empty list
    function_type=FunctionType.RERANK,
    params={
        "reranker": "boost",
        "filter": "doctype == 'abstract'",
        "random_score": { 
            "seed": 126,
            "field": "id"
        },
        "weight": 0.5
    }
)
import io.milvus.v2.service.vector.request.ranker.BoostRanker;

BoostRanker ranker = BoostRanker.builder()
        .name("boost")
        .filter("doctype == \"abstract\"")
        .weight(5.0f)
        .randomScoreField("id")
        .randomScoreSeed(126)
        .build();
// go
import {FunctionType} from '@zilliz/milvus2-sdk-node';

const ranker = {
  name: "boost",
  input_field_names: [],
  type: FunctionType.RERANK,
  params: {
    reranker: "boost",
    filter: "doctype == 'abstract'",
    random_score: {
      seed: 126,
      field: "id",
    },
    weight: 0.5,
  },
};

# restful

參數

需要嗎?

說明

值/範例

name

此功能的唯一識別碼

"boost"

input_field_names

要應用函式的向量欄位清單 (Boost Ranker 必須為空)

[]

function_type

要調用的函數類型;使用RERANK 指定重排策略

FunctionType.RERANK

params.reranker

指定重排策略的類型。

必須設定為boost 才能使用 Boost Ranker。

"boost"

params.weight

指定原始搜尋結果中任何匹配實體的得分所乘以的權重。

該值應該是浮點數。

  • 若要強調匹配實體的重要性,請將其設定為可提高分數的值。

  • 若要降低匹配實體的重要性,請將此參數設定為可降低其分數的值。

1

params.filter

指定用於在搜尋結果實體中匹配實體的篩選表達式。它可以是任何有效的基本篩選表達式,請參閱篩選說明

注意:只能使用基本運算符號,例如==>< 。使用進階運算符號,例如text_matchphrase_match ,會降低搜尋效能。

"doctype == 'abstract'"

params.random_score

指定隨機函數,隨機產生01 之間的值。它有以下兩個可選參數:

  • seed (number)指定用於啟動偽隨機數生成器 (PRNG) 的初始值。

  • field (string)指定欄位的名稱,其值將用作產生隨機數的隨機因子。具有唯一值的欄位即可。

    建議您同時設定seedfield ,以使用相同的種子和欄位值來確保各代的一致性。

{"seed": 126, "field": "id"}

使用單一 Boost Ranker 搜尋

一旦 Boost Ranker 函式準備就緒,您就可以在搜尋請求中引用它。以下範例假設您已經建立了一個集合,這個集合有以下欄位:idvectordoctype

from pymilvus import MilvusClient

# Connect to the Milvus server
client = MilvusClient(
    uri="http://localhost:19530",
    token="root:Milvus"
)

# Assume you have a collection set up

# Conduct a similarity search using the created ranker
client.search(
    collection_name="my_collection",
    data=[[-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911]],
    anns_field="vector",
    params={},
    output_field=["doctype"],
    ranker=ranker
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.response.SearchResp;
import io.milvus.v2.service.vector.request.data.FloatVec;

MilvusClientV2 client = new MilvusClientV2(ConnectConfig.builder()
        .uri("http://localhost:19530")
        .token("root:Milvus")
        .build());
        
SearchResp searchReq = client.search(SearchReq.builder()
        .collectionName("my_collection")
        .data(Collections.singletonList(new FloatVec(new float[]{-0.619954f, 0.447943f, -0.174938f, -0.424803f, -0.864845f})))
        .annsField("vector")
        .outputFields(Collections.singletonList("doctype"))
        .functionScore(FunctionScore.builder()
                .addFunction(ranker)
                .build())
        .build());
SearchResp searchResp = client.search(searchReq);
// go
import { MilvusClient } from '@zilliz/milvus2-sdk-node';

// Connect to the Milvus server
const client = new MilvusClient({
  address: 'localhost:19530',
  token: 'root:Milvus'
});

// Assume you have a collection set up

// Conduct a similarity search
const searchResults = await client.search({
  collection_name: 'my_collection',
  data: [-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911],
  anns_field: 'vector',
  output_fields: ['doctype'],
  rerank: ranker,
});

console.log('Search results:', searchResults);
# restful

使用多個 Boost Ranker 搜尋

您可以在單一搜尋結合多個 Boost Ranker 來影響搜尋結果。要做到這一點,請建立多個 Boost Ranker,在FunctionScore範例中引用它們,並在搜尋請求中使用FunctionScore範例作為排名器。

以下範例顯示如何透過套用介於0.81.2 之間的權重,來修改所有已識別實體的得分。

from pymilvus import MilvusClient, Function, FunctionType, FunctionScore

# Create a Boost Ranker with a fixed weight
fix_weight_ranker = Function(
    name="boost",
    input_field_names=[], # Must be an empty list
    function_type=FunctionType.RERANK,
    params={
        "reranker": "boost",
        "weight": 0.8
    }
)

# Create a Boost Ranker with a randomly generated weight between 0 and 0.4
random_weight_ranker = Function(
    name="boost",
    input_field_names=[], # Must be an empty list
    function_type=FunctionType.RERANK,
    params={
        "reranker": "boost",
        "random_score": {
            "seed": 126,
        },
        "weight": 0.4
    }
)

# Create a Function Score
ranker = FunctionScore(
    functions=[
        fix_weight_ranker, 
        random_weight_ranker
    ],
    params={
        "boost_mode": "Multiply",
        "function_mode": "Sum"
    }
)

# Conduct a similarity search using the created Function Score
client.search(
    collection_name="my_collection",
    data=[[-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911]],
    anns_field="vector",
    params={},
    output_field=["doctype"],
    ranker=ranker
)
import io.milvus.common.clientenum.FunctionType;
import io.milvus.v2.service.collection.request.CreateCollectionReq;

CreateCollectionReq.Function fixWeightRanker = CreateCollectionReq.Function.builder()
                 .functionType(FunctionType.RERANK)
                 .name("boost")
                 .param("reranker", "boost")
                 .param("weight", "0.8")
                 .build();
                 
CreateCollectionReq.Function randomWeightRanker = CreateCollectionReq.Function.builder()
                 .functionType(FunctionType.RERANK)
                 .name("boost")
                 .param("reranker", "boost")
                 .param("weight", "0.4")
                 .param("random_score", "{\"seed\": 126}")
                 .build();

Map<String, String> params = new HashMap<>();
params.put("boost_mode","Multiply");
params.put("function_mode","Sum");     
FunctionScore ranker = FunctionScore.builder()
                 .addFunction(fixWeightRanker)
                 .addFunction(randomWeightRanker)
                 .params(params)
                 .build()

SearchResp searchReq = client.search(SearchReq.builder()
                 .collectionName("my_collection")
                 .data(Collections.singletonList(new FloatVec(new float[]{-0.619954f, 0.447943f, -0.174938f, -0.424803f, -0.864845f})))
                 .annsField("vector")
                 .outputFields(Collections.singletonList("doctype"))
                 .addFunction(ranker)
                 .build());
SearchResp searchResp = client.search(searchReq);
// go
import {FunctionType} from '@zilliz/milvus2-sdk-node';

const fix_weight_ranker = {
  name: "boost",
  input_field_names: [],
  type: FunctionType.RERANK,
  params: {
    reranker: "boost",
    weight: 0.8,
  },
};

const random_weight_ranker = {
  name: "boost",
  input_field_names: [],
  type: FunctionType.RERANK,
  params: {
    reranker: "boost",
    random_score: {
      seed: 126,
    },
    weight: 0.4,
  },
};

const ranker = {
  functions: [fix_weight_ranker, random_weight_ranker],
  params: {
    boost_mode: "Multiply",
    function_mode: "Sum",
  },
};

await client.search({
  collection_name: "my_collection",
  data: [[-0.619954382375778, 0.4479436794798608, -0.17493894838751745, -0.4248030059917294, -0.8648452746018911]],
  anns_field: "vector",
  params: {},
  output_field: ["doctype"],
  ranker: ranker
});

# restful

具體來說,有兩個 Boost Ranker:一個會對所有找到的實體套用固定的權重,另一個則會隨機指定權重。接著,我們在一個FunctionScore 中引用這兩個排名器,這也定義了權重如何影響找到的實體的分數。

下表列出了建立FunctionScore範例所需的參數。

參數

需要?

說明

值/範例

functions

在清單中指定目標排名者的名稱。

["fix_weight_ranker", "random_weight_ranker"]

params.boost_mode

指定指定的權重如何影響任何匹配實體的得分。

可能的值為

  • Multiply

    表示加權值等於匹配實體的原始分數乘以指定的權重。

    這是預設值。

  • Sum

    表示加權值等於匹配實體的原始分數與指定權重的總和。

"Sum"

params.function_mode

指定如何處理來自不同 Boost Rankers 的加權值。

可能的值為

  • Multiply

    表示匹配實體的最終得分等於來自所有 Boost Rankers 的加權值的乘積。

    這是預設值。

  • Sum

    表示匹配實體的最終得分等於所有 Boost Rankers 的加權值之和。

"Sum"