全部產品
Search
文件中心

:過濾配置

更新時間:May 20, 2026

PAI-Rec 引擎已經內建了多個過濾模板,包括曝光過濾、狀態過濾、數量調整過濾等。

如何配置

過濾的配置對應配置總覽中的 FilterConfs,FilterConfs 是一個[]object 結構,可以定義多個過濾策略。

過濾公用配置一覽

每種召回配置,都會用到公用配置中的一部分,在此統一解釋,單獨的召回配置中則不再贅述。

配置樣本:

{
    "FilterConfs": [
        {
            "Name": "",
            "FilterType": "",
            "Dimension": "",
            "DaoConf": {},
            "AdjustCountConfs": [],
            "ItemStateDaoConf": {},
            "FilterParams": [],
            "DiversityDaoConf": {},
            "FilterVal": {}
        }
    ]
}

欄位

類型

是否必填

描述

Name

string

過濾的自訂名稱,可以在 FilterNames 中引用

FilterType

string

引擎內建過濾類型,枚舉值,目前支援:

  • User2ItemExposureFilter

  • User2ItemCustomFilter

  • AdjustCountFilter

  • PriorityAdjustCountFilter

  • ItemStateFilter

  • ItemCustomFilter

  • CompletelyFairFilter

  • GroupWeightCountFilter

  • DimensionFieldUniqueFilter

Dimension

string

item 的維度欄位

DaoConf

DaoConfig

資料來源表的一些資訊

AdjustCountConfs

優先順序數量調整過濾的配置

ItemStateDaoConf

狀態過濾的配置

FilterParams

上下文條件的配置

曝光過濾(User2ItemExposureFilter)

很多業務情境都需要曝光過濾來避免重複推薦,通常的曝光過濾做法是偽曝光和真實曝光的組合。

偽曝光:因為即時日誌有延時等原因,我們並不能立刻知道哪些 item 被曝光,所以我們把Recommendation Engine返回的 item 列表作為偽曝光列表。

真實曝光:需要客戶的即時日誌,通過Flink 這種Realtime Compute引擎寫入資料庫,然後被PAI-Rec 引擎消費。

不同資料來源的曝光過濾有以下幾個公用參數配置:

欄位名

類型

是否必填

描述

Name

string

自訂過濾名稱

FilterType

string

過濾類型,固定值 User2ItemExposureFilter

MaxItems

int

擷取最近的條目批次數量,相當於 limit ${MaxItems}, MaxItems 這裡不是指的具體的 item 數量, 而是批次的概念,也就是說一次推薦請求算作是一次,和一次請求裡有多個具體的物料 item 無關

TimeInterval

int

按照時間戳記取最近多長時間內的條目,單位秒

WriteLog

bool

是否寫入曝光日誌

ClearLogIfNotEnoughScene

string

對某個情境下曝光表中的資料進行刪除

OnlyLogUserExposeFlag

bool

不實際過濾資料,如果已經曝光過,只會在item中添加_is_exposure_屬性。在某些情境下,物品池太小,很容易被曝光過濾全部過濾掉,可以在重排階段根據_is_exposure_屬性進行提降權,避免返回空。

GenerateItemDataFuncName

string

構造寫入曝光表item資料的函數,為空白時,會使用 PAI-Rec內建的函數,內建函數只返回 item_id

GenerateItemDataExpr

string

用運算式構造寫入曝光表的 item 資料。運算式文法參考 expr-lang/expr,可用變數:uiditem_iditem(物料屬性)、sprintfcontext。為空白時使用 GenerateItemDataFuncName 註冊的函數,預設只返回 item_id。支援資料來源:Hologres、TableStore、FeatureStore、RecallEngine

GenerateUserDataExpr

string

用運算式構造曝光表中的 user 標識資料。運算式文法參考 expr-lang/expr,可用變數:uidcontextsprintf。為空白時預設使用 uid。支援資料來源:Hologres、FeatureStore、RecallEngine

WriteLogExcludeScenes

[]string

哪些情境不進行曝光日誌的寫入

Hologres

