Freelance Mobile & Web Developer

A Ruby & Javascript hacker.

Full Text Search With MongoDB

Recently I had to implement full text search on a Ruby on Rails application I was developing and based on my needs the easiest way to do it was building a helper array with the keywords needed and search using those keywords.

To illustrate this I’ve implemented stock tickers search using the awesome rstat.us project. Have a look at my Github fork.

update.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Update
...
    # Adds stocks array mentions
    key: stocks, Array, :default => []

  # Parses update text and build stocks array for simpler search
  def parse_symbols
    stocks = []
    self.text.split(' ').each do |update|
      stocks << update if update[0] == '$'
    end
    return stocks
  end
end

and then the controller:

updates_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
class UpdatesController < ApplicationController
...
    def create
      ...
      u = Update.new(:text => params[:text],
          :referral_id => params[:referral_id],
          :author => current_user.author,
          :twitter => do_tweet)

      u.stocks = u.parse_symbols
      ...
    end
end

So it basically looks for words starting with $ and adds it to the stocks array. We can then search using

Perform search using MongoDB built in text search
1
Update.all(:conditions => {:stocks => '$goog'})

There are different solutions such as mongoid_search that could be used but for my needs this approach works the best.

MongoDB gives a lot of flexibility for the developer and speeds things up. Of course there are trade-offs to be made while using so YMMV.