# Advanced search with ferret?

**URL:** <https://www.ruby-forum.com/t/advanced-search-with-ferret/77114>\
**Category:** Ferret\
**Created:** [November 24, 2006, 12:07pm UTC](https://www.ruby-forum.com/t/advanced-search-with-ferret/77114 "2006-11-24T12:07:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnessa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@agnessa](https://www.ruby-forum.com/u/agnessa)\
**Post date:** [November 24, 2006, 12:07pm UTC](https://www.ruby-forum.com/t/advanced-search-with-ferret/77114/1 "2006-11-24T12:07:47Z")

</div>

Hello,

I’m a novice to ferret, so far only used it via acts\_as\_ferret. My  
question is about a recommended pattern for an ‘advanced search’, which  
would be searching by all fields of a model and some fields from related  
models, with range search, expression search and wildcards. The kind of  
search in which a user is presented with a huge form that allows them to  
set the variuos criteria.

is this something ferret (acts\_as\_ferret?) is suited for and is there a  
clean way to do it?

Thanks,  
Agnieszka F.

---

<div class="post-metadata">

**Author:** ![agnessa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@agnessa](https://www.ruby-forum.com/u/agnessa)\
**Post date:** [November 24, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/advanced-search-with-ferret/77114/2 "2006-11-24T21:30:58Z")

</div>

On 24.11.2006, at 12:07, Agnieszka F. wrote:

> is this something ferret (acts\_as\_ferret?) is suited for and is  
> there a  
> clean way to do it?

When I last checked, you could use Ferret’s advanced query syntax in  
acts\_as\_ferret. So you can use wildcards, ranges, phrases, boolean  
expressions and field qualifiers just as if you used Ferret directly.

If you haven’t used Ferret’s query language before, check out the  
RDoc documentation for Ferret::QueryParser. It’s pretty well  
explained there.

The ‘pattern’ for implementing an advanced search form would be to  
gather the information from the form an build a query string from that.

As for indexing/searching fields from related models: This has been  
extensively discussed on this list recently, so you might want to  
consult the archives. In short, you provide an accessor method for  
the field in the related model and index that.

Cheers,  
Andy

---

<div class="post-metadata">

**Author:** ![agnessa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@agnessa](https://www.ruby-forum.com/u/agnessa)\
**Post date:** [November 24, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/advanced-search-with-ferret/77114/3 "2006-11-24T22:05:15Z")

</div>

Andreas K. wrote:

> If you haven’t used Ferret’s query language before, check out the  
> RDoc documentation for Ferret::QueryParser. It’s pretty well  
> explained there.

Hello,

Thank you, I’m definitely missing this part so far.

As for the advanced search pattern, I’m only worried that my code for  
glueing the query from all sorts of fields will be very complex. That’s  
why I’m trying to think of something easier to maintain.

So far I was thinking of a solution going along these lines:

- data from the search form would be used to create a pattern object
- the query would be constructed by iterating through all the  
field\_infos of the index
- for each field\_info add to the query a pair attribute : value of that  
attribute from the pattern object

I’m considering whether it’s a good course to take, seems tempting  
because of its dryness – no need to alter the search method after  
extending the index. And it would make the search method a few lines  
only, simple loop and not a terrifying sequence of ifs and concats 🙂

I’m trying to spot the weaknesses of this idea before I start coding. I  
would be happy to hear your thoughts on this idea!

Agnieszka