{
    "FilterConfs": [
        {
            "Name": "holo_exposure_filter",
            "FilterType": "User2ItemExposureFilter",
            "MaxItems": 100,
            "TimeInterval": 172800,
            "WriteLog": true,
            "DaoConf": {
                "AdapterType": "hologres",
                "HologresName": "holo_info",
                "HologresTableName": "exposure_history"
            }
        }
    ]
}

DaoConf 配置說明

欄位

類型

是否必填

描述

AdapterType

string

資料來源的類型,取值 hologres

HologresName

string

在資料來源配置(HologresConfs)中配置好的 holo 的自訂名稱,如資料來源配置中的 holo_info

HologresTableName

string

曝光表名稱

曝光表定義注意這裡可以 按照實際的情況設定 time_to_live_in_seconds 。

BEGIN;
CREATE TABLE "exposure_history" (
 "uid" text NOT NULL,
 "item" text NOT NULL,
 "create_time" int4 NOT NULL
);
CALL SET_TABLE_PROPERTY('"exposure_history"', 'orientation', 'column');
CALL set_table_property('"exposure_history"', 'distribution_key', 'uid');
CALL SET_TABLE_PROPERTY('"exposure_history"', 'clustering_key', '"uid","create_time"');
CALL SET_TABLE_PROPERTY('"exposure_history"', 'segment_key', '"create_time"');
CALL SET_TABLE_PROPERTY('"exposure_history"', 'bitmap_columns', '"uid","item"');
CALL SET_TABLE_PROPERTY('"exposure_history"', 'dictionary_encoding_columns', '"uid","item"');
CALL SET_TABLE_PROPERTY('"exposure_history"', 'time_to_live_in_seconds', '172800');
comment on table "exposure_history" is '曝光記錄表';
COMMIT;

PAI-FeatureStore

FeatureStore 內建的線上資料來源 FeatureDB使用布隆過濾器演算法支援PAI-Rec 的曝光過濾的需求。

FeatureStore 通過建立即時FeatureView來支援曝光表。設定參考如下:

視圖名稱填寫為 user_expose寫入方式選擇自訂表格結構分區數設定為 5副本數設定為 1

重點參數說明下:

  1. 類型選擇 即時 。

  2. 特徵實體 選擇 user 。

  3. 特徵欄位需要填入 user_id、item_id、timestamp。 其中 user_id 為主鍵, timestamp 勾選 事件時間。 主鍵 和 事件時間是必須選擇的。 資料類型上, user_id、item_id 都是 string , timestamp 是 int64 。

  4. 特徵生命週期,這裡預設是 2 天。 需要按實際情況調整。 對於一個使用者來說, 曝光的條目需要保留多長時間。

  5. 進階配置必填, 輸入 {"table_type":"bloom", "expose_count":5000},table_type 說明建立的是支援 bloom filter 的 table 。 expose_count 是對於單個使用者來說,保留的曝光條目的最大數量。

    PAI-Rec 引擎中的曝光過濾配置參考如下:

{
    "FilterConfs": [
        {
            "Name": "fs_exposure_filter",
            "FilterType": "User2ItemExposureFilter",
            "TimeInterval": 300,
            "WriteLog": true,
            "DaoConf": {
                "AdapterType": "featurestore",
                "FeatureStoreName": "fs_pairec",
                "FeatureStoreViewName": "user_expose"
            }
        }
    ]
}

TimeInterval 單位是秒。 這塊是引擎寫入偽曝光條目的最大保留時間。當 WriteLog = true時是有效。上面是偽曝光的配置,如果是真實曝光,PAI-Rec 引擎不負責寫入資料,設定WriteLog為false,並且TimeInterval可以不設定。

真實曝光的配置如下:

"FilterConfs": [
        {
            "Name": "fs_exposure_filter2",
            "FilterType": "User2ItemExposureFilter",
            "WriteLog": false,
            "DaoConf": {
                "AdapterType": "featurestore",
                "FeatureStoreName": "fs_pairec",
                "FeatureStoreViewName": "user_expose"
            }
        }
    ]

一般情況下,偽曝光和真實曝光的生命週期不同,推薦使用不同的FeatureView表來分別支援偽曝光和真實曝光。

真實曝光的資料寫入可以參考寫入特徵

DaoConf 配置說明

欄位

類型

是否必填

