public
Description: Solr-powered search for Ruby objects
Home | Edit | New

Ordering and pagination

Ordering

Sunspot allows ordering on one or more fields using the order_by method in the search DSL. order_by may be called more than once, and the earlier calls will have higher ordering precedence than the later calls. Each call to the method should take the field name and either :asc or :desc:


Sunspot.search(Post) do
  order_by(:blog_id, :asc)
  order_by(:created_at, :desc)
end

Sunspot also provides the special method order_by_random, which uses Solr’s RandomSortField to (usually) return results in a different order each time.

Pagination

In Solr, all searches are paginated . Sunspot sets a default of 30 results per page, although this can be changed globally (see below). To specify pagination explicitly, use the paginate method, which takes two options: :page and :per_page (this should be familiar to anyone who has used WillPaginate). :page is required.


Sunspot.search(Post) do
  paginate(:page => 2, :per_page => 15)
end

Chaging default pagination

The default number of results to return per page (if no pagination is specified for a particular search) can be changed using the configuration API:


Sunspot.configuration.pagination.default_per_page = 30

Last edited by outoftime, Tue Aug 11 05:13:09 -0700 2009
Home | Edit | New
Versions: