【问题标题】:mongoid create text index on all text fields on modelmongoid 在模型上的所有文本字段上创建文本索引
【发布时间】:2017-06-14 23:58:58
【问题描述】:

我能否像使用这个 mongodb 命令一样为 ruby​​ 模型中的所有字段创建文本索引: db.documents.createIndex({ "$**": "text" }, { name: "TextIndex" })

我们也可以以某种方式将数字属性添加到 index.html 中。我试过这样,但它没有

def fulltext_index
    attributes.except(:_rev, :_type, :doc_type).values.map{|e| e.class==String ? e.to_s : ""}.join(" ").strip + " " + sequence.to_s
end

index({ fulltext_index: "text"})

【问题讨论】:

    标签: ruby-on-rails mongodb elasticsearch mongoid mongoid5


    【解决方案1】:

    您可以创建您需要的索引作为以下示例中的第二行:

    client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music') 
    client[:bands].indexes.create_one( { "$**": "text" } )
    

    查看此链接以获取更多信息: https://github.com/mongodb/mongo-ruby-driver/blob/master/docs/tutorials/ruby-driver-indexing.txt

    【讨论】:

      【解决方案2】:

      在我的情况下,我需要打开一个 Rails 控制台并运行它:

      对于名为 Model 的模型

      client = Mongoid.default_client[Model.collection_name]
      client.indexes.create_one( { "$**" => "text" } )
      

      对于其他自定义 idex,请查看 answer

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-15
        • 2015-08-28
        • 1970-01-01
        • 1970-01-01
        • 2016-03-12
        相关资源
        最近更新 更多