Reputation: 81450
How do I order results in a case-insensitive alphabetical order using Sunspot?
I couldn't find anything relevant in https://github.com/sunspot/sunspot/wiki/Ordering-and-pagination or http://wiki.apache.org/solr/FunctionQuery or https://github.com/sunspot/sunspot#ordering
Upvotes: 3
Views: 546
Reputation: 1827
I was looking for a similar answer and i haven't found it on SO. However I stumbled upon this piece of magical code: string(:sort_title) { title.downcase }
.
Now a little bit of explaining. Solr (sunspot) will not let you sort/order by text type and string type can't be case insensitive. Thus I thought of the following solution. Let's say you have a field field1
that you want to sort using case insensitive sorting. You then make another field in the searchable part of of indexing let's call it field2
with the value field1.downcase
. You then order by field2
and retrieve the results. This way you get to keep both the case type(in field1 which makes for easy and correct display of data) and sort by case type(with the help of field2). It should look a little like this:
class Test < ActiveRecord::Base
searchable do
string(:field1) { your_class_field }
string(:field2) { your_class_field.downcase(same as above) }
end
end
The answer came a little bit late for you, but maybe some other guy will benefit from it!
Upvotes: 1