dearlbry
dearlbry

Reputation: 3211

Ordering ThinkingSphinx.search results

Has anyone been able to sort search results from an application-wide ThinkingSphinx query?

I have the following setup:

class Resource < ActiveRecord::Base

  # ... 

  define_index do
    indexes :title, :as => :sortable_name, :sortable => true
    indexes :tease
    indexes :description
    indexes authors(:name), :as => :author, :facet => true
  end

end

and

class Author < ActiveRecord::Base

  # ...

  define_index do
    indexes :name, :as => :sortable_name, :sortable => true
    indexes :title
    indexes :bio
  end

end

I want to be able to search across these models (as well as 2 others), and sort the results alphabetically. The fields I need to sort by can have different names, so I index them using :as => :sortable_name (suggestions for doing this a better way are welcome).

The following all work like a charm:

Author.search 'something', :order => :sortable_name

Resource.search 'something', :order => :sortable_name

ThinkingSphinx.search 'something'

But when I try

ThinkingSphinx.search 'something', :order => :sortable_name

I get this:

ThinkingSphinx::SphinxError: index author_core,resource_core: sort-by attribute 'sortable_name' not found

Explicitly limiting the search to those classes using :classes => [Author, Resource] doesn't help. There's clearly something I'm not understanding about the way that Sphinx works here...

I've rebuilt my index and stopped/started the searchd process, all with the same result.

Rails 3.0.5 and ThinkingSphinx 2.0.2

Any suggestions?

Upvotes: 2

Views: 648

Answers (1)

pat
pat

Reputation: 16226

In your Author and Resource search examples, Thinking Sphinx has a single model reference, and so can look at that model to find its fields, and translate sortable_name to sortable_name_sort (the attribute which is actually used for sorting).

When you're searching across multiple models, Thinking Sphinx doesn't delve into each model - so you need to be a little more patient with telling it what to do. Try the following:

ThinkingSphinx.search 'something', :order => :sortable_name_sort

Upvotes: 5

Related Questions