drf-haystack icon indicating copy to clipboard operation
drf-haystack copied to clipboard

How to sort?

Open alreadysabbir opened this issue 8 years ago • 5 comments

I can search by field like /search?is_approved=true&is_active=true However, I need a way to sort the result by a field e.g. /search?is_approved=true&is_active=true&order=[-date]

How to achieve this?

alreadysabbir avatar Jul 26 '17 18:07 alreadysabbir

Will be looking into this myself in the near future. But could you pass a form_class in your view similar to how it would be achieved with a standard haystack view?

wilkinj33 avatar Oct 31 '17 09:10 wilkinj33

http://www.django-rest-framework.org/api-guide/filtering/#orderingfilter you could try this, it does work!

Mr-win7 avatar Jun 17 '18 21:06 Mr-win7

@rhblind I'm testing the ordering and it is not working with char fields. Do I have to do anything different when indexing char fields?

rodrigowirth avatar Sep 27 '18 17:09 rodrigowirth

@rodrigowirth What backend do you use? I've done some testing on this with Elasticsearch backend and for me it "somewhat" works. The reason is that Elasticsearch (or any other backend for that matter) does ordering slightly differently. I've not fully managed to understand how this works, but in practice any ordering passed are just passed directly to SearchQueryset().order_by(*args) and from there it's up to the backend to order the dataset by its own rules. In my tests I've also tried ordering by char fields and gotten unexpected results.

Unfortunately I don't have any better answer other than that I'd also like to have this feature working, but haven't had time to look too deeply into it yet. 😕

rhblind avatar Sep 28 '18 08:09 rhblind

sorting is not working for the fields of type long in solr. I am using django-haystack library. URL: localhost:8000/searchManagement/search/t/?age_group_to__gte=35&sort=age_group_from asc

suthugotyolo avatar Apr 14 '20 15:04 suthugotyolo