On 7/20/06, Caspar <[EMAIL PROTECTED]> wrote:
> 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
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
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk