3

I have text index in MongoDB and want to use text command for searching in my collection. Can't find this functionality in Mongoid.

1
  • 1
    Are you looking for something like this? Commented Apr 23, 2013 at 2:58

2 Answers 2

2

I have tried to find the solution as I am also a mongoid user. I haven't found anything specific related to search text in mongoid other than where query. After little investigation I have found mongoid_search gem which could be useful for search text.

It has one limitation that is written in very first line of readme is "If your searchable model is big (i.e. 1.000.000+ records), solr or sphinx may suit you better." If you have so you need to configure solr or elasticsearch for better performance. If you are going to deploy on heroku then please develop in that consideration too.

May be this would help you to achieve your goal.

Sign up to request clarification or add additional context in comments.

Comments

1

Recently ran into a similar situation. You can run a search command in Mongoid in the following manner:

session = Mongoid.session('default')
session.command({"text" => 'my_collection', 'search' => 'whatever'})

Makes for a nice mixin:

# mixins/search.rb
module Mixins
  module Search

    extend ActiveSupport::Concern

    module ClassMethods

      def search(query)
        session = Mongoid.session('default')
        session.command({"text" => collection.name, 'search' => query})
      end

    end

  end
end

That way you can just include Mixins::Search into your models and use Model.search 'whatever' to search for stuff.

1 Comment

This looks like the correct answer. I would tweak the Mixin to use Mongoid's mongo_session document class method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.