# Need some suggestions on search strategy

**URL:** <https://www.ruby-forum.com/t/need-some-suggestions-on-search-strategy/141409>\
**Category:** Rails\
**Created:** [June 25, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/need-some-suggestions-on-search-strategy/141409 "2008-06-25T16:07:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [June 25, 2008, 4:07pm UTC](https://www.ruby-forum.com/t/need-some-suggestions-on-search-strategy/141409/1 "2008-06-25T16:07:51Z")

</div>

I need some suggestions for the best way to implement search for my  
client. They have several models (project, request, usage, etc.) that  
they need to search for. Each model will have its own search page  
with a form that is similar to the one shown for editing. So the  
search needs to be across model fields and the results are limited to  
the model.

Some of the model fields will be for associations (like comments) but  
again we’re only interested in returning a single model. For some of  
the models we also want to filter results based on a parent. For  
example, when searching requests we want to limit the requests to a  
specific project.

Finally, for one of the models, we need full text searching across  
fields (as opposed to a search form with inputs for every field.) The  
search is still limited to returning only results for a specific  
model. There would also be one or two filters so the user could  
explicitly limit a few select fields.

I would really appreciate any suggestions you might have on the  
general approach you would use to solve this problem. I’m using Rails  
2.1 btw so named scope is available.

TIA,

Sean

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [June 25, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/need-some-suggestions-on-search-strategy/141409/2 "2008-06-25T16:46:34Z")

</div>

I’ve implemented something like this with ferret & acts\_as\_ferret

this line:

acts\_as\_ferret :fields =\> {:name =\> {:boost =\> 3}, :description =\>  
{:boost =\> 2, :store =\> :no}, :tag\_list =\> {}},  
:index\_dir =\> File.join(RAILS\_ROOT, “public”,  
“index”)

is all I need to allow easy searching  
In this case it’s mixed with acts\_as\_taggable and indexes name,  
description and the tags

put the search text in @query and make something like this:

@products = Product.find\_with\_ferret(@query,  
{:page =\>  
params[:page].to\_i, :per\_page =\> params[:per\_page].to\_i},  
{:conditions =\>  
["(products.status = 1 AND products.created\_at BETWEEN ? AND ?)",  
“#{query\_start\_date} 00:00:00”, “#{query\_end\_date} 23:59:59”],  
:order =\> @sort})

allowing for a mix of fulltext & database search  
As you may see, pagination is included

Another option that quite some people seem to like is ultrasphinx  
I haven’t used it yet, but people seem to be happy with it

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [June 25, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/need-some-suggestions-on-search-strategy/141409/3 "2008-06-25T17:08:21Z")

</div>

I knew about ferret but I had forgotten that you can filter ferret  
with conditions (and filtering is key for me.) Turns out you can  
filter with something like

@posts= Post.find\_with\_ferret(query, {:page =\>  
params[:page], :per\_page =\> per\_page}, :conditions =\> [‘category\_id  
= ?’, category\_id])

Thanks for the tip.

Sean

---

<div class="post-metadata">

**Author:** ![schof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8dc957/32.png) [@schof](https://www.ruby-forum.com/u/schof)\
**Post date:** [June 26, 2008, 5:40am UTC](https://www.ruby-forum.com/t/need-some-suggestions-on-search-strategy/141409/4 "2008-06-26T05:40:11Z")

</div>

Sean,

We’ve implemented similar searches using acts\_as\_ferret and it’s been  
easy to use, pretty fast and efficient. I’m not 100% about how  
scalable it is but I suspect it’ll do very well.  
I’d highly recommend it - you should be able to get it up and running  
in less than a day.

Dale
