1. Search experiences

Widgets

Version:

After Sitecore Search indexes your website content, you can request content from Sitecore Search using widgets. A widget is a configurable page component that collects and transmits the search-related content to your app.

For example, when you create a search results page, you request data for a search results widget using the SearchWidgetItem class. The following example describes a request for 10 index documents in the Product entity, including all the available attributes. The getWidgetData function makes the data request.

import { SearchWidgetItem } from "@sitecore-cloudsdk/search/browser";

// Configure the search request:
const widgetRequest = new SearchWidgetItem("product", "rfkid_7"); // Create a new widget request for the "Product" entity
widgetRequest.content = {}; // Request all attributes for the entity
widgetRequest.limit = 10; // Limit the number of results to 10

// ...

// Call the `getWidgetData` function to request the data:
const response = await getWidgetData(
  new WidgetRequestData([widgetRequest]),
);

Here's an example response, including attributes for a shoes index document:

{
  "brand": "Sneakers4All",
  "id": "id-12345",
  "image_url": "https://...",
  "name": "Men's Trainers",
  "price": "79.99",
  "source_id": "10569",
  "in_stock": true,
  "review_count": "...",
  "sku": "...",
  ...
}

You can use the SearchOptions interface to configure your request. For example, you could specify certain attributes to be returned in the response, add personalization options, or change the search limit and offset to paginate the results.

Widget types

There are different types of widgets, each used to create a distinct search experience. The getWidgetData function lets you request data for the following widget types:

  • Search results widget - used for requesting search results data. This data is typically displayed on a search results page.

  • Recommendation widget - used for requesting data for content or product recommendations.

  • Suggestion widget - the suggestion parameter of the search results widget is used for requesting data for search suggestions. This data is typically displayed on a search results page, below the search field where the site visitor enters text.

  • Questions-answers widget - used for requesting data for questions and answers. For example, when the site visitor enters a question in the search field and then performs the search, you can display a questions-answers block listing the question, the answer, and related questions.

Tip

It's common to request data for multiple widgets in a single request. For example, when the site visitor enters text in the search field, you can display the contents of a search results widget, a recommendation widget, and a suggestion widget at the same time.

Widgets are not restricted to a specific entity, so you can reuse them across your app. For example, you can use the "rfkid_7" search results widget in the Product, Content, and any other entity.

Capturing events

After setting up a widget, you can capture events that track when the site visitor views or clicks the associated user interface element.

The captured event data is sent to Sitecore and becomes available in Sitecore Search. This data lets you personalize search results and track the performance of your search experiences so you can refine and improve them.

For more information, see Sitecore Search events.

If you have suggestions for improving this article, let us know!