描述

AdapterType

string

資料來源的類型,取值 featurestore

FeatureStoreName

string

在資料來源配置(FeatureStoreConfs)中配置好的 FeatureStore 的自訂名稱,如資料來源配置中的 fs_pairec

FeatureStoreViewName

string

曝光表FeatureView名稱

Redis

{
    "FilterConfs": [
        {
            "Name": "redis_exposure_filter",
            "FilterType": "User2ItemExposureFilter",
            "MaxItems": 100,
            "TimeInterval": 172800,
            "WriteLog": true,
            "DaoConf": {
                "AdapterType": "redis",
                "RedisName": "redis_info",
                "RedisPrefix": "exposure_"
            }
        }
    ]
}

DaoConf 配置說明

欄位

類型

是否必填

描述

AdapterType

string

資料來源的類型,取值 redis

RedisName

string

在資料來源配置(RedisConfs)中配置好的 Redis 的自訂名稱,如資料來源配置中的 redis_info

RedisPrefix

string

曝光資料的 key 首碼,key = RedisPrefix + uid

TableStore

{
    "FilterConfs": [
        {
            "Name": "ots_exposure_filter",
            "FilterType": "User2ItemExposureFilter",
            "MaxItems": 100,
            "TimeInterval": 172800,
            "WriteLog": true,
            "DaoConf": {
                "AdapterType": "tablestore",
                "TableStoreName": "tablestore_info",
                "TableStoreTableName": "exposure_history"
            }
        }
    ]
}

DaoConf 配置說明

欄位

類型

是否必填

描述

AdapterType

string

資料來源的類型,枚舉值,如 hologres、mysql、tablestore 等

TableStoreName

string

在資料來源配置(TableStoreConfs)中配置好的 tablestore 的自訂名稱,如資料來源配置中的 tablestore_info

TableStoreTableName

string

曝光表名稱

生命週期:(使用者需要自訂,必須有一個明確的周期)

maxVersion: 1 

欄位

類別

類型

說明

樣本

user_id

主鍵

string

使用者唯一id

10944750

auto_id

主鍵

integer

自增列

item_ids

屬性

string

item 唯一id 列表,多個以 ',' 分隔, 如果一次曝光可以包含多個 item_id 的話,可以插入一行資料即可

17019277,17019278

曝光物品沉底

在某些情境下,使用者的召回池很小,如果配置了曝光過濾,很容易把召回池中的item全部過濾掉,這裡我們可以不具體的過濾,只是對曝光過的物品打一個標籤,然後通過提降權把曝光過的物品排在最後,這裡我們以Hologres資料來源舉例,其他資料來源類似,具體的過濾和重排配置樣本如下:

{
    "FilterConfs": [
        {
            "Name": "holo_exposure_filter",
            "FilterType": "User2ItemExposureFilter",
            "MaxItems": 20,
            "TimeInterval": 172800,
            "WriteLog": true,
            "OnlyLogUserExposeFlag": true,
            "DaoConf": {
                "AdapterType": "hologres",
                "HologresName": "holo_info",
                "HologresTableName": "exposure_history"
            }
        }
    ],
    "SortConfs": [
        {
            "Name": "boost_score_sort",
            "SortType": "BoostScoreSort",
            "BoostScoreConditions": [
                {
                    "Conditions": [
                        {
                            "Name": "_is_exposure_",
                            "Domain": "item",
                            "Type": "int",
                            "Value": 1,
                            "Operator": "equal"
                        }
                    ],
                    "Expression": "score / 10"
                }
            ]
        }
    ]
}

User2Item自訂過濾(User2ItemCustomFilter)

Hologres

使用者需要提供一個自訂的 user2item 的過濾表,進行資料過濾。過濾表一般是離線產出,例如每天淩晨匯總每個使用者過去15天看過的物品(即有曝光行為的物品),按照逗號分隔放在一個欄位item_ids中。

{
    "FilterConfs": [
        {
            "Name": "u2i_custom_filter",
            "FilterType": "User2ItemCustomFilter",
            "DaoConf": {
                "AdapterType": "hologres",
                "HologresName": "holo_info",
                "HologresTableName": "u2i_custom_filter"
            },
            "ItemStateCacheSize": 10000,
            "ItemStateCacheTime": 3600
        }
    ]
}

