2013-04-30 70 views
3

有沒有辦法通過Mongoid使用MongoDB(v 2.4)的全文搜索功能?我試着從google group link得到答案,但不斷收到以下錯誤。使用Mongoid進行全文搜索

在一個標籤,我開始的mongod這樣:~$ mongod --setParameter textSearchEnabled=true 導致錯誤的代碼行: Article.mongo_session.command({:text => {:search => 'Ruby'}})

這將是巨大的,如果有人能指出一個方式在Ruby中執行MongoDB的runCommand,讓我可以直接運行命令db.collection.runCommand("text", { search: <string> })

failed with error 13111: "exception: wrong type for field (text) 3 != 2" 

See https://github.com/mongodb/mongo/blob/master/docs/errors.md 
for details about this error. 
+0

你可以發佈導致錯誤的行嗎? – MervS 2013-04-30 06:28:34

+0

請參閱更新後的帖子。 – brayne 2013-04-30 18:19:41

回答

2

我只是試着通過mongoid蒙戈2.4文本搜索和碰到了同樣的問題,因爲你。我玩了一點點,發現了以下解決方案。下面是我做了它的工作

例子:

User.mongo_session.command(text: 'users', search: 'search string') 

這裏「文」的關鍵應該指向集合名稱。

您將獲得Moped :: BSON ::具有關於找到文件信息的文件。 希望它能幫助你,以及

+0

這不適用於Mongoid 4。我得到以下錯誤:失敗,錯誤59:「沒有這樣的命令:'文本',錯誤的cmd:'{text:\」contacts \「,搜索:\」something \「}'」 – Donato 2016-07-27 15:29:51

0

對於我這樣一個工作。

這裏 Favorite.mongo_session.command(:文本=>「表名」,搜索:「搜索參數」)

確保:文字的價值是表名,而不是場。

11

對於任何人找到這個,Mongoid 4.0允許全文搜索這樣的:

Model.text_search('query') 

你可以連續使用,你通常會:

Listings.in(category: sections).where(listing_type: 'Website').text_search(params[:q]).to_a 

索引是很容易的,以及:

index({name: "text", description: "text"}, {weights: {name: 10, description: 2}, name: "TextIndex"}) 

編輯:2017年7月

正如以下所報告的,text_search已被棄用。我一直在使用where()像這樣:

Model.where('$ text'=> {'$ search'=>「\」#{params [:q]} \「」})

+1

text_search現已棄用 – mymlyn 2015-09-21 16:26:34

+0

@ mymlyn會很高興評論使用新的方法。 – 2016-07-16 22:38:21

+0

@mymlyn而不是告訴我們這不再是解決方案,告訴我們解決方案實際上是什麼。 – Donato 2016-07-27 15:32:32