clustering by model in a multi-model search

30 views
Skip to first unread message

Liam Morley

unread,
Sep 30, 2008, 8:23:40 PM9/30/08
to Thinking Sphinx
I'm doing a multi-model search, and it's important that models are
shown in a particular order. After searching how to do this in the
discussions, I stumbled upon one of the first questions in the group,
http://groups.google.com/group/thinking-sphinx/browse_thread/thread/5dce1209ec9ffe92
where Pat suggested to use :sort_by => "class_crc ASC, @relevance
DESC" . I had been searching for something like class_crc, so this is
a good start. (Thanks Pat!) However, I'd like to customize how it
orders the classes. I'm using the following search:

ThinkingSphinx::Search.search(cleaned_query,
:classes => [Specialty, Store, MenuCategory, Food],
:sort_mode => :extended,
:sort_by => "class_crc ASC, @relevance DESC", :limit => 100 )

I wanted to sort Specialties at the top, Foods at the bottom. This
doesn't happen when sorting by class_crc. Is there a way that I can
sort by class in the order I'd like? I can't imagine that it'd be wise
to override 'to_crc32', perhaps I could add a new class method? But
I'm not sure how class_crc is tied to to_crc32.

The only other solution I can think of is to make sure that all
indexed fields are unique, and to add extra weight to Specialty
fields, less weight to Food fields, etc, but this seems a little
silly. I also thought of running 4 separate queries, or sorting the
results with Ruby afterwards, but both of those solutions mess with
pagination, and aren't so great efficiency-wise either.

Pat Allan

unread,
Oct 1, 2008, 12:59:05 AM10/1/08
to thinkin...@googlegroups.com
Hi Liam

I think the best approach is to add manual attributes to each of the
model, giving them the sort order you like. So, in Specialties:

has "1", :as => :custom_model_sort, :type => :integer

Store has 2, MenuCategory 3, Food 4 - or something like that. Then
when searching:

ThinkingSphinx::Search.search(cleaned_query,
:classes => [Specialty, Store, MenuCategory, Food],
:sort_mode => :extended,

:sort_by => "custom_model_sort ASC, @relevance DESC",
:limit => 100
)

It's a tad hacky, but it will hopefully work.

Cheers

--
Pat

Liam Morley

unread,
Oct 2, 2008, 4:09:05 PM10/2/08
to Thinking Sphinx
Better to be able to store it in the index than having to store it in
the database, so that's perfect. I'm once again impressed with the
range of things you can do with ThinkingSphinx/Sphinx.

Liam

On Oct 1, 12:59 am, Pat Allan <p...@freelancing-gods.com> wrote:
> Hi Liam
>
> I think the best approach is to add manual attributes to each of the  
> model, giving them the sort order you like. So, in Specialties:
>
> has "1", :as => :custom_model_sort, :type => :integer
>
> Store has 2, MenuCategory 3, Food 4 - or something like that. Then  
> when searching:
>
> ThinkingSphinx::Search.search(cleaned_query,
>    :classes   => [Specialty, Store, MenuCategory, Food],
>    :sort_mode => :extended,
>    :sort_by   => "custom_model_sort ASC, @relevance DESC",
>    :limit     => 100
> )
>
> It's a tad hacky, but it will hopefully work.
>
> Cheers
>
> --
> Pat
>
> On 01/10/2008, at 7:23 AM, Liam Morley wrote:
>
>
>
> > I'm doing a multi-model search, and it's important that models are
> > shown in a particular order. After searching how to do this in the
> > discussions, I stumbled upon one of the first questions in the group,
> >http://groups.google.com/group/thinking-sphinx/browse_thread/thread/5...
Reply all
Reply to author
Forward
0 new messages