Forum: Ferret Sorting issues, can anyone help me?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Manoel L. (Guest)
on 2007-03-31 17:45
I have this model:

class Post < ActiveRecord::Base

  acts_as_ferret  :fields => {  :title            => {:boost => 2},
                                :description      => {},
                                :url              => {},
                                :rank_sort        => {:index =>
:untokenized_omit_norms, :term_vector => :no},
                                :posted_at_sort   => {:index =>
:untokenized_omit_norms, :term_vector => :no}
                            },  :remote => true

  belongs_to :blog

  def rank_sort
    begin
      return self.blog.rank_links.to_i
    rescue
      return nil
    end
  end

  def posted_at_sort
    begin
      return self.posted_at.to_i
    rescue
      return nil
    end
  end

end

But when I try to sort by :rank_sort or :posted_at_sort it didn't work,
see:

>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new(:rank_sort, :reverse => false)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new(:rank_sort, :reverse => false)} )[1].last.posted_at_sort
=> [1085549830]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new(:rank_sort, :reverse => true)} )[1].last.posted_at_sort
=> [1085549830]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new(:rank_sort, :reverse => true)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new(:rank_sort, :reverse => :true)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new(:rank_sort, :reverse => :false)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new("rank_sort", :reverse => :false)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new("rank_sort", :reverse => false)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new("rank_sort", :reverse => true)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new("rank_sort", :reverse => :true)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new("rank_sort", :reverse => :yes)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new("rank_sort", :reverse => :no)} )[1].first.posted_at_sort
=> [1146857920]
>> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new("rank_sort", :reverse => :no)} )[1].last.posted_at_sort
=> [1085549830]
>>

Can anyone help me with this?

[]s

Manoel
Jens K. (Guest)
on 2007-04-01 13:12
(Received via mailing list)
On Sat, Mar 31, 2007 at 03:45:40PM +0200, Manoel L. wrote:
> :untokenized_omit_norms, :term_vector => :no}
>                             },  :remote => true
>
>   belongs_to :blog
>
[..]
> >> h,r = Post.full_text_search("voltamos", {:sort => 
Ferret::Search::SortField.new("rank_sort", :reverse => :no)} )[1].last.posted_at_sort
> => [1085549830]

First of all, use symbols for your field name (don't know if that
solves the problem, but you should use symbols for fieldnames
everywhere).

Then, try to give an array of sort fieldsas value of the :sort
parameter, and specify the correct type for your integer fields. So to
sort by rank_sort, descending, do:

:sort => [ Ferret::Search::SortField.new(:rank_sort, :type => :integer,
:reverse => true) ]

Jens

--
Jens Krämer
webit! Gesellschaft für neue Medien mbH
Schnorrstraße 76 | 01069 Dresden
Telefon +49 351 46766-0 | Telefax +49 351 46766-66
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa
Manoel L. (Guest)
on 2007-04-01 21:21
Jens, so this is the correct way to define the fields:

:rank_sort => {:index => :untokenized_omit_norms, :term_vector => :no}

since rank_sort is like this:

  def rank_sort
    return self.rank_links.to_i if !self.rank_links.blank? and
self.rank_links > 0
    return 999999999
  end
Jens K. (Guest)
on 2007-04-02 14:28
(Received via mailing list)
On Sun, Apr 01, 2007 at 07:21:16PM +0200, Manoel L. wrote:
> Jens, so this is the correct way to define the fields:
>
> :rank_sort => {:index => :untokenized_omit_norms, :term_vector => :no}
>
> since rank_sort is like this:
>
>   def rank_sort
>     return self.rank_links.to_i if !self.rank_links.blank? and self.rank_links > 0
>     return 999999999
>   end

yeah, looks ok.

Jens

--
Jens Krämer
webit! Gesellschaft für neue Medien mbH
Schnorrstraße 76 | 01069 Dresden
Telefon +49 351 46766-0 | Telefax +49 351 46766-66
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa
This topic is locked and can not be replied to.