有時候需要對字段里面的字符串進行索引,比如查找評論,搜索引擎等需求。
- 全文搜索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
$textexpression. - The
$textquery can not appear in$norexpressions. - To use a
$textquery in an$orexpression, all clauses in the$orarray must be indexed. - You cannot use
hint()if the query includes a$textquery expression. - You cannot specify
$naturalsort order if the query includes a$textexpression. - You cannot combine the
$textexpression, which requires a special text index, with a query operator that requires a different type of special index. For example you cannot combine$textexpression with the$nearoperator. - Views do not support text search.
翻譯:
- 一個查詢只能指定最多一個$text表達式。
- $text表達式不能跟$nor一起使用。
- $text表達式跟$or一起使用的情況下,數(shù)組必須包含全文索引。
- 不能使用hint()強制指定使用全文搜索。
- 在$text表達式中不能使用{$natural:true}進行排序。
- 不能將$text表達式與可以允許不同類型的索引進行組合,例如不能將$text表達式跟$near操作符組合。
- View不支持全文索引。