DaoConf 配置說明

欄位

類型

是否必填

描述

AdapterType

string

資料來源的類型,固定值 hologres 。

HologresName

string

在資料來源配置(HologresConfs)中配置好的 hologres 的自訂名稱,如資料來源配置中的 holo_info。

HologresTableName

string

自訂曝光表名稱。

ItemStateCacheSize

int

開啟緩衝的情況下,可以緩衝的條目數量。如果此值大於0, 預設會開啟緩衝。

ItemStateCacheTime

int

開啟緩衝的情況下,緩衝條目的生命週期。預設值為 3600, 單位:秒。

表定義如下:

欄位

類別

類型

說明

樣本

user_id

主鍵

string

使用者唯一id

10944750

item_ids

屬性

string

item 唯一id 列表,多個以 ',' 分隔

17019277,17019278

TableStore(OTS)

使用者需要提供一個自訂的 user2item 的過濾表,進行資料過濾。

{
    "FilterConfs": [
        {
            "Name": "u2i_custom_filter",
            "FilterType": "User2ItemCustomFilter",
            "DaoConf": {
                "AdapterType": "tablestore",
                "TableStoreName": "tablestore_info",
                "TableStoreTableName": "u2i_table"
            }
        }
    ]
}

DaoConf 配置說明

欄位

類型

是否必填

描述

AdapterType

string

資料來源的類型,固定值 tablestore

TableStoreName

string

在資料來源配置(TableStoreConfs)中配置好的 tablestore 的自訂名稱,如資料來源配置中的 tablestore_info

TableStoreTableName

string

自訂曝光表名稱

表定義如下:

欄位

類別

類型

說明

樣本

user_id

主鍵

string

使用者唯一id

10944750

item_ids

屬性

string

item 唯一id 列表,多個以 ',' 分隔

17019277,17019278

PAI-FeatureStore

警告

僅支援線上資料來源為FeatureDB的FeatureView。

和上文提到的曝光過濾類似,也是提供一個即時的FeatureView, 通過 java sdk 或者 flink connector 寫入資料,資料寫入可以參考寫入特徵。PAI-Rec 引擎通過讀取這裡的資料進行過濾。FeatureView 的配置如下:

視圖名稱填寫為 user2item_custom_filter,寫入方式選擇自訂表格結構,預估數量級選擇千萬級以下

重點參數說明下:

  1. 類型選擇 即時 。

  2. 特徵實體 選擇 user 。

  3. 特徵欄位需要填入 user_id、item_id、timestamp。 其中 user_id 為主鍵, timestamp 勾選 事件時間。 主鍵和事件時間是必須選擇的。 資料類型上, user_id、item_id 都是 string , timestamp 是 int64 ,這裡timestamp代表時間戳記,單位毫秒。

  4. 特徵生命週期,這裡預設是 2 天。 需要按實際情況調整。這裡可以理解成某一行資料的TTL,是根據timestamp欄位傳入的數值計算生命週期的。

  5. 進階配置必填, 輸入 {"table_type":"bloom"},table_type 說明建立的是支援 bloom filter 的 table 。

    PAI-Rec 引擎中的配置參考如下:

  {
    "FilterConfs": [
        {
            "Name": "u2i_custom_filter",
            "FilterType": "User2ItemCustomFilter",
            "DaoConf": {
                "AdapterType": "featurestore",
                "FeatureStoreName": "fs_pairec",
                "FeatureStoreViewName": "u2icustom_filter"
            }
        }
    ]
}

DaoConf 配置說明

欄位

類型

是否必填

描述

AdapterType

string

資料來源的類型,固定值 featurestore 。

FeatureStoreName

string

在資料來源配置(FeatureStoreConfs)中配置好的 featurestore 的自訂名稱。

FeatureStoreViewName

string

自訂過濾表的featureview 名稱。

數量調整過濾(AdjustCountFilter)

簡單的調整數量,把召回數量隨機打散,然後保留需要控制的數量。

配置樣本:

{
    "FilterConfs": [
        {
            "Name": "adjust_count_filter",
            "FilterType": "AdjustCountFilter",
            "ShuffleItem": true,
            "RetainNum": 500
        }
    ]
}

欄位名

類型

是否必填

描述

ShuffleItem

string

是否打散

RetainNum

string

打散之後保留的數量

優先順序數量調整過濾(PriorityAdjustCountFilter)

優先順序過濾可以對各路召回進行數量控制,每路召回都會根據召回的 score 進行排序

配置樣本:

{
    "FilterConfs": [
        {
            "Name": "priority_adjust_count_filter",
            "FilterType": "PriorityAdjustCountFilter",
            "AdjustCountConfs": [
                {
                    "RecallName": "recall_1",
                    "Count": 125,
                    "Type": "accumulator"
                },
                {
                    "RecallName": "recall_2",
                    "Count": 250,
                    "Type": "accumulator"
                },
                {
                    "RecallName": "recall_3",
                    "Count": 400,
                    "Type": "accumulator"
                }
            ]
        }
    ]
}

欄位名

類型

是否必填

描述

Name

string

自訂的過濾名稱

FilterType

string

過濾類型,固定值 PriorityAdjustCountFilter

RecallName

string

召回源名稱

AdjustCountConfs

json array

優先順序配置

  • Count

int

此路召回限制的數量

  • Type

string

Type 限制類型,枚舉值:accumulator 或 fix。

accumulator 為累加限制。

  • 例如上述配置:recall_1 數量最多控制在 125, 當 recall_1 不足 125 時, recall_2 也會盡量補齊到 250 ,依次類推

fix 為固定限制:

  • 各路召回按照自己的 count 進行取數,某一路召回數量不足時,其他路召回不進行補足

狀態過濾(ItemStateFilter)

我們經常需要對召回的資料,進行狀態過濾。item 的狀態有可能會即時變動的,一般會有專有的表格儲存體 item 狀態。 此過濾流程需要即時擷取 item 狀態資訊,然後進行過濾。

Hologres

{
    "FilterConfs": [
        {
            "Name": "ItemStateFilter",
            "FilterType": "ItemStateFilter",
            "ItemStateDaoConf": {
                "AdapterType": "hologres",
                "HologresName": "",
                "HologresTableName": "",
                "ItemFieldName": "",
                "WhereClause": "",
                "SelectFields": ""
            },
            "ItemStateCacheSize": 50000,
            "ItemStateCacheTime": 3600,
            "FilterParams": [

            ]
        }
    ]
}

如果item的狀態不經常變化,可以配置緩衝選項。

欄位名

類型

是否必填

描述

ItemStateCacheSize

int

要緩衝數量。

ItemStateCacheTime

int

緩衝的時間,單位:秒。

ItemStateDaoConfig 定義如下:

欄位名

類型

是否必填

描述

AdapterType

string

資料來源的類型,枚舉值,如 hologres、mysql、tablestore 等

HologresName

string

在資料來源配置(HologresConfs)中配置好的 holo 的自訂名稱,如資料來源配置中的 holo_info

HologresTableName

string

holo 中 item 狀態表的表名

ItemFieldName

string

item 狀態表的主鍵

WhereClause

string

條件過濾語句

SelectFields

string

需要擷取的欄位

FilterParams 定義如下:

{
    "FilterParams": [
        {
            "Name": "publicStatus",
            "Type": "int",
            "Operator": "equal",
            "Value": 0
        },
        {
            "Name": "state",
            "Type": "int",
            "Operator": "equal",
            "Value": 1
        },
        {
            "Name": "checkStatus",
            "Type": "int",
            "Operator": "not_equal",
            "Value": 2
        },
        {
            "Name": "norec",
            "Type": "int",
            "Operator": "not_equal",
            "Value": 1
        }
    ]
}

欄位名

類型

是否必填

描述

Name

string

特徵名

Domain

string

特徵所屬,枚舉值:user/item,預設為item

Operator

string

操作符,支援 equal、not_equal、in, greater , greaterThan , less, lessThan

Type

string

特徵的類型

Value

object

條件值

重要

註:WhereClause 和 FilterParams都可以過濾,WhereClause 是在查詢的時候過濾,相當於 SQL 中的Where條件,FilterParams是在本地過濾。當通過FilterParams過濾時,滿足條件的會下發,不滿足條件的則過濾掉。

