Reputation: 914
I have a search implemented with the gem sunspot_solr. I would like to add links in the search results to be able to filter the search by alphabetical order and other parameters. my controller is
class SearchController < SuperSiteController
def index
@sunspot_search = Sunspot.search User, Post do |query|
query.keywords @search_query
query.paginate(:page => params[:page], :per_page => 30)
end
@posts = @sunspot_search.results
end
I wish I could filter for older, recent, and alphabetical order within your search has already been completed. I did not find anything in the documentation about this.
Has anyone worked with this type of search before, and if so, do you know the best practice for doing this?
Upvotes: 1
Views: 158
Reputation: 94
I think you aim to order results based on a property (data field) of documents.
For example, to sort the results of Post
model by created_at
, you have to add this field to the index definition in app/models/post.rb
as below:
class Post < ActiveRecord::Base
searchable do
...
time :created_at
...
end
end
And then in you SearchController
sort the result as below:
class SearchController < SuperSiteController
def index
Post.search do
fulltext @search_query
paginate(:page => params[:page], :per_page => 30)
order_by :created_at, :desc #:desc for descending, :asc for ascending
end
@posts = @sunspot_search.results
end
In order to order results alphabetically you just have to add the field to you index definition as what mentioned above with text function instead of time and use order_by
in SearchController
.
Upvotes: 0