Limit results to specific IDs

How would you go about searching only a specific set of IDs in
acts_as_ferret? Should I just create a huge “or” query or is there a
more stylish way?

Thanks,
Winton

New to this, but can’t you do something like:

your_index.search(‘id:1|2|3|4|5|6|7’)

???

On Fri, Sep 29, 2006 at 07:18:25AM -0400, Vince P. wrote:

New to this, but can’t you do something like:

your_index.search(‘id:1|2|3|4|5|6|7’)

that would result in the huge ‘or’ query mentioned :slight_smile:

however, in that special case a range query would be more
appropriate: ‘id:[1 7]’

with acts_as_ferret, you can also limit the result set on the
active_record side of things, by giving a standard AR conditions
argument:

find_by_contents(‘query’, {}, :conditions => [‘in in (?)’, id_array])

Note that this makes ferret’s limit and offset parameters useless, as
the ar conditions further limit the resultset found in the ferret index.
Usually this is intended to be used on fields that aren’t in the ferret
index or are easier to query via the DB.

In your case I’d go for the or query approach, maybe you can even
optimize them to some range queries.

Jens


Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

Great, thanks to all of you for your help. Looks like I’ll just be
making a big ol’ query since the IDs aren’t sequential.

  • Winton

On 9/29/06, Winton [email protected] wrote:

How would you go about searching only a specific set of IDs in
acts_as_ferret? Should I just create a huge “or” query or is there a
more stylish way?

Thanks,
Winton

That depends. If all the ids are in a certain range then you are best
off using a range query. If the are randomly distributed then you can
use an “or” query, although a MultiTermQuery would be better. To
specify it in Ferret Query Language just add the terms separated by a
“|”. For example:

index.search(query + ' id:"1257|8732|3428|2387"')

Or if the IDs are in an array you’d do it like this:

index.search(query + ' id:"' + id_ary.join('|') + '"')

You could also build a filter but I don’t think it would be worth it
in this case.

Cheers,
Dave