0

I am trying to make methods for sorting desc and asc. I am using rails, tire gem and elasticsearch. I am trying to figure out what sort params I can send in the URL

So I have defined in the search-block that it is sorting the result desc order.

sort { by :price, "desc"}

When a user search for apartments in: new-york the result is sort desc order.

The search query/URL looks like this:

http://localhost:3000/apartmens?utf8&query=newyork

Why cant I add a sort-params in the url, like this:

http://localhost:3000/apartmens?utf8&query=newyork&sort=asc

1 Answers1

0

I believe it would be something like this.

params[:sort] ||= 'asc'
Tire.search('apartmens') do |s| 
   s.query do |q|
     q.string 'newyork'
   end
   s.sort { by :__FIELD_YOU_WANT_TO_SORT_, params[:sort]}
end
karmi
  • 14,059
  • 3
  • 33
  • 41
Mike
  • 364
  • 1
  • 9
  • Mike is right. SHUMAcupcake, have a look at Tire's integration tests, https://github.com/karmi/tire/blob/master/test/integration/sort_test.rb – karmi Feb 10 '13 at 07:24
  • For some reason, sort by a date field is not sorting. Its working for all the other types (integers and floats). Is there a specific syntax which should be used when it is a date field ? – Krishna Prasad Varma Aug 01 '14 at 09:25