How to sort with elasticsearch?

人走茶凉 提交于 2019-12-25 04:08:11

问题


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


来源:https://stackoverflow.com/questions/14756149/how-to-sort-with-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!