運算子 $text 會在索引字串字段的內容上執行文字搜尋。 它可透過搜尋文字索引字段的指定單字或片語,來啟用全文搜索功能。 運算符 $text 在集合上至少需要一個文字索引,並提供字幹分析、停止文字移除和相關性評分等功能。
語法
{
$text: {
$search: <string>,
$language: <string>,
$caseSensitive: <boolean>,
$diacriticSensitive: <boolean>
}
}
參數
| 參數 | Description |
|---|---|
$search |
必須的。 包含要搜尋之字詞的搜尋字串。 除非以引弧括住片語比對,否則會將多個字詞視為 OR 作業。 |
$language |
選擇性。 文字搜尋的語言,決定詞幹規則和停止字組,不過如果您未指定索引的默認語言,則系統會使用索引的默認語言。 |
$caseSensitive |
選擇性。 布爾值旗標可啟用區分大小寫的搜尋。 默認值為 false(不區分大小寫)。 |
$diacriticSensitive |
選擇性。 布爾值旗標可啟用區分音調的搜尋。 默認值為 false (不區分音調字)。 |
先決條件
使用 $text 運算子之前,您必須在想要搜尋的欄位上建立文字索引。
範例
我們來了解 stores 資料集範例 json 的使用方式。
{
"_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
"name": "First Up Consultants | Bed and Bath Pantry - Port Antone",
"location": {
"lat": 87.2239,
"lon": -129.0506
},
"staff": {
"employeeCount": {
"fullTime": 8,
"partTime": 7
}
},
"sales": {
"salesByCategory": [
{ "categoryName": "Towel Sets", "totalSales": 520 },
{ "categoryName": "Bath Accessories", "totalSales": 41710 },
{ "categoryName": "Drapes", "totalSales": 42893 },
{ "categoryName": "Towel Racks", "totalSales": 30773 },
{ "categoryName": "Hybrid Mattresses", "totalSales": 39491 },
{ "categoryName": "Innerspring Mattresses", "totalSales": 6410 },
{ "categoryName": "Bed Frames", "totalSales": 41917 },
{ "categoryName": "Mattress Protectors", "totalSales": 44124 },
{ "categoryName": "Bath Towels", "totalSales": 5671 },
{ "categoryName": "Turkish Towels", "totalSales": 25674 }
],
"revenue": 279183
},
"promotionEvents": [
{
"eventName": "Discount Derby",
"promotionalDates": {
"startDate": { "Year": 2023, "Month": 12, "Day": 26 },
"endDate": { "Year": 2024, "Month": 1, "Day": 5 }
},
"discounts": [
{ "categoryName": "Microfiber Towels", "discountPercentage": 6 },
{ "categoryName": "Bath Sheets", "discountPercentage": 16 },
{ "categoryName": "Towels", "discountPercentage": 10 },
{ "categoryName": "Hand Towels", "discountPercentage": 11 },
{ "categoryName": "Kitchen Towels", "discountPercentage": 21 },
{ "categoryName": "Placemat", "discountPercentage": 11 },
{ "categoryName": "Bath Accessories", "discountPercentage": 11 },
{ "categoryName": "Bedspreads", "discountPercentage": 21 },
{ "categoryName": "Shower Curtains", "discountPercentage": 24 },
{ "categoryName": "Pillow Top Mattresses", "discountPercentage": 10 }
]
},
{
"eventName": "Big Bargain Blitz",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 3, "Day": 25 },
"endDate": { "Year": 2024, "Month": 4, "Day": 3 }
},
"discounts": [
{ "categoryName": "Mattress Toppers", "discountPercentage": 24 },
{ "categoryName": "Pillow Cases", "discountPercentage": 14 },
{ "categoryName": "Soap Dispensers", "discountPercentage": 20 },
{ "categoryName": "Beach Towels", "discountPercentage": 18 },
{ "categoryName": "Bath Mats", "discountPercentage": 22 },
{ "categoryName": "Blankets", "discountPercentage": 12 },
{ "categoryName": "Kitchen Towels", "discountPercentage": 8 },
{ "categoryName": "Memory Foam Mattresses", "discountPercentage": 14 },
{ "categoryName": "Placemat", "discountPercentage": 17 },
{ "categoryName": "Bed Frames", "discountPercentage": 23 }
]
},
{
"eventName": "Massive Markdown Mania",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 6, "Day": 23 },
"endDate": { "Year": 2024, "Month": 6, "Day": 30 }
},
"discounts": [
{ "categoryName": "Bed Skirts", "discountPercentage": 17 },
{ "categoryName": "Shower Curtains", "discountPercentage": 23 },
{ "categoryName": "Bath Towels", "discountPercentage": 21 },
{ "categoryName": "Memory Foam Mattresses", "discountPercentage": 11 },
{ "categoryName": "Bathrobes", "discountPercentage": 19 },
{ "categoryName": "Bath Accessories", "discountPercentage": 5 },
{ "categoryName": "Box Springs", "discountPercentage": 21 },
{ "categoryName": "Hand Towels", "discountPercentage": 13 },
{ "categoryName": "Tablecloths", "discountPercentage": 19 },
{ "categoryName": "Duvet Covers", "discountPercentage": 23 }
]
},
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": { "Year": 2024, "Month": 9, "Day": 21 },
"endDate": { "Year": 2024, "Month": 9, "Day": 30 }
},
"discounts": [
{ "categoryName": "Adjustable Beds", "discountPercentage": 19 },
{ "categoryName": "Mattress Toppers", "discountPercentage": 23 },
{ "categoryName": "Washcloths", "discountPercentage": 7 },
{ "categoryName": "Comforters", "discountPercentage": 24 },
{ "categoryName": "Kitchen Towels", "discountPercentage": 7 },
{ "categoryName": "Pillows", "discountPercentage": 13 },
{ "categoryName": "Bath Sheets", "discountPercentage": 25 },
{ "categoryName": "Napkins", "discountPercentage": 25 },
{ "categoryName": "Bath Towels", "discountPercentage": 15 },
{ "categoryName": "Beach Towels", "discountPercentage": 15 }
]
}
],
"company": "First Up Consultants",
"city": "Port Antone",
"storeOpeningDate": { "$date": "2024-09-19T17:31:59.665Z" },
"lastUpdated": { "$timestamp": { "t": 1729359119, "i": 1 } }
}
範例 1:簡單文字搜尋
此查詢會擷取索引文字欄位中包含單字「Microphone」的存放區。
// First create a text index
db.stores.createIndex({ "name": "text", "sales.salesByCategory.categoryName": "text" })
// Then perform the search
db.stores.find(
{ $text: { $search: "Microphone" }},
{ _id: 1, name: 1, "sales.salesByCategory.categoryName": 1 }).limit(2)
此查詢傳回的前兩個結果是:
[
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"sales": {
"salesByCategory": [
{ "categoryName": "Lavalier Microphones" },
{ "categoryName": "Wireless Microphones" }
]
}
},
{
"_id": "7cecdb2d-33c2-434c-ad55-bf529f68044b",
"name": "Contoso, Ltd. | Microphone Haven - O'Connellside",
"sales": {
"salesByCategory": [
{ "categoryName": "Microphone Accessories" },
{ "categoryName": "Wireless Microphones" }
]
}
}
]
範例 2:多個字詞搜尋
查詢會擷取與「Home Decor」相關的商店 (預設會被視為 OR 多個字詞)。
// First create a text index
db.stores.createIndex({ "name": "text", "sales.salesByCategory.categoryName": "text" })
// Then perform the search
db.stores.find(
{ $text: { $search: "Home Decor" }},
{ _id: 1, name: 1, "sales.salesByCategory.categoryName": 1 }).limit(5)
此查詢傳回的前五個結果如下:
[
{
"_id": "905d1939-e03a-413e-a9c4-221f74055aac",
"name": "Trey Research | Home Office Depot - Lake Freeda",
"sales": {
"salesByCategory": [
{ "categoryName": "Desk Lamps" }
]
}
},
{
"_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
"name": "Lakeshore Retail | Home Decor Hub - Franciscoton",
"sales": {
"salesByCategory": [
{ "categoryName": "Lamps" },
{ "categoryName": "Rugs" }
]
}
},
{
"_id": "1a2c387b-bb43-4b14-a6cd-cc05a5dbfbd5",
"name": "Contoso, Ltd. | Smart Home Device Vault - Port Katarina",
"sales": {
"salesByCategory": [
{ "categoryName": "Smart Locks" },
{ "categoryName": "Smart Home Hubs" }
]
}
},
{
"_id": "15e9ca57-ebc1-4191-81c2-5dc2f4ebd973",
"name": "Trey Research | Gardening Supply Stop - Port Saul",
"sales": {
"salesByCategory": [
{ "categoryName": "Garden Decor" },
{ "categoryName": "Pruning Shears" }
]
}
},
{
"_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
"name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
"sales": {
"salesByCategory": [
{ "categoryName": "Lumber" },
{ "categoryName": "Windows" }
]
}
}
]
範例 3:片語搜尋
此查詢使用引號搜尋確切的短語「家庭劇院」。
db.stores.find(
{ $text: { $search: "\"Home Theater\"" }},
{ _id: 1, name: 1, "sales.salesByCategory.categoryName": 1 }).limit(2)
此查詢傳回的前兩個結果是:
[
{
"_id": "0bc4f653-e64e-4342-ae7f-9611dfd37800",
"name": "Tailwind Traders | Speaker Bazaar - North Mireyamouth",
"sales": {
"salesByCategory": [
{ "categoryName": "Home Theater Speakers" }
]
}
},
{
"_id": "28bb05ed-d516-4186-9144-b9eeee30917a",
"name": "Adatum Corporation | Home Entertainment Market - East Bennettville",
"sales": {
"salesByCategory": [
{ "categoryName": "Media Players" },
{ "categoryName": "Home Theater Projectors" },
{ "categoryName": "Projector Accessories" },
{ "categoryName": "Sound Bars" },
{ "categoryName": "Blu-ray Players" }
]
}
}
]
範例 4:排除具有否定的字詞
此查詢會搜尋具有「音訊」的商店,但排除具有「無線」的商店。
db.stores.find(
{ $text: { $search: "Audio -Wireless" }},
{ _id: 1, name: 1, "sales.salesByCategory.categoryName": 1 }).limit(2)
此查詢傳回的前兩個結果是:
[
{
"_id": "32afe6ec-dd3c-46b3-a681-ed041b032c39",
"name": "Relecloud | Audio Equipment Gallery - Margretshire",
"sales": {
"salesByCategory": [
{ "categoryName": "Audio Receivers" },
{ "categoryName": "Portable Bluetooth Speakers" }
]
}
},
{
"_id": "a3d3e59f-54bd-44be-943c-50dca5c4d667",
"name": "Contoso, Ltd. | Audio Equipment Shop - West Darrion",
"sales": {
"salesByCategory": [
{ "categoryName": "Soundbars" }
]
}
}
]
範例 5:區分大小寫的搜尋
備註
區分大小寫的支援正在管線中,應該很快就會發行。
此範例允許對 「BAZAAR」 執行區分大小寫的搜尋。
db.stores.find(
{ $text: { $search: "BAZAAR", $caseSensitive: true } },
{ _id: 1, name: 1, "sales.salesByCategory.categoryName": 1 }
).limit(2)
查詢會比對 「BAZAAR」 出現在該案例中的檔。
[
{
"_id": "future-electronics-001",
"name": "Future Electronics Hub",
"sales": { "totalSales": 25000 }
},
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"sales": {
"salesByCategory": [
{ "categoryName": "Sound Bars" },
{ "categoryName": "Game Controllers" },
{ "categoryName": "Remote Controls" },
{ "categoryName": "VR Games" }
],
"totalSales": 160000
}
}
]
範例 6:取得文字搜尋分數
此查詢會擷取具有排名相關性分數的文字搜尋結果。
db.stores.find(
{ $text: { $search: "Hub" } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } }).limit(5)
此查詢傳回的前五個結果如下:
[
{ "_id": "511c9932-d647-48dd-9bd8-baf47b593f88", "score": 2 },
{ "_id": "a0a2f05c-6085-4c99-9781-689af759662f", "score": 2 },
{ "_id": "fb5aa470-557c-43cb-8ca0-5915d6cae34b", "score": 2 },
{ "_id": "1a2c387b-bb43-4b14-a6cd-cc05a5dbfbd5", "score": 1 },
{ "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", "score": 1 }
]
範例 7:跨多個類別搜尋
首先,建立一個全面的文字索引來搜尋所有文字欄位。
// Create comprehensive text index
db.stores.createIndex({
name: "text",
"sales.salesByCategory.categoryName": "text",
"promotionEvents.eventName": "text",
"promotionEvents.discounts.categoryName": "text"
})
// Search across all indexed fields
db.stores.find(
{ $text: { $search: "\"Home Theater\"" }},
{ name: 1, "sales.salesByCategory.categoryName": 1, "promotionEvents.eventName": 1, "promotionEvents.discounts.categoryName": 1}).limit(2)
此查詢傳回的前兩個結果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"sales": {
"salesByCategory": [
{ "categoryName": "Sound Bars" },
{ "categoryName": "Game Controllers" },
{ "categoryName": "Remote Controls" },
{ "categoryName": "VR Games" }
]
},
"promotionEvents": [
{
"eventName": "Massive Markdown Mania",
"discounts": [
{ "categoryName": "DVD Players" },
{ "categoryName": "Projector Lamps" },
{ "categoryName": "Media Players" },
{ "categoryName": "Blu-ray Players" },
{ "categoryName": "Home Theater Systems" },
{ "categoryName": "Televisions" }
]
},
{
"eventName": "Fantastic Deal Days",
"discounts": [
{ "categoryName": "TV Mounts" },
{ "categoryName": "Game Accessories" },
{ "categoryName": "Portable Projectors" },
{ "categoryName": "Projector Screens" },
{ "categoryName": "Blu-ray Players" },
{ "categoryName": "DVD Players" }
]
},
{
"eventName": "Discount Delight Days",
"discounts": [
{ "categoryName": "Game Controllers" },
{ "categoryName": "Home Theater Projectors" },
{ "categoryName": "Sound Bars" },
{ "categoryName": "Media Players" },
{ "categoryName": "Televisions" },
{ "categoryName": "Projector Lamps" }
]
},
{
"eventName": "Super Sale Spectacular",
"discounts": [
{ "categoryName": "Laser Projectors" },
{ "categoryName": "Projector Screens" },
{ "categoryName": "PC Games" },
{ "categoryName": "PlayStation Games" },
{ "categoryName": "TV Mounts" },
{ "categoryName": "Mobile Games" }
]
},
{
"eventName": "Grand Deal Days",
"discounts": [
{ "categoryName": "Remote Controls" },
{ "categoryName": "Televisions" },
{ "categoryName": "Business Projectors" },
{ "categoryName": "Laser Projectors" },
{ "categoryName": "Projectors" },
{ "categoryName": "Projector Screens" }
]
},
{
"eventName": "Major Bargain Bash",
"discounts": [
{ "categoryName": "Sound Bars" },
{ "categoryName": "VR Games" },
{ "categoryName": "Xbox Games" },
{ "categoryName": "Projector Accessories" },
{ "categoryName": "Mobile Games" },
{ "categoryName": "Projector Cases" }
]
}
]
},
{
"_id": "0bc4f653-e64e-4342-ae7f-9611dfd37800",
"name": "Tailwind Traders | Speaker Bazaar - North Mireyamouth",
"sales": {
"salesByCategory": [
{ "categoryName": "Home Theater Speakers" }
]
},
"promotionEvents": [
{
"eventName": "Epic Bargain Bash",
"discounts": [
{ "categoryName": "Bluetooth Speakers" },
{ "categoryName": "Outdoor Speakers" }
]
},
{
"eventName": "Fantastic Deal Days",
"discounts": [
{ "categoryName": "Portable Speakers" },
{ "categoryName": "Home Theater Speakers" }
]
}
]
}
]