Create a request to get search page data

This topic shows you how to make a call to the Sitecore Discover Search and Recommendation API to get a search page widget in the following scenario:

  • You want to show one result on each search result page (n_item = 1).

    Note

    For brevity, this example shows a request with one result per search page. You can change the value of n_item to, say, 12, and use the rest of the code.

  • You want to make a call to get data for the first page of the search page widget (page_number =1).

  • The user searches for the keyword sanda, a misspelling of sandal. You want to show the user one corrected suggestion.

  • The user must be able to sort products by customer ratings.

Note

Use the API explorer to try out requests and see sample responses.

To create a request to get search page data:

  1. In the header, add cookies and, if required, authentication details.

  2. In the request body, add page context and user context. For page context, include the page uri and locale information (if applicable). For user context, include either the uuid or the user_ID.

    RequestResponse
    {
    	"context": {
    		"page": {
    			"uri": "/search"
    		},
    		"user": {
    			"uuid": "sampleUUID"
    		}
    	}
    }
  3. Add widget information. Use the rfkid of the search page widget.

    RequestResponse
    {
        "widget": {
            "rfkid": "rfkid_7"
        },
    }
  4. Add pagination information. Specify the number of items (n_item) you want to see in one page of search results. Also, specify the page for which (page_number) you are making the call.

    RequestResponse
    {
        "n_item": 1,
        "page_number": 1,
    }
  5. Add query information, that is, the keywords that the user searches for.

    RequestResponse
    {
    "query": {
            "keyphrase": {
                "value": [
                    "sanda"
                ]
            }
        }
    }
  6. Add suggestion information to tell Discover how many suggestions you want to display if the user mistypes a keyword.

    RequestResponse
    {
        "suggestion": {
            "keyphrase": {
                "max": 1
            },
        }
    }
  7. Request information about the suggested keyword used if the user misspells something. To do this, use the request_for key. For details, see Related key

    RequestResponse
    {
        "request_for": [
            "query"
        ]
    }
    Note

    In the request_for response object, orig_keyphrase contains the original keyword and keyphrase is suggested by Discover.

  8. Add facet information to tell Discover how many facets you want.

    RequestResponse
    {
        "facet": {
            "all": true
        },
    }
  9. Add sort information to tell Discover how you want to sort.

    RequestResponse
    {
        "sort": {
            "value": [
                {
                    "name": "rating",
                    "order": "desc"
                }
            ]
        }
    }
    
  10. 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.

Do you have some feedback for us?

If you have suggestions for improving this article,