Is there a way to use facets with the pg_search gem
Asked Answered
G

1

6

I'd like to use facets in plus of standard search. Is there a way to make search results be itself "searched" with facets using pg_search?

As far as I can tell, pg_search_scope are mutually exclusive (is there a workaround?). Thanks!

Example:

1) search blogs with word "test"

2) click link to get only articles from previous result that were also posted in june

Glandular answered 20/10, 2011 at 15:14 Comment(0)
I
8

I'm the original author and maintainer of pg_search.

A pg_search_scope works like any other Active Record scope, so you can chain them.

So let's say you have a model Blog with a pg_search_scope named search_title and another scope named in_month that takes two parameters, a month number and a year number. Something like this:

class Blog < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search_title, :against => :title
  scope :in_month, lambda { |month_number, year_number| 
    where(:month => month_number, :year => year_number)
  }
end

Then you can call it like this:

Blog.search_title("broccoli").in_month(6, 2011)

The reverse should also work:

Blog.in_month(6, 2011).search_title("broccoli")

And pagination solutions like Kaminari could also be called on the end.

Indelicate answered 22/10, 2011 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.