Sort Search Results by Date, Price or Any Field

Solr API

Without a sort parameter, results come in order of relevance. sort orders them by a field instead.

01 · Examples

sort=score desc                      by relevance, the order you get without sort
sort=creation_date desc              newest first
sort=price_f asc,score desc          cheapest first; equal prices by relevance
sort=sent_com desc,score desc        most positive tone first

02 · Which fields can sort

A field needs docValues and one value per document. In the site search schema that means the dates and numbers such as creation_date, timestamp and price_f, the meta_* fields, content_type, and the single-valued suffixes *_s, *_i, *_l, *_f, *_dt and *_b. Text fields such as title and description cannot sort; for alphabetical order, send a copy of the title in a *_s field: dynamic fields.

03 · Add a tie-breaker

Documents with the same value in the sort field have no fixed order between them, so a page can repeat or skip results. Add score desc or id asc after the field.

04 · Newest first or fresh first

Sorting by date puts the newest page first even when it matches poorly. To lift newer pages while good matches stay on top, use a freshness boost instead.

05 · On the hosted search page

Visitors sort by the fields you turn on as facets: sorting results.