Forums » Ferret » Sorting the Result

Sorting the Result
Posted by P L Patodia (Guest)
on 01.03.2006 16:19
The document describes
search(query, options)
sort: An array of SortFields describing how to sort the results.

I have created index with two fields: 'file' and 'content'

When I give SortField name as 'file' while searching, it results into
error.

The exact command given by me:
index.search_each("sleepless AND dreams", :num_docs => 100, :sort => 
'file')
    do |doc, score|
  ....
end

My question is how do I give field name for sorting.

Thanks and Regards,

P L Patodia
Re: Sorting the Result
Posted by Tom Davies (Guest)
on 01.03.2006 16:22
I believe your sort field needs to be a Ferret::Search::SortField

Here is how I am sorting (with two fields).  You might be able to do
it without the array and just passing in a Ferret::Search::SortField
to :sort.  I haven't tried that though.

    sort_fields = []
    sort_fields << Ferret::Search::SortField.new('created_at')
    sort_fields << Ferret::Search::SortField.new('url')
    INDEX.search_each(query, {:sort => sort_fields}) do |doc, score|

Tom
Re: Sorting the Result
Posted by David Balmain (Guest)
on 02.03.2006 02:59
On 3/2/06, Tom Davies <atomgiant@gmail.com> wrote:
>
> Tom

You may also want to specify the sort type. For example;

     include Ferret::Search
     sort_fields = []
     sort_fields << SortField.new('created_at',
                                          :sort_type =>
SortField::SortType::INTEGER)
     sort_fields << SortField.new('url',
                                          :sort_type =>
SortField::SortType::STRING)
     INDEX.search_each(query, {:sort => sort_fields}) do |doc, score|

Although, now that I think about it, it would be nice if you could
just pass the search method a string or array of strings. Expect to
see this functionality in the future.

Cheers,
Dave