# Grouping with ferret

**URL:** <https://www.ruby-forum.com/t/grouping-with-ferret/144302>\
**Category:** Ferret\
**Created:** [July 28, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/grouping-with-ferret/144302 "2008-07-28T13:30:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cubiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b19c9b/32.png) [@cubiq](https://www.ruby-forum.com/u/cubiq)\
**Post date:** [July 28, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/grouping-with-ferret/144302/1 "2008-07-28T13:30:11Z")

</div>

Hi list,

I have a problem grouping with ferret.

I’m using the filter\_proc from Dave’s book as seen below

results = {}  
group\_by\_proc = lambda do |doc\_id, score, searcher|  
doc = searcher[doc\_id]  
(results[doc[:pk\_file\_id]]||=[]) \<\< doc[:filename] \<\< doc[:path]  
next true  
end

The problem is that if I use this it ignores my limit clause.

I set limit on 10 and I still get 5995 results and it takes several  
seconds.

How come the limit clause is ignored when using a filter\_proc? How can  
I change this behaviour?

term = “wi”

bool1 = Ferret::Search::BooleanQuery.new()  
bool1.add\_query(Ferret::Search::PrefixQuery.new(:filename, “#{term}”))  
bool1.add\_query(Ferret::Search::PrefixQuery.new(:path, “#{term}”))

index.search(bool1, :limit =\> 10, :filter\_proc =\> group\_by\_proc)

puts result.size  
5995

Cheers,  
Henke

---

<div class="post-metadata">

**Author:** ![cubiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b19c9b/32.png) [@cubiq](https://www.ruby-forum.com/u/cubiq)\
**Post date:** [July 29, 2008, 11:41am UTC](https://www.ruby-forum.com/t/grouping-with-ferret/144302/2 "2008-07-29T11:41:51Z")

</div>

Hi!

On 28.07.2008, at 13:29, Henrik wrote:

> next true  
> end
> 
> The problem is that if I use this it ignores my limit clause.
> 
> I set limit on 10 and I still get 5995 results and it takes several  
> seconds.
> 
> How come the limit clause is ignored when using a filter\_proc? How  
> can I change this behaviour?

Filters are applied by Ferret before the result is limited, that’s why  
your filter gets to see all possible results regardless of the limit  
you specify. If it was implemented the other way around, first  
limiting and then filtering, you would possibly end up with less than  
limit results in case your filter would actually filter out any  
results. Of course in your case this wouldnt happen as your filter  
does no filtering but always returns true.

If you really only want the first 10 results, why dont you just use  
the results you get back and do your result  
collecting there like this?

results = {}  
hit\_count = index.search\_each(query, :limit =\> 10, :filter\_proc =\>  
group\_by\_proc) do |doc, score|  
(results[doc[:pk\_file\_id]]||=[]) \<\< doc[:filename] \<\< doc[:path]  
end

You could of course also return false in your filter\_proc for every  
possible hit once your results collection has reached the desired size  
to save the time collecting all results.

cheers,  
Jens

–  
Jens Krämer  
Finkenlust 14, 06449 Aschersleben, Germany  
VAT Id DE251962952  
[http://www.jkraemer.net/](http://www.jkraemer.net/) - Blog  
[http://www.omdb.org/](http://www.omdb.org/) - The new free film database
