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 -- Posted via http://www.ruby-forum.com/.
On 7/20/06, Caspar <waspfactory at gggmmail.com> 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 > CasparOne 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 Balmain wrote:> On 7/20/06, Caspar <waspfactory at gggmmail.com> wrote:[..]> > 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? > > 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") > endthat''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.> 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?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 kraemer at webit.de Schnorrstra?e 76 Tel +49 351 46766 0 D-01069 Dresden Fax +49 351 46766 66
tried the solution that was posted but no luck. seems to make exactly no difference. def ferret_date_registered date_registered.strftime("%Y%m%d") end ##### log/development.log################# Adding field date_registered with value ''Wed Jul 19 12:07:52 BST 2006'' to index what am I doing wrong? regards Caspar -- Posted via http://www.ruby-forum.com/.
On 7/20/06, Caspar <waspfactory at gggmmail.com> wrote:> tried the solution that was posted but no luck. > seems to make exactly no difference. > > def ferret_date_registered > date_registered.strftime("%Y%m%d") > end > > ##### log/development.log################# > Adding field date_registered with value ''Wed Jul 19 12:07:52 BST 2006'' > to index > > what am I doing wrong? > regards > CasparHave another look at the solution posted. You must have forgotten this line; acts_as_ferret :fields=> [''short_description'',...,''ferret_date_registered'']