BM25 是 Azure DocumentDB 全文搜尋的核心相關性排序演算法。 本文說明如何建立涵蓋單一文字欄位的搜尋索引,並對該索引執行依 BM25 評分的 $search + text 查詢。 有關預覽版全文搜尋與正式推出的文字索引之間的差異,請參閱 「全文搜尋與文字索引」。
Note
全文搜尋目前處於預覽階段。 在使用這些範例之前,請先為您的叢集啟用全文搜尋。
什麼是 BM25?
BM25(Best Match 25)會根據每份文件中的詞項與查詢的匹配程度為文件排序,並考量三項因素:
- 詞項頻率。 更常提及查詢詞的文件,得分會更高,但增幅會遞減,因此單一關鍵字即使重複出現,也無法主導結果。
- 反向文件頻率。 語料庫中的罕見詞比常用詞更有分量,因此匹配
bracket比匹配the更有價值。 - 文件長度正規化。 長篇文件不會因為重複詞語而受到不公平的懲罰,短文件也不會因此獲得不公平的獎勵。
演算法經過深入研究,預設值合理,且 Azure DocumentDB 回傳的分數是單調的(越高代表單一查詢中相關性越高)。 分數無法在不同查詢間直接比較。
建立關鍵字搜尋索引
// ❌ Community MongoDB text-index shape. Not the Azure DocumentDB FTS path.
// The new $search engine does not consume this form.
db.products.createIndex({ title: "text" });
// ✅ Define a search index with createSearchIndexes.
// Always set dynamic: false and enumerate fields explicitly.
db.runCommand({
createSearchIndexes: "products",
indexes: [
{
name: "idx_title_standard",
definition: {
mappings: {
dynamic: false,
fields: {
title: { type: "string" }
}
}
}
}
]
});
索引名稱應以欄位和意圖命名,方便從 $search中引用。 索引是非同步建立的。 在對其發出$listSearchIndexes查詢前,請先確認下方所示的$search階段已準備好。
列出與檢視搜尋索引
搜尋索引不會出現在 db.collection.getIndexes() 的輸出中,因為它只會列出一般索引。 利用 $listSearchIndexes 聚合階段檢查它們:
// ✅ List all search indexes on the collection.
db.products.aggregate([
{ $listSearchIndexes: {} }
]);
// Inspect a specific search index by name.
db.products.aggregate([
{ $listSearchIndexes: { name: "idx_title_standard" } }
]);
每個結果包含索引 name、其 latestDefinition、 status、以及 queryable 狀態。 請等待索引回報 status: "READY" 和 queryable: true 後,再發出 $search 查詢。
執行 $search + text 查詢
// ❌ Regex substring search on a large collection: COLLSCAN, unranked,
// case-sensitive without the /i flag.
db.products.find({ title: { $regex: "bracket", $options: "i" } });
// ✅ BM25 keyword search with $search + text.
// Always pass index: "<name>" and cap with a downstream $limit.
db.products.aggregate([
{
$search: {
index: "idx_title_standard",
text: {
query: "bracket",
path: "title"
}
}
},
{ $limit: 20 },
{
$project: {
_id: 0,
title: 1,
score: { $meta: "searchScore" }
}
}
]);
每項 $search + text 查詢都適用三項規則:
-
$search是聚合管線的第一階段。 -
index: "<name>"被明確設定,讓引擎知道該使用哪個搜尋索引。 - 結果集會在下游
{ $limit: N }階段受到限制。 在limit內沒有$search或count欄位。
text運算子接受單一非空query字串。 設定 matchCriteria: "any" 為匹配任何已分析的查詢詞,或 "all" 要求所有已分析的查詢詞。
path 可以是欄位名稱、具名 multi 分析器映射,或是任一形式的明確陣列。 所有路徑都必須存在於所選索引中。
如需完整的多欄位範例,請參閱 結構化與文字搜尋。 若要設定大小寫正規化、前綴匹配或語言專屬處理,請參閱 自訂分析器。
分數操作
每份由 $search 傳回的文件都會附有 BM25 分數,你可透過 { $meta: "searchScore" } 讀取。 只要排名很重要,就使用 Project it:
db.products.aggregate([
{ $search: { index: "idx_title_standard", text: { query: "bracket", path: "title" } } },
{ $limit: 50 },
{ $project: { _id: 1, title: 1, score: { $meta: "searchScore" } } },
{ $match: { score: { $gte: 1.0 } } }
]);
$search 預設會依 BM25 由高到低排序傳回結果,因此除非下游階段擾亂了該順序,否則不需要明確的排序 $sort 。 使用最低分 $match 來剔除低相關性的命中,並使用固定的 $limit 來限制有效載荷大小。 若要依照在 $search 內對應的純量欄位排序,請參閱「排序與限制搜尋結果」。
與篩選器結合
若要將文字匹配與內頁 $search精確的值或範圍結合,請將結構化欄位映射到相同的搜尋索引中,並使用 compound。 完整索引與查詢請參見 「結構化與文字搜尋」。
您也可以套用下游篩選器 $match,包括在欄位未對應至搜尋索引中時。 請在最終 $limit 前套用篩選器,避免符合資格的相符項目被較早的候選項截斷:
db.products.aggregate([
{ $search: { index: "idx_title_standard", text: { query: "bracket", path: "title" } } },
{ $match: { inStock: true, price: { $lte: 100 } } },
{ $limit: 20 },
{ $project: { _id: 0, title: 1, price: 1, score: { $meta: "searchScore" } } }
]);