how to sort by field (eg. rating) and if equal then by relevance (score)

38 views
Skip to first unread message

Muayyad AlSadi

unread,
Nov 16, 2011, 4:25:21 AM11/16/11
to whoosh
hi,

let's assume I have two fields book title and book rating

some one searched for "how to learn python programming" (with OR operator)
I want to give the user the option to sort by rating yet if both have same rating then to consider better match


Matt Chaput

unread,
Nov 16, 2011, 12:48:56 PM11/16/11
to who...@googlegroups.com

This should work, assuming "rating" is a numeric field:

from whoosh import sorting

facet = sorting.MultiFacet(["rating", sorting.ScoreFacet()])
results = searcher.search(myquery, sortedby=facet)

Hope this helps :)

Matt

Matt Chaput

unread,
Nov 16, 2011, 12:58:33 PM11/16/11
to who...@googlegroups.com
On 16/11/2011 12:48 PM, Matt Chaput wrote:
> facet = sorting.MultiFacet(["rating", sorting.ScoreFacet()])
> results = searcher.search(myquery, sortedby=facet)

I probably should have reversed the rating field, assuming rating=5 is
better than rating=1...

facet = sorting.MultiFacet([sorting.FieldFacet("rating", reverse=True),
sorting.ScoreFacet()])

Matt

Muayyad AlSadi

unread,
Nov 18, 2011, 7:57:10 AM11/18/11
to who...@googlegroups.com
sortedby = []
sortedby.append(FieldFacet('rating', reverse=True))
sortedby.append(ScoreFacet())
results = searcher.search(myquery, sortedby=sortedby)




Matt

--
You received this message because you are subscribed to the Google Groups "Whoosh" group.
To post to this group, send email to who...@googlegroups.com.
To unsubscribe from this group, send email to whoosh+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/whoosh?hl=en.


Muayyad AlSadi

unread,
Nov 21, 2011, 4:22:17 PM11/21/11
to who...@googlegroups.com
I meant to say how about using list like this


sortedby = []
sortedby.append(FieldFacet('rating', reverse=True))
sortedby.append(ScoreFacet())
results = searcher.search(myquery, sortedby=sortedby)

what is the difference between the two

Matt Chaput

unread,
Nov 21, 2011, 4:35:09 PM11/21/11
to who...@googlegroups.com

There should be no difference... if you pass a list to sortedby Whoosh
will automatically wrap it with a MultiFacet object.

Matt

Reply all
Reply to author
Forward
0 new messages