Mongodb全文檢索索引

來源:互聯網
上載者:User

標籤:res   size   檢索   odi   doc   find   mongo   div   coff   

  1. //插入測試資料 有列name和description
  2. > db.stores.insert(
  3. ... [
  4. ... { _id: 1, name: "Java Hut", description: "Coffee and cakes" },
  5. ... { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
  6. ... { _id: 3, name: "Coffee Shop", description: "Just coffee" },
  7. ... { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
  8. ... { _id: 5, name: "Java Shopping", description: "Indonesian goods" }
  9. ... ]
  10. ... )
  11. BulkWriteResult({
  12.    "writeErrors" : [ ],
  13.    "writeConcernErrors" : [ ],
  14.    "nInserted" : 5,
  15.    "nUpserted" : 0,
  16.    "nMatched" : 0,
  17.    "nModified" : 0,
  18.    "nRemoved" : 0,
  19.    "upserted" : [ ]
  20. })
  21. //在stores上建立所以 包含name列和description都是文本
  22. > db.stores.createIndex( { name: "text", description: "text" } )
  23. {
  24.    "createdCollectionAutomatically" : false,
  25.    "numIndexesBefore" : 1,
  26.    "numIndexesAfter" : 2,
  27.    "ok" : 1
  28. }
  29. //執行全文檢索索引 會將關鍵字分詞 然後匹配結果還可以 由於資料量小 速度就測不出來了
  30. > db.stores.find( { $text: { $search: "java coffee shop" } } )
  31. { "_id" : 3, "name" : "Coffee Shop", "description" : "Just coffee" }
  32. { "_id" : 1, "name" : "Java Hut", "description" : "Coffee and cakes" }
  33. { "_id" : 5, "name" : "Java Shopping", "description" : "Indonesian goods" }

優勢:即時的全文檢索索引。

不知道效能如何,不支援高亮這種展示,只有在3.2+的版本才支援中文分詞。

大致瞭解下,暫時不會用到,以後用到可以詳細看手冊:

https://docs.mongodb.com/manual/text-search/

Mongodb全文檢索索引

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.