Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you sort Using Thinking Sphinx on Rails 3?

I'm trying to accomplish a simple sort using parameters passed to my controller. I'm following the documentation on the Searching & Thinking Sphinx website and I'm met with the following error. What am I doing wrong?

The following @places object is an instance of the think Sphinx class.

 @places = Place.search(params[:q], :order => :created_at)

ThinkingSphinx::SphinxError (index place_core: sort-by attribute 'created_at' not found):
like image 657
JZ. Avatar asked May 14 '11 00:05

JZ.


1 Answers

You need to add fields on which you want to search. Then to sort by a field you need to mark it as sortable in your model, or you need to add an attribute in the define_index method as explained here.

For your model, something like this:

class Place < ActiveRecord::Base
  # ...

  define_index do
    # fields
    indexes subject, :sortable => true
    indexes content
    indexes author.name, :as => :author, :sortable => true

    # attributes
    has created_at
  end

  # ...
end

In that example, subject, author and created_at are sortable.

like image 65
Matteo Alessani Avatar answered Sep 28 '22 14:09

Matteo Alessani