dan
dan

Reputation: 1080

Not quite multiple model search - PG

:user has_many :books etc.

I want to implement a search which matches against books or users, but as books and users are so closely related, it does't quite feel like a multiple class search.

Essentially I want to perform a full text search against -

book.title
book.description
book.user.username
book.user.aboutuser

... and return ranked book objects. Currently I'm playing around with textacular. Am I right in thinking that this is not really a multiple class search? Can anyone point to a good resource for woking out how to make the query?

Upvotes: 0

Views: 153

Answers (2)

blelump
blelump

Reputation: 3243

It depends of how "big" is your problem, hundreds or millions of records? The most common search engines for the latter are Solr or Sphinx (a comparison more discussed here: Choosing a stand-alone full-text search server: Sphinx or SOLR? ) and both of them are having Rails support. I've personally used Sphinx, however if you do not have bunch of records, try pg_search gem (see also this discussion: Any reason not use PostgreSQL's built-in full text search on Heroku?).

Upvotes: 1

Esse
Esse

Reputation: 3298

I would use sunspot here - which uses Sorl, quite good search engine.

In your gemfile:

gem 'sunspot_rails'
gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development

After required bundle, you can create config and launch solr deamon:

rails generate sunspot_rails:install
bundle exec rake sunspot:solr:start

In your model:

  searchable do
    integer :rank
    text :title, :descritpion
    text :aboutuser do
      users.map { |user| user.aboutuser }
    end

    text :username do
      users.map { |user| user.username }
    end
  end

Usage:

books = Book.search do
  fulltext 'my text'
  order_by :rank, :desc
end

Only minus is that you need to launch it on production server - which shouldn't be done by using rake sunspot:solr:start. Instead you should it deploy properly on (probably) Tomcat or other java app server.

Upvotes: 1

Related Questions