【问题标题】:Full text search using Mongoid使用 Mongoid 进行全文搜索
【发布时间】:2013-04-23 21:24:21
【问题描述】:

有没有办法通过 Mongoid 使用 MongoDB (v 2.4) 的全文搜索功能?我尝试了google group link 的答案,但不断收到以下错误。

在一个标签中,我这样启动了 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.

【问题讨论】:

  • 你能发布导致错误的行吗?
  • 请查看更新后的帖子。

标签: ruby mongodb mongoid


【解决方案1】:

对于任何发现此内容的人,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]}\"" })

【讨论】:

  • text_search 现已弃用
  • @mymlyn 会很高兴评论新的使用方法。
  • @mymlyn 与其告诉我们这不再是解决方案,不如告诉我们解决方案实际上是什么。
【解决方案2】:

我刚刚通过 mongoid 尝试了 mongo 2.4 文本搜索,并遇到了与您相同的问题。我玩了一点,找到了以下解决方案。这是我的工作方式

例子:

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

这里的'text'键应该指向集合名称。

您将获得 Moped::BSON::Document,其中包含有关找到的文档的信息。 希望对你也有帮助

【讨论】:

  • 这不适用于 Mongoid 4。我收到以下错误:失败并出现错误 59:“没有这样的命令:'text',错误的 cmd:'{ text:\"contacts\",搜索: \"某事\" }'"
【解决方案3】:

基于 Durran Jordan 对“使用 Moped 进行 Mongo 2.4 全文搜索?”的问题的回答。在此链接中:https://groups.google.com/forum/?fromgroups=#!topic/mongoid/hJRbaNMy6w4,我建议您将命令更改为:

 Article.mongo_session.command({:text => {:search => 'Ruby'}})

对此:

 Article.with(database: "text").mongo_session.command({:text => {:search => 'Ruby'}})

【讨论】:

    【解决方案4】:

    对我来说,这个很有效。

    这里 Favorite.mongo_session.command(:text =>'table_name', search:'搜索参数')

    确保 :text 的值是表名而不是字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 2023-03-02
      • 2010-11-11
      • 2010-11-25
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多