Create a request to get preview search data
This topic shows you how to make a call to the Sitecore Discover Search and Recommendation API to get a preview search widget in the following scenario:
-
The user enters the keyphrase pink in the search field.
-
You want to show 3 product suggestions in the search results area (
n_item=3). -
You want to show a maximum of 4 keyphrase suggestions in the autocomplete area.
To create a request to get preview search data:
-
In the header, add cookies and, if required, authentication details.
-
Add page context and user context. For page context, include the page
uriand locale information (if applicable). For user context, include either theuuidor theuser_ID.RequestResponse{ "context": { "page": { "uri": "/" }, "user": { "uuid": "sampleUUID" } }, } -
Add widget information. Use the
rfkidof the preview search widget.RequestResponse{ "widget": { "rfkid": "rfkid_6" }, } -
Specify the number of items you want to see in the preview search results.
NoteYou do not have to specify the page number in your request because preview search results usually do not have more than one page.
Here is an example:
RequestResponse{ "n_item": 3, } -
Add query information, that is, the keywords the user searches for.
RequestResponse{ "query": { "keyphrase": { "value": [ "pink" ] } }, } -
Add suggestion information to tell Discover that you want a maximum of 4 keyphrase suggestions for what the user types.
RequestResponse"suggestion": { "keyphrase": { "max": 4 } } -
Request product content. Here, we want the entire product.
RequestResponse"content": { "product": {} },
You can see the complete request with all objects put together and the corresponding response in the sample section.