Sorting content
You can add a widget.items.search.sort object to your request to sort content, that is, determine the order in which they appear in the search results.
Before you can sort content, make sure that an administrator configures sorting options in Search.
When you use the widget.items.search.sort object, pass the name of the sorting option as the sort.value.name.
For the data model of this object, see the Search and Recommendation API reference .
In this example, you want to:
-
Sort all results in ascending alphabetical order. You have a previously created sorting option called
title. In the request, passtitleas thenameandascas theorder. -
Get only the title. For this use the
widget.items.search.contentobject and passtitlein the fields array.
The following code sample shows how to create a request:
{
"widget": {
"items": [
{
"rfk_id": "rfkid_7",
"entity": "content",
"search": {
"content": {
"fields": [
"title"
]
},
"sort": {
"value": [
{
"name": "title",
"order": "asc"
}
]
},
"limit": 3,
"offset": 0
}
}
]
},
"context": {
"locale": {
"country": "us",
"language": "en"
},
"user": {
"uuid": "159871551-1g-i4-4x-1p-f5tdhddqwvywv7b9g2vy-1664852644904"
}
}
The following is the response to the request:
{
"widgets": [
{
"rfk_id": "rfkid_7",
"entity": "content",
"content": [
{
"id": "https___www_sitecore_com_knowledge-center_blog_2019_10_commerce-twitter-chat",
"source_id": "388218",
"title": "#ReinventingCommerce"
},
{
"id": "https___www_sitecore_com_knowledge-center_blog_2019_10_commerce-twitter-chat",
"source_id": "407033",
"title": "#ReinventingCommerce"
},
{
"id": "https___www_sitecore_com_webinars_2019_modular-content-creation-for-better-omni-channel",
"source_id": "388218",
"title": "**You need modular content creation for better omnichannel**"
}
],
"total_item": 5858,
"limit": 3,
"offset": 0
}
],
"dt": 66,
"ts": 1669436439108
}In the response widgets.content object, you can see that all content is sorted by descending alphabetical order of their title.