Sorting and pagination

Hello All,
Okay i think I’m finally getting all of what i want out of ferret
working, thanks mostly to reading this forum and also getting ALOT of
questions answered, thanks alot everyone. Anyway my last ferret task is
too get the results sorted by a field called date_registered and have
this working with pagination.

here is what i’m doing at the moment:

################################
acts_as_ferret :fields=> [‘short_description’,…,‘date_registered’]

def VoObject.find_results(query,page)

sort_fields = []
sort_fields << Ferret::Search::SortField.new("date_registered",

:reverse => :false)

	results2 = VoObject.find_by_contents(query,:num_docs=> 2000000,:sort

=>sort_fields )
num = results2.size

	if page == 1
		page = 0
	else
		page = (page-1)*20
	end

	results = VoObject.find_by_contents(query,:first_doc=>page,

:num_docs=> 20,:sort => sort_fields)
[num,results,results2]
end
##############################

I added date_registered to the ferret fields and rebuilt the index (not
sure if this is neccessary but anyway) but my results page still shows
the results in a random order. Seems like the sorting has no effect.
date_registered is a date_time field. I call ifnd by contents twice in
the above code because i need to populate some dropdowns with values for
the entire returned results so that they can be used to refine the
search. Also just from the look of this code am i right in thinking that
each page of results will be sorted but not the entire returned results?

Any ideas?
thanks
regards
Caspar

On 7/20/06, Caspar [email protected] wrote:

################################
num = results2.size
end

Any ideas?
thanks
regards
Caspar

One of the acts_as_ferret guys will be able to confirm this but I
don’t think acts_as_ferret automatically converts dates to a format
that is searchable. What you need to do is convert the DateTime to a
format that is lexicographically sortable;

acts_as_ferret :fields=> 

[‘short_description’,…,‘ferret_date_registered’]

def ferret_date_registered
    date_registered.strftime("%Y%m%d")
end

This should fix your problem. Note that you if you need a little more
precision
you can add hours, minutes, etc.

          results2 = VoObject.find_by_contents(query,:num_docs=> 2000000,:sort

=>sort_fields )

Just a word of warning. Whatever you specify num_docs to be, an array
is created to hold that many hits, so even if your query only returns
2 hits, the above line will use 2 Mb (bit it will be freed immediately
once the search is finished, not garbage collected). I might try and
change that behaviour this morning.

On this topic, :first_doc is going to be changing to :offset and
:num_docs to :limit. I’m toying with the idea of making :all a valid
option for :limit to get all search results although :limit => :all
sounds a bit funny to me. Perhaps simply :limit => nil would be
better. Feedback anyone?

Cheers,
Dave

On Thu, Jul 20, 2006 at 09:13:30AM +0900, David B. wrote:

On 7/20/06, Caspar [email protected] wrote:
[…]

One of the acts_as_ferret guys will be able to confirm this but I
don’t think acts_as_ferret automatically converts dates to a format
that is searchable. What you need to do is convert the DateTime to a
format that is lexicographically sortable;

acts_as_ferret :fields=> ['short_description',...,'ferret_date_registered']

def ferret_date_registered
    date_registered.strftime("%Y%m%d")
end

that’s right, you’ll have to do the datetime to string conversion
yourself. I think we could add conversions like that to acts_as_ferret
in a future version.

change that behaviour this morning.

On this topic, :first_doc is going to be changing to :offset and
:num_docs to :limit. I’m toying with the idea of making :all a valid
option for :limit to get all search results although :limit => :all
sounds a bit funny to me. Perhaps simply :limit => nil would be
better. Feedback anyone?

actually acts_as_ferret already supports :num_docs => :all, where the
:all gets transformed to a user-definable (via the :max_results option
in
the call to acts_as_ferret) large number of records.

I agree that :limit => nil makes more sense.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs