How can I change the sort order of search results?


I would like my search results to be sorted differently from the default. How can I do this?

By default, Swiftype sorts results by relevance so that the most likely search results are at the top.

You can change how relevance is determined by boosting a field’s weight. For example, if you use a Swiftype crawler-based search engine and you want to increase the importance of matches in the title field, add a boost to the page DocumentType:

  "q": "example",
  "search_fields": {"page": ["title^3", "sections", "body"]

Keep in mind that if you do not specify search_fields Swiftype will use all string and text fields defined in your Document Type. When specifying boosted fields, it’s important to include all the other fields you also want to be searched – otherwise matches in those fields won’t be included in the results. (For a list of all the fields in the page DocumentType, see the documentation.)

Read more about field weights.

Another way to change the relevance calculation is to apply a functional boost. Functional boosts can only be applied to numeric fields, so this is mostly useful for customers who are indexing their own content with the Swiftype API instead of using the Swiftype web crawler.

You can also change the order of results by specifying a sort_field and sort_direction. This will override the default relevance based sort. All results returned will match the search, but the results will be sorted in the order you specify.

For example, if you have a DocumentType named posts that has a date field named timestamp, you can sort the results so the newest documents are returned first:

  "q": "example",
  "sort_field": {"posts": "timestamp"},
  "sort_direction": {"posts": "desc"}

Note that you can only sort on date, enum, float, and integer fields.

To learn more about sorting you can refer to this documentation.