rogerkk
rogerkk

Reputation: 5695

Thinking Sphinx - making indices sortable is not working

I've set up some indices and everything seems to work nicely, until I wanted to try getting the results sorted. I have several models, but the the one in which I am defining my indices is "Doctor". I'm defining them like this:

class SearchController < ApplicationController
  define_index do
    indexes :firstname,  :sortable => true
    indexes :lastname,   :sortable => true
    indexes :sex,        :sortable => true
    indexes practice(:name),    :as => :practice_name,    :sortable => true
    indexes practice(:address), :as => :practice_address, :sortable => true

    # Added after denormalization
    indexes county(:name),        :as => :county
    indexes municipality(:name),  :as => :municipality

    # Can only filter on integers, thus CRC32-ing
    has "CRC32(sex)", :as => :sex_filter, :type => :integer
  end
end

Searching seems to be working just fine. When trying to sort on any of the attributes, I get an error like the following

index employee_core: sort-by attribute 'firstname' not found

For firstname, lastname and sex. I am able to partially "solve" this by adding

has firstname, lastname, sex

to the define_index block, but I'm not sure how to do the same for the references to another model (Practice), or if it is even possible.

Anyone know what I might be doing wrong in the first place to make the :sortable => true not work, or how I might define the references to Practice as attributes as well?


An example of the call I'm making to Thinking Sphinx which is failing in the manner mentioned above:

ThinkingSphinx.search "<some search string>", {:with=>{}, :order=>:firstname, :sort_mode=>:desc, :page=>1}

Upvotes: 0

Views: 1528

Answers (1)

pat
pat

Reputation: 16226

So, the issue here is that Sphinx fields are not sortable at all. Thinking Sphinx gets around this by adding the :sortable option - which adds an attribute with the same name plus a _sort suffix - so, firstname_sort in this example.

When you're searching on a single model (Employee.search), Thinking Sphinx is able to refer to that model and see which fields are sortable, and automatically switch firstname to firstname_sort for you. However, you're searching globally, and so there's no easy reference for TS. Thus, the workaround is to use the full attribute name instead:

ThinkingSphinx.search "foo", :order => :firstname_sort, :sort_mode => :desc

Also: if you're searching across all indexed models, make sure attributes used in filters and sorting exist in all index definitions.

Adding :sortable to fields from associations will work fine too, given the same caveats.

Upvotes: 3

Related Questions