Sorting ferret results by column

I have the acts_as_ferret plugin installed. Everything searches great,
but I would like to limit the results (i.e. by ‘end_date’) and sort them
(by ‘end_date’). ‘end_date’ is a valid column in my “posts” table.
Here’s the code I have already:

@posts = Post.find_by_contents(params[:query])

params[:query] comes from a form. I am replacing less efficent code that
has the restrictions working :

@posts = Post.find(:all,
:conditions => [ ‘(title LIKE :search_query OR body
LIKE :search_query) AND end_date >= :enddate’,
{:search_query => ‘%’ + params[:query] + ‘%’,
:enddate => Time.now}],
:order => ‘end_date’)

I realize i have to pass something similar to :conditions and :order,
but I can’t seem to get ferret to bey the options.

Hi,

You need to pass a SortField to your search, like this:

sort_fields = []
sort_fields << Ferret::Search::SortField.new(“end_date”, :reverse =>
:true)

@posts = Post.find_by_contents(“my query”, :sort => sort_fields)

To limit results by date you could use a range query:

http://ferret.davebalmain.com/api/classes/Ferret/Search/RangeQuery.html

Or pass a query to the queryparser like this (from the ferret site):

A range query finds all documents with terms between the two query
terms. This can be very useful in particular for dates. eg;

‘end_date’:[20050725 20050905]’ # all end dates >= 20050725 and <=
20050905
‘end_date’:[20050725 20050905}’ # all end dates >= 20050725 and <
20050905
‘end_date’:{20050725 20050905]’ # all end dates > 20050725 and <=
20050905
‘end_date’:{20050725 20050905}’ # all end dates > 20050725 and <
20050905

/David