-->

How to sort with elasticsearch?

2019-09-12 00:31发布

问题:

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:

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