Strange Sorting Issues

Hi there,

I’m having some strange sorting stuff goign on. Here’s my search method:

sort_fields = []
sort_fields << Ferret::Search::SortField.new(“name”,
:reverse => :false)
@results = Listing.find_by_contents @search_criteria, :limit => :all,
:sort => sort_fields

page = (params[:page] ||= 1).to_i
items_per_page = 9
offset = (page - 1) * items_per_page
@pages = Paginator.new(self, @results.length, items_per_page, page)
@results = @results[offset…(offset + items_per_page - 1)]

For some queries, the sorting is correct. Other times, it’s not. I’m not
sure what’s causing this. Any help would be greatly appreciated!

We’re using ferret 10.3, I believe.

Thanks.

Are you sorting by the same “name” field each time, or is it by
different
fields?

For some queries, the sorting is correct. Other times, it’s not. I’m not
sure what’s causing this. Any help would be greatly appreciated!

I’ve had the same problem. I solved it by using the find_options in
find_by_contents method. Like this:

find_by_contens(q, options, find_options)

find_options is a hash passed on to active_recordâ??s find when retrieving
the data from db, useful to i.e. prefetch relationships.

So for your query:

@results = Listing.find_by_contents @search_criteria, {:limit => :all,
:sort => sort_fields},{:order => “name ASC”}

I’m not sure this is the best way but it worked for me.

/David

On Mon, Oct 02, 2006 at 09:15:43AM +0200, David W. wrote:

So for your query:

@results = Listing.find_by_contents @search_criteria, {:limit => :all,
:sort => sort_fields},{:order => “name ASC”}

I’m not sure this is the best way but it worked for me.

please note that this will only work with :limit => :all, otherwise
you’ll only sort the subset of records retrieved from ferret, not the
whole result set.

As :limit => :all can be very expensive (with Ferret returning
all results, and aaf fetching them all from the db), making the Ferret
sorting work correctly would be the better way.

Is the correctness of sorting related to a special kind of queries ?

Jens


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
SchnorrstraÃ?e 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

On Mon, Oct 02, 2006 at 12:03:31PM +0200, David W. wrote:

“items.id in (1,12,13,45,23)” was still in the wrong order unless a
passed an find_options ordering them by “name”.

aaf is supposed to retain the sorting of results delivered by Ferret.
The records retrieved with the sql ‘in’ clause are sorted afterwards so
they are in the same order as the originial Ferret result set.

At least that is how it is supposed to be.
Could you please post your acts_as_ferret declaration, and the snippet
where you call find_by_contents, so I can check if this is a bug in aaf?

cheers,
Jens


webit! Gesellschaft für neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Krämer [email protected]
Schnorrstraße 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66

please note that this will only work with :limit => :all, otherwise
you’ll only sort the subset of records retrieved from ferret, not the
whole result set.

Just to make sure that I don’t misunderstand something. If I skip the
find_options but use a Ferret sort field I get the correct result (for
exampel, 20 hits ordered by name).

My problem was that if I didn’t provide the find_options, the records
when loaded with an sql like this (in the find_by_contents method)
“items.id in (1,12,13,45,23)” was still in the wrong order unless a
passed an find_options ordering them by “name”.

/David

Hey guys.

Thaks for all the great feeback.

Actually using David W.'s suggestions did the trick!

Thanks David!

M.

Jens K. wrote:

On Mon, Oct 02, 2006 at 12:03:31PM +0200, David W. wrote:

“items.id in (1,12,13,45,23)” was still in the wrong order unless a
passed an find_options ordering them by “name”.

aaf is supposed to retain the sorting of results delivered by Ferret.
The records retrieved with the sql ‘in’ clause are sorted afterwards so
they are in the same order as the originial Ferret result set.

At least that is how it is supposed to be.
Could you please post your acts_as_ferret declaration, and the snippet
where you call find_by_contents, so I can check if this is a bug in aaf?

cheers,
Jens


webit! Gesellschaft f�r neue Medien mbH www.webit.de
Dipl.-Wirtschaftsingenieur Jens Kr�mer [email protected]
Schnorrstra�e 76 Tel +49 351 46766 0
D-01069 Dresden Fax +49 351 46766 66