craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 635 forks source link

Sort search results by multiple parameters #6901

Closed Saboteur777 closed 4 years ago

Saboteur777 commented 4 years ago

Description

It would be beneficial if we could sort search results by additional parameters alongside their score. Right now you can do this:

{% set entries = craft.entries.search('trolo').orderBy('score DESC') %}

OR this:

{% set entries = craft.entries.search('trolo').orderBy('postDate DESC, customFieldHandle ASC') %}

But not this:

{% set entries = craft.entries.search('trolo').orderBy('score DESC, postDate DESC') %}
darylknight commented 4 years ago

I don't understand. Aren't the second and third examples the same thing?

Saboteur777 commented 4 years ago

@darylknight No, they are different queries: the first orders the results by their relevancy score, the second orders the results by their postDate and their custom field's value (in descending and ascending orders, respectively), and the third orders the results by their relevancy score and their postDate.

They are effectively the same, but the third is not possible right now. (confirmed by @angrybrad)

FFR: score: https://craftcms.com/docs/3.x/searching.html#ordering-results-by-score

brandonkelly commented 4 years ago

It wouldn’t be possible to mix them, as score is a special case, which is actually sorted by PHP code rather than by the DB.