具體的 Operator 的使用,可以參考附錄

PAI-FeatureStore

{
    "FilterConfs": [
        {
            "Name": "ItemStateFilter",
            "FilterType": "ItemStateFilter",
            "ItemStateDaoConf": {
                "AdapterType": "featurestore",
                "FeatureStoreName": "",
                "FeatureStoreViewName": "",
                "ItemFieldName": "",
                "SelectFields": ""
            },
            "ItemStateCacheSize": 50000,
            "ItemStateCacheTime": 3600,
            "FilterParams": [

            ]
        }
    ]
}

如果item的狀態不經常變化,可以配置緩衝選項。

欄位名

類型

是否必填

描述

ItemStateCacheSize

int

要緩衝數量。

ItemStateCacheTime

int

緩衝的時間,單位:秒。

ItemStateDaoConfig 定義如下:

欄位名

類型

是否必填

描述

AdapterType

string

資料來源的類型,固定值 featurestore

FeatureStoreName

string

在資料來源配置(FeatuerStoreConfs)中配置好的 featurestore 的自訂名稱

FeatureStoreViewName

string

儲存item狀態的特徵視圖名稱

ItemFieldName

string

item 狀態表的主鍵

SelectFields

string

需要擷取的欄位

FilterParams 定義如下:

{
    "FilterParams": [
        {
            "Name": "publicStatus",
            "Type": "int",
            "Operator": "equal",
            "Value": 0
        },
        {
            "Name": "state",
            "Type": "int",
            "Operator": "equal",
            "Value": 1
        },
        {
            "Name": "checkStatus",
            "Type": "int",
            "Operator": "not_equal",
            "Value": 2
        },
        {
            "Name": "norec",
            "Type": "int",
            "Operator": "not_equal",
            "Value": 1
        }
    ]
}

欄位名

類型

是否必填

描述

Name

string

特徵名

Domain

string

特徵所屬,枚舉值:user/item

Operator

string

操作符,支援 equal、not_equal、in, greater , greaterThan , less, lessThan

Type

string

特徵的類型

Value

object

條件值

具體的 Operator 的使用,可以參考附錄

SnakeFilter

在召回階段可以通過多種召回通道擷取候選集。SnakeFilter提供一種根據權重比例,輪流的從各個召回通道拼接資料的方式。

比如召回有3個通道 A,B, C ,配置的權重比例為 1, 2, 3。那麼組裝邏輯為:

  • 先從A候選集中取1個元素

  • 從B候選集中擷取2個元素

  • 從C候選集中擷取3個元素

  • 迴圈擷取,直至找到滿足數量的元素

當遇到重複元素時,目前通過兩種策略來控制:

  • REFILL_ON_DUPLICATE 如果當前候選集裡擷取的元素和之前取的重複,會在當前候選集中繼續尋找,直至找到不重複的元素。比如C候選集中擷取的3個元素都和A,B裡重複了,會繼續在C候選集裡尋找。

  • SKIP_ON_DUPLICATE 直接跳過重複的元素,不補足元素,繼續下一輪迭代。比如C候選集中擷取的3個元素都和A,B裡重複了,那麼本輪C候選集返回的元素為空白,重新一輪迭代,繼續從A尋找。

{
    "FilterConfs": [
        {
            "Name": "SnakeFilter",
            "FilterType": "SnakeFilter",
            "RetainNum": 20,
            "SnakeType": "REFILL_ON_DUPLICATE",
            "AdjustCountConfs": [
                 {
                     "RecallName": "GroupHotRecall",
                     "Weight": 1
                 },
                 {
                     "RecallName": "U2IRecall",
                     "Weight": 2
                 },
                 {
                     "RecallName": "GlobalHotRecall",
                     "Weight": 3
                 }
             ]
        }
    ]
}

欄位名

類型

是否必填

描述

Name

string

自訂的過濾名稱。

FilterType

string

過濾類型,固定值SnakeFilter。

RetainNum

string

最終候選集的數量

SnakeType

string

  • REFILL_ON_DUPLICATE 繼續在當前召回通道尋找,直至滿足配額。預設值

  • SKIP_ON_DUPLICATE 跳過重複的元素,不補足配額,繼續下一輪迭代。

AdjustCountConfs

json array

  • RecallName

string

召回通道名稱

  • Weight

int

權重比例,在每輪擷取資料時,根據此值擷取相應的通道條目數量

說明

經過這個過濾後,item屬性中會增加snake_filter,說明來自於哪些召回鏈路,之前鏈路的位置順序(從0開始)及得分。舉例說明:snake_filter:GroupHotRecall:0:0.144200,說明來自於GroupHotRecall召回並且是第一個條目。

CompletelyFairFilter

根據召回條目的 score 進行排序,然後公平的從每一路召回擷取資料

{
    "FilterConfs": [
        {
            "Name": "CompletelyFairFilter",
            "FilterType": "CompletelyFairFilter",
            "RetainNum": 500
        }
    ]
}

DimensionFieldUniqueFilter

和 UniqueFilter 不同, 根據 item 的某個欄位進行去重過濾

{
    "FilterConfs": [
        {
            "Name": "DimensionFieldUniqueFilter",
            "FilterType": "DimensionFieldUniqueFilter",
            "Dimension": ""
        }
    ]
}

欄位名

類型

是否必填

描述

Name

string

自訂的過濾名稱。

FilterType

string

過濾類型,固定值DimensionFieldUniqueFilter。

Dimension

string

item屬性欄位,根據這個欄位進行去重。如果 item 的這個屬性欄位為空白,則會保留這個item 。

ConditionFilter

這個是組合 filter,可以根據請求條件(user 特徵, context 傳入的 features 等)來決定走哪個 filter。可以設定一組或者多組 condition 條件 + 具體 filter (在 FilterConfs 裡已經定義好了)的組合。

{
"FilterConfs":[
  {
      "Name": "adjust_count_filter",
      "FilterType": "AdjustCountFilter",
      "ShuffleItem": true,
      "RetainNum": 500
  },
  {
    "Name": "CompletelyFairFilter",
    "FilterType": "CompletelyFairFilter",
    "RetainNum": 500
  },
  {
    "Name": "ConditionFilter",
    "FilterType": "ConditionFilter",
    "ConditionFilterConfs":{
      "FilterConfs":[
        {
          "Conditions":[
            {
		"Name":     "query",
		"Type":     "string",
		"Domain":   "user",
		"Operator": "equal",
		"Value":    "1"
            }
          ],
          "FilterName":"adjust_count_filter"
        }
      ],
      "DefaultFilterName": "CompletelyFairFilter"
    }
  }
]
}

上文定義了兩個獨立的 filter:adjust_count_filter 和 CompletelyFairFilter。ConditionFilter 是組合條件的filter,當 user 屬性 query == "1" 時,走 adjust_count_filter 的邏輯。如果條件沒有匹配到,走 DefaultFilterName 指定的filter,在本情境中會走 CompletelyFairFilter。

具體配置含義如下:

欄位名

類型

是否必填

描述

Name

string

自訂的 filter 名稱

FilterType

string

filter 類型,固定值 ConditionFilter

ConditionFilterConfs

json map

具體條件配置

  • FilterConfs

json array

可以設定多個條件匹配規則,只要匹配到,就會走相應的 FilterName

  • DefaultFilterName

string

當 FilterConfs 裡的條件都沒有匹配到,會走這裡指定的 filter。

FilterConfs 具體配置含義:

欄位名

類型

是否必填

描述

Conditions

[]FilterParamConfig

匹配的規則條件

FilterName

string

當匹配條件後,走具體的 filter name,filter 需要在 FilterConfs 裡設定

FilterParamConfig 配置如下:

欄位名

類型

是否必填

描述

Name

string

item 或者 user 的特徵名

Domain

string

枚舉值,item/user。指的是 Name 選項屬於 item 特徵還是 user 特徵,Name 必須在 item 或 user 的 properties 裡找到。

Operator

string

枚舉值:equal/not_equal/in/not_in/greater/greaterThan/less/lessThan/contains/not_contains

Type

string

特徵的類型

Value

object

特徵的值

具體的條件設定,請參考下面的附錄

