ruby-on-railsmongodbelasticsearchmongoidmongoid5

mongoid create text index on all text fields on model


Can I create text index for all fields in ruby model like with this mongodb command: db.documents.createIndex({ "$**": "text" }, { name: "TextIndex" })

Also can we somehow add number properties to index. I've tried like this but it doesn't

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"})

Solution

  • You can create the index you need as the second line in the following example:

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

    Take a look to this link for further information: https://github.com/mongodb/mongo-ruby-driver/blob/master/docs/tutorials/ruby-driver-indexing.txt