標籤:res size 檢索 odi doc find mongo div coff
- //插入測試資料 有列name和description
- > db.stores.insert(
- ... [
- ... { _id: 1, name: "Java Hut", description: "Coffee and cakes" },
- ... { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
- ... { _id: 3, name: "Coffee Shop", description: "Just coffee" },
- ... { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
- ... { _id: 5, name: "Java Shopping", description: "Indonesian goods" }
- ... ]
- ... )
- BulkWriteResult({
- "writeErrors" : [ ],
- "writeConcernErrors" : [ ],
- "nInserted" : 5,
- "nUpserted" : 0,
- "nMatched" : 0,
- "nModified" : 0,
- "nRemoved" : 0,
- "upserted" : [ ]
- })
- //在stores上建立所以 包含name列和description都是文本
- > db.stores.createIndex( { name: "text", description: "text" } )
- {
- "createdCollectionAutomatically" : false,
- "numIndexesBefore" : 1,
- "numIndexesAfter" : 2,
- "ok" : 1
- }
- //執行全文檢索索引 會將關鍵字分詞 然後匹配結果還可以 由於資料量小 速度就測不出來了
- > db.stores.find( { $text: { $search: "java coffee shop" } } )
- { "_id" : 3, "name" : "Coffee Shop", "description" : "Just coffee" }
- { "_id" : 1, "name" : "Java Hut", "description" : "Coffee and cakes" }
- { "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods" }
優勢:即時的全文檢索索引。
不知道效能如何,不支援高亮這種展示,只有在3.2+的版本才支援中文分詞。
大致瞭解下,暫時不會用到,以後用到可以詳細看手冊:
https://docs.mongodb.com/manual/text-search/
Mongodb全文檢索索引