MongoDB全文索引。

有時候需要對字段里面的字符串進行索引,比如查找評論,搜索引擎等需求。

  • 全文搜索2.4以后默認開啟,還在用以前版本的請自行百度Google開啟全文搜索。
  • 全文索引的建立比較慢,實操中需要等服務器閑時或者離線進行(否則會阻塞讀寫,當然也可以放在后臺運行)。

使用

  • 現(xiàn)在tesla集合有幾條歌詞文檔數(shù)據(jù)如下:
{ "_id" : ObjectId("5a5b56b80f12feec77a93a86"), "song" : "1. Hotel California", "lyrics" : "On a dark desert highway, cool wind in my hair. Warm smell of colitas, rising up through the air.Chan" }
{ "_id" : ObjectId("5a5b56c00f12feec77a93a87"), "song" : "1. Hotel California", "lyrics" : "On a dark desert highway, cool wind in my hair. Warm smell of colitas, rising up through the air." }
{ "_id" : ObjectId("5a5b57200f12feec77a93a88"), "song" : "3. Hotel California", "lyrics" : "Such a lovely place, Such a lovely face." }
{ "_id" : ObjectId("5a5b57200f12feec77a93a89"), "song" : "4. Hotel California", "lyrics" : "Some dance to remember, some dance to forget." }
{ "_id" : ObjectId("5a5b57200f12feec77a93a8a"), "song" : "5. Hotel California", "lyrics" : "Welcome to the Hotel California" }
{ "_id" : ObjectId("5a5b57200f12feec77a93a8b"), "song" : "hell world", "lyrics" : "Welcome to beijing" }
{ "_id" : ObjectId("5a5b79090f12feec77a93a8c"), "song" : "6. Hotel California", "lyrics" : "ssl certificate" }
{ "_id" : ObjectId("5a5b7c080f12feec77a93a8d"), "song" : "7. Hotel California", "lyrics" : "pre-market lovely" }

添加全文索引:

db.tesla.ensureIndex({song:"text",lyrics:"text"},{weights:{song:1,lycris:2}})

{song:"text",lyrics:"text"}:給"song","lyrics"字段添加全文索引。
{weights:{song:1,lycris:2}: 權重分配,"lycris" 的權重為2,"song"的權重為1.(權重越大優(yōu)先級越高,權重的區(qū)間為1-1000,000,000)
一旦分配錯了權重,只能刪除索引重新建立,建立權重請務必小心

  • 搜索含有"lovely"的文檔:
db.tesla.find({$text:{$search:"lovely"}})
{ "_id" : ObjectId("5a5b57200f128"), "song" : "3. Hotel California", "lyrics" : "Such a lovely place, Such a lovely face." }

(只有一條)

  • 搜索含有"dark"的文檔:
db.tesla.find({$text:{$search:"dark"}})
{ "_id" : ObjectId("5a5b56b80f126"), "song" : "1. Hotel California", "lyrics" : "On a dark desert highway, cool wind in my hair. Warm smell of colitas, rising up through the air. Chan" }
{ "_id" : ObjectId("5a5b56c00f167"), "song" : "1. Hotel California", "lyrics" : "On a dark desert highway, cool wind in my hair. Warm smell of colitas, rising up through the air." }
  • 或搜索,搜索包含"lovely"或包含"remember"的文檔:
db.tesla.find({$text:{$search:"lovely remember"}})
{ "_id" : ObjectId("5a5b57200f12feec77a93a88"), "song" : "3. Hotel California", "lyrics" : "Such a lovely place, Such a lovely face." }
{ "_id" : ObjectId("5a5b57200f12feec77a93a89"), "song" : "4. Hotel California", "lyrics" : "Some dance to remember, some dance to forget." }

(中間使用空格隔開)

  • 與搜索,搜索包含"dark"和"Chan"的文檔:
db.tesla.find({$text:{$search:"\"dark\"\"chan\""}})
{ "_id" : ObjectId("5a5b56b80f12feec77a93a86"), "song" : "1. Hotel California", "lyrics" : "On a dark desert highway, cool wind in my hair. Warm smell of colitas, rising up through the air.Chan" }

(使用\"把關鍵字包起來)

  • 排除關鍵字,搜索包含"dark"但是不包含"Chan"的文檔:
db.tesla.find({$text:{$search:"dark -chan"}})
{ "_id" : ObjectId("5a5b56c00f12feec77a93a87"), "song" : "1. Hotel California", "lyrics" : "On a dark desert highway, cool wind in my hair. Warm smell of colitas, rising up through the air." }

(使用-將排除的關鍵字標識出來)

  • 排除關鍵字短語,例如pre-market,搜索含有"lovely"但是不包含"pre-market"的文檔:
db.tesla.find({$text:{$search:"lovely -pre -market"}})
{ "_id" : ObjectId("5a5b57200f12feec77a93a88"), "song" : "3. Hotel California", "lyrics" : "Such a lovely place, Such a lovely face." }

(有短語的需要在短語之間進行拆分,例子中的pre-market拆成 pre -market,否則mongoDB會把pre-market的減號當成分隔符)

  • A query can specify, at most, one $text expression.
  • The $text query can not appear in $nor expressions.
  • To use a $text query in an $or expression, all clauses in the $or array must be indexed.
  • You cannot use hint() if the query includes a $text query expression.
  • You cannot specify $natural sort order if the query includes a $text expression.
  • You cannot combine the $text expression, which requires a special text index, with a query operator that requires a different type of special index. For example you cannot combine $text expression with the $near operator.
  • Views do not support text search.

翻譯:

  • 一個查詢只能指定最多一個$text表達式。
  • $text表達式不能跟$nor一起使用。
  • $text表達式跟$or一起使用的情況下,數(shù)組必須包含全文索引。
  • 不能使用hint()強制指定使用全文搜索。
  • 在$text表達式中不能使用{$natural:true}進行排序。
  • 不能將$text表達式與可以允許不同類型的索引進行組合,例如不能將$text表達式跟$near操作符組合。
  • View不支持全文索引。
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 創(chuàng)建全文索引 - 全文索引 ``` > use imooc switched to db imooc > db.i...
    威研威語閱讀 343評論 0 0
  • Solr&ElasticSearch原理及應用 一、綜述 搜索 http://baike.baidu.com/it...
    樓外樓V閱讀 7,648評論 1 17
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,599評論 19 139
  • 獻給三十年教師的榮譽證章 何水長 三十年彈指一揮間。一萬多個日日夜夜,點燈熬油翻書掲片,查閱資料,撰寫教案,批改作...
    水天滄浪閱讀 786評論 1 0
  • 在我剛工作時,我的上司是個情商非常高的年輕女生,她常常跟我強調(diào):要注意你的工作情商。 記得有項工作任務,我有我的看...
    簡淺Jian閱讀 959評論 0 2

友情鏈接更多精彩內(nèi)容