0

I have a model School which is ferret-indexed on 10 or 12 different indexes.

In one particular search, i want to return only schools which match the search term in one of the following fields: ["name", "postcode", "urn"] (urn is a uid kind of field)

For example, a search for "grange" should return schools with "grange" in their name but NOT return schools that have "grange" in their address (unless it's also in their name of course).

If there's just one field (eg name), i can achieve this by

School.find_with_ferret("name:#{term}")

But, i can't work out how to do this with a list of fields. I thought i might be able to use an "or" syntax, like

School.find_with_ferret("name:#{term} || postcode:#{term} || urn:#{term}")

or

School.find_with_ferret("name:#{term} or postcode:#{term} or urn:#{term}")

but neither of these work. Anyone know how to do this? Thanks, max

Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • Off the top of my head, I believe this is defined on the Model level – Kyle Macey Jan 13 '12 at 18:24
  • I've thought of one hacky way of achieving this, which is to make a new field that which aggregates the contents of the three fields i want to search on in the example (name, postcode, and urn) and then do my search against only this new aggregated field, since i *can* do a search against only a single field. Like i say very hacky though and any change to my search requirements would require re-indexing all the records (~80,000, currently takes about 4 hours). – Max Williams Jan 16 '12 at 11:03

2 Answers2

0

In case anyone googles to this with the same problem, i found the answer (in the ferret documentation, doh).

School.find_with_ferret("name|postcode|urn:#{term}")
Max Williams
  • 32,435
  • 31
  • 130
  • 197
0

From the acts_as_ferret documentation:

ActsAsFerret::define_index( 'my_index',
                            :models => {
                              SomeModel => {
                                :fields => {
                                  :name => { :boost => 4, :store => :yes, :via => :ferret_title },
                                  :foo => { :store => :no,  :index => :untokenized },
                                  :baz => { :store => :yes, :via => :ferret_content }
                                }
                              }
                            } )

Note the 'fields' definition

https://github.com/jkraemer/acts_as_ferret

Kyle Macey
  • 8,074
  • 2
  • 38
  • 78
  • Hi kyle - the model defines all the fields that are indexed. My question asks how to do a search using only a subset of those fields. So, in your example, i might want to do a search on SomeModel that only uses the "foo" and "baz" fields, but not the "name" field. That's what i'm asking about. – Max Williams Jan 16 '12 at 10:59