UniqueFilter

唯一性過濾,保證 item id 是唯一的,如果兩路召回包含同一個 item id,哪路召回先返回,取哪一路的 item id。

無需配置,可直接在 FilterNames 中引用。

如何使用

過濾配置和召回配置類似,配置好後,提供一個分情境使用的 FilterNames,FilterNames 是一個 Map[string]object 結構,其中 key 是情境,每個情境對應一組過濾策略

"FilterNames": {
  "${scene_name}": [
    "UniqueFilter"
  ]
}
  • ${scene_name} 為情境名,如果多個情境想使用同一個配置,則可以使用 "default"。

  • UniqueFilter: 此參數為在 FilterConfigs 中定義的過濾自訂名稱。

附錄

條件匹配 Operator 樣本

  1. equal (相等 ==)

{
    "Name": "publicStatus",
    "Type": "int",
    "Operator": "equal",
    "Value": 0
}
  1. not_equal (不等於 !=)

{
    "Name": "checkStatus",
    "Type": "int",
    "Operator": "not_equal",
    "Value": 2
}
  1. greater (大於 >)

{
    "Name": "checkStatus",
    "Type": "int",
    "Operator": "greater",
    "Value": 2
}
  1. greaterThan (大於等於 >=)

{
    "Name": "checkStatus",
    "Type": "int",
    "Operator": "greaterThan",
    "Value": 2
}
  1. less (小於 <)

{
    "Name": "checkStatus",
    "Type": "int",
    "Operator": "less",
    "Value": 2
}
  1. lessThan (小於等於 <=)

{
    "Name": "checkStatus",
    "Type": "int",
    "Operator": "lessThan",
    "Value": 2
}
  1. in (與數組中某個條目匹配)

{
    "Name": "state",
    "Type": "int",
    "Operator": "in",
    "Value": [2,4,6]
}

string 類型

{
    "Name": "state",
    "Type": "string",
    "Operator": "in",
    "Value": ["success","ok"]
}
  1. not_in (不與數組中任何條目匹配)

{
    "Name": "state",
    "Type": "int",
    "Operator": "not_in",
    "Value": [2,4,6]
}

也可以使用 string 類型,配置可以參考 in Operator。

  1. contains (包含數組中任意一個條目)

{
    "Name": "state",
    "Type": "[]int",
    "Operator": "contains",
    "Value": [2,4,6]
}

string 類型

{
    "Name": "state",
    "Type": "[]string",
    "Operator": "contains",
    "Value": ["success","ok"]
}
  1. not_contains (不包含數組中任意一個條目)

{
    "Name": "state",
    "Type": "[]int",
    "Operator": "not_contains",
    "Value": [2,4,6]
}

也可以使用 string 數群組類型,配置可以參考 contains Operator。

  1. bool (串連多個條件匹配 Operator)

使用 (or)的方式串連

{
    "Operator": "bool",
    "Type": "or",
    "Configs":
    [
        {
            "Name": "publicStatus",
            "Type": "int",
            "Operator": "equal",
            "Value": 0
        },
        {
            "Name": "checkStatus",
            "Type": "int",
            "Operator": "greater",
            "Value": 2
        }
    ]
}

使用 (and)的方式串連同理,只需要把 Type 設定為 and。

  1. expression (運算式結果為 true 時匹配)

{
    "Operator": "expression",
    "Value": "item.size == 43"
}

item.size 表示來自 item 的屬性,如果需要用到 user 屬性,可以用 user.xxx 表示。

運算式中支援使用各種算術運算子、比較子、邏輯運算子,三目運算子等,一個更加複雜的例子如下:

{
    "Operator": "expression",
    "Value": "!item.sold_out and user.list != nil ? item.size in user.list : true"
}

完整的運算式文法支援可以參考: https://expr-lang.org/docs/language-definition

說明

從引擎介面中features欄位傳入的上下文特徵,會存放在user側。如果上述配置中Value的值需要填變數而不是定值,可以通過介面傳入或者使用user已有的特徵,則Value可以填寫為:"Value":"user.xxx",代表和user側的某個屬性值做比較。例如和使用者的age屬性值做比較,則為:"Value":"user.age"