I think the simplest way to attack this problem is just to write a
paginator for an array of objects. Then you can just do your usual
find_by_contents, with num_docs set to :all, and then just paginate the
array that is returned.
Tom D. wrote:
To add to what Jens said, you may find this code useful:
The snipped out portion is just where you process the results. In my
case, I am searching for gifts so here is how I search and collect the
Gift objects:
num = INDEX.search_each(query, {:num_docs => options[:limit],
:first_doc => options[:offset]}) do |doc, score|
logger.debug(“Found doc: #{doc}, id: #{INDEX[doc][‘id’]}, score:
#{score}”)
gifts << Gift.find(INDEX[doc][‘id’])
end
Also, the reason I defined my own search method as opposed to using
find_by_contents is because I am not using the acts_as_ferret plugin.
From looking at the acts_as_ferret code, it looks like you can just
use the SearchResults object returned by the find_by_contents since it
also includes the total hits needed to create the Paginator pages.
Oh, one more thing, I just looked and the find_by_contents didn’t
support returning the total results when I wrote my original
pagination post
Also, I don’t think the current acts_as_ferret release supports it
either (but the trunk does), so you may have to grab the trunk if you
want to paginate in pure acts_as_ferret land.
if this is the case, is there an easier way and also a way to count how
many sets or how many pages there are in a table for a given query?
would i just get a total from the array and then divide by 10 docs?
etc…?
thanks!
Jens K. wrote:
Hi!
On Mon, May 01, 2006 at 08:55:22AM +0200, SchmakO wrote:
end
find_by_contents has two options suitable for paging:
:first_doc (first result to retrieve) and
:num_docs (number of results to retrieve).
so to retrieve results 10 to 20, you would use @results =
Tutorial.find_by_contents(@query,:first_doc=>10,:num_docs=>10)
hth,
Jens
–
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
I guess that’s sort of what you did, but I don’t think you need to
define the self.search method. find_by_contents works just fine.
Mike Michelson wrote:
I think the simplest way to attack this problem is just to write a
paginator for an array of objects. Then you can just do your usual
find_by_contents, with num_docs set to :all, and then just paginate the
array that is returned.
Strange parameter name but yes, that’s how you’d do it.
if this is the case, is there an easier way and also a way to count how
many sets or how many pages there are in a table for a given query?
would i just get a total from the array and then divide by 10 docs?
etc…?
I’m not sure about easier way but you can get the total number of
matching hits from @test.total_hits. On the other hand, @test.size
will be the number of hits returned.
For starters, :first_doc is now :offset and :num_docs is now :limit,
as of Ferret 0.10.0.
right, though aaf still works with the old naming, too
[…]
if this is the case, is there an easier way and also a way to count how
many sets or how many pages there are in a table for a given query?
would i just get a total from the array and then divide by 10 docs?
etc…?
I’m not sure about easier way but you can get the total number of
matching hits from @test.total_hits. On the other hand, @test.size
will be the number of hits returned.
exactly. I didn’t try this, but it should be possible to use a Rails
Paginator to handle the whole pagination stuff.
Jens
–
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