0

: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?

dan
  • 1,030
  • 1
  • 9
  • 24
  • Here's a good resource I just read from thoughtbot: [Implementing Multi-Table Full Text Search with Postgres in Rails](https://robots.thoughtbot.com/implementing-multi-table-full-text-search-with-postgres) – konyak Sep 28 '15 at 22:17

2 Answers2

1

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.

Esse
  • 3,278
  • 2
  • 21
  • 25
  • I've got it all up and running, but the search is returning a different object - # rather than my book objects - probably something really simple but any idea what it might be? If its complicated I'll stick it in another question :) – dan Oct 23 '14 at 15:14
  • You need to call ````results```` on it - that will give you array (or will_paginated-array) of AR objects :) – Esse Oct 23 '14 at 15:53
  • btw: you may want to take a look at documentation of sunspot. It got **a lot** of useful options (f.e. boosting one field, grouping results). And it's really fast. – Esse Oct 23 '14 at 15:57
  • When I came to saving new objects, I realised the original code example wasn't quite right, so I've updated it to what works for me. I have not sorted the ranking part out yet, so have commented that out. – dan Oct 24 '14 at 08:49
1

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?).

Community
  • 1
  • 1
blelump
  • 3,233
  • 1
  • 16
  • 20
  • I've had a look texticle and textacular - had not seen pg_search - I'll have a look, thanks! – dan Oct 23 '14 at 15:04