Create a questions-answers widget
You can use the Cloud SDK search package to build search experiencessearch experiences, such as a questions-answers (Q&A) widget. This widget lets you show site visitors an AI-generated answer to their question, and optionally, more questions related to their query.
In this walkthrough, you'll create a simple Q&A widget in a Next.js app. Specifically, you'll request Q&A content from Sitecore Search and display it in the user interface.
The walkthrough describes the typical workflow for creating a questions-answers widget. After completing it, you can tailor the requests and the presentation of the content to your own needs for this Q&A widget, or build other types of search experiences.
This walkthrough describes how to:
-
Make sure the Questions & Answers capability is configured in Sitecore Search and that batch generation runs at least once. Batch generation automatically creates the
"rfkid_qa"widget, which is required for requesting questions and answers. -
In your code editor, open the root folder of your Next.js app. This walkthrough was tested on Next.js version 15, both for the Pages Router and the App Router.
-
Install and initialize the Cloud SDK and the
eventsandsearchpackages. -
Optionally, install Tailwind CSS in your app. This walkthrough uses Tailwind CSS to stylize the user interface, but it is not required to complete the walkthrough.
Request and display questions and answers
The first step to creating a Q&A widget is to request content from Sitecore Search. In Sitecore Search, widgetswidgets hold this content, so you will request data for a specific widget.
-
In your code editor, open the root folder of your Next.js app.
-
Depending on your router type:
-
If using the Pages Router - in the
pagesfolder, create a file calledquestions.tsx. -
If using the App Router - in the
appfolder, create a subfolder calledquestions. Then, in thequestionsfolder, create a file calledpage.tsx.
-
-
To import everything from the
searchpackage that's required to request data from Sitecore Search:-
If using the Pages Router - paste the following code into
questions.tsx: -
If using the App Router - paste the following code into
page.tsx:
-
-
Inside the default function, directly before the
returnstatement, paste the following code then save your changes:This code requests Q&A data from Sitecore Search. Specifically, the code:
-
Creates an object called
widgetRequestusing theQuestionsAnswersWidgetItemclass. This object describes a questions-answers widgetquestions-answers widget with the widget IDwidget ID of"rfkid_qa"and is configured to find an answer for the keyphrase "What is sitecoreai". It also retrieves maximum five other questions related to this query.XM Cloud is now SitecoreAISome code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
NoteAlways use
"rfkid_qa"to request a questions-answers widget. -
Runs the
getWidgetDatafunction to request data associated withwidgetRequest, and saves the data that Sitecore Search returns into theresponsevariable. -
Stores the returned main question and related questions, if any, in the
mainQuestionandrelatedQuestionsstate variables.
-
-
Update the
returnstatement to return the following, then save your changes:This code displays a loading message while retrieving data. After the data is retrieved, it iterates through the contents of the state variables and displays the main question and answer, followed by a list of related questions.
-
In your terminal, enter
npm run devto start your Next.js app, then open the page you created in your web browser. A main question and a list of related questions display. This is the content you requested from Sitecore Search. -
Open your web browser's developer tools and on the Network tab, start recording network activity.
You'll use the Network tab while building search experiences to check the network requests you make to Sitecore.
-
In your web browser, reload the page and then find a network request made to
edge-platform.sitecorecloud.io/v1/search. This is the request you made for the Q&A content.
Stylize the user interface
After requesting and displaying the content, you continue updating your code to stylize the user interface in your app.
To stylize the user interface:
Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
-
Depending on your router type:
-
If using the Pages Router - replace the contents of
questions.tsxwith the following, then save your changes: -
If using the App Router - replace the contents of
page.tsxwith the following, then save your changes:
This updated code replaces the basic HTML structure in the
returnstatement with styled components using Tailwind CSS classes. -
-
In your web browser, reload the Q&A page. The webpage now loads with Tailwind CSS styles applied.
Next steps
You've now successfully created a questions-answers widget.
Next, you can:
-
Learn about the concepts that will help you successfully build search experiences. For example, index documents and attributes, widgets, context, filters, and facets.
-
See examples of other types of search experiences to build, such as recommendations and search suggestions.
-
Learn more about tracking, what other events you can capture, and what the collected data can be used for.
-
Explore the reference documentation for the
searchpackage.