Sitecore Discover - Part 3

Integrating Using Search JS Data Package

Sitecore Search is a headless search engine that enables personalized experiences for visitors. It performs entity-based searches by indexing searchable content and managing these indexes as entities—distinct, search-indexable object types with unique structures that represent various content categories.

The SearchJS Data package can be utilized in any JavaScript project, regardless of framework or flavor, to build search experiences that are configured within Sitecore Search.

Integration architecture:

In your application, which integrates with Sitecore Search using the JS Data package, communication with Search services is handled through a singleton instance called DataProvider. At runtime, DataProvider processes requests constructed with various methods, objects, and types, and returns responses in predefined formats.


Install the JS Data package:

To start development, you'll need to install the JS Data package from NPM. You can accomplish this either through your IDE or by using the terminal.

To install Search JS Data package to your project:

·       In your project Location (C:\Sitecore Serach), in the terminal, run the following:

·       npm install --save @sitecore-search/data


Note: If You Are facing Error Like npm : The term ‘npm' is not recognized as the name of a cmdlet, function, script file, or operable program.

Solution: Go Through/Click On given Solution Urls

Authenticate a Session:

All requests to Search require authentication. You can use the built-in setup method to authenticate your session.

1.     In a TypeScript file named index.ts, copy and paste the following code block.

Location: C:\Sitecore Serach\node_modules\type-fest

import {

  setup,

} from "@sitecore-search/data";

setup({

  customerKey: '<YOUR_CUSTOMER_KEY>',

  apiKey:'<YOUR_API_KEY>',

});

import {

  setup,

} from "@sitecore-search/data";

setup({

  customerKey: '196452776-6854',

  apiKey:'01-cb2539b5-0bbeebadec52f425',

});

1.     In the Customer Engagement Console (CEC), in the Developer Resources section, copy your Customer key and API key values.

2.     In the index.ts file, in the setup function, replace <YOUR_CUSTOMER_KEY> and <YOUR_API_KEY> with the vales you copied for the Customer key and the API key.

Make a widget request object:

Widget-specific parameters are transmitted to Search via WidgetRequest objects.

1.     In the index.ts file, to create widget request objects of widget and entity pairs, paste the following code block, then adjust the imports: 

import {

  setup,

  WidgetRequest,

} from "@sitecore-search/data";

const searchResultsidgetRequest = new WidgetRequest("rfkid_7");

searchResultsWidgetRequest.setEntity("content");

const previewSearchWidgetRequest = new WidgetRequest("rfkid_6");

previewSearchWidgetRequest.setEntity("content");

1.     In the WidgetRequest objects, to set pagination parameters, paste the following code block, then adjust the imports:

searchResultsWidgetRequest.setSearchLimit(10); 

searchResultsWidgetRequest.setSearchOffset(30);

previewSearchWidgetRequest.setSearchLimit(10); 

previewSearchWidgetRequest.setSearchOffset(30);

Make a search request:

The Request object containing context and widget information, is sent to Search. Its parameters include those that apply to all included widgets.

1.     In the index.ts file, to create a request with locale as context, paste the following code block in the file, then adjust the imports.

import {

setup,

Request,

} from "@sitecore-search/data";

const request = new Request();

request.setContextLocaleCountry("us");

request.setContextLocaleLanguage("en");


1.     In the request object, to add WidgetRequest objects individually, paste the following code block, then adjust the imports.

request.addWidgetItem(searchResultsWidgetRequest.toJson());

request.addWidgetItem(previewSearchWidgetRequest.toJson());

2.     Alternatively, in the request object, to add an array of WidgetRequest objects, paste the following code block, then adjust the imports.

request.setWidgetItems( [ searchResultsWidgetRequest.toJson(), previewSearchWidgetRequest.toJson() ] );


Dispatch a search request: 

The DataProvider is a singleton that orchestrates all communication between the UI and Search.

1.     In the index.ts file, paste the following code block, then adjust the imports.

import {

  setup,

  DataProvider,

  Request,

  WidgetRequest,

} from "@sitecore-search/data";

DataProvider.get(request.toJson())

  .then((response) => console.log(JSON.stringify(response, null, 4)))

  .catch((error) => console.log(JSON.stringify(error, null, 4)));


For More Go Through : Integrating using Search JS Data package

For More Details Read My Next Blog

Know More About Sitecore Discover

=====================================================

Thanks For Reading My Blogs

You Can Also Continue Your Learning By Reading These More Blogs - 

 


Comments

Read More Blogs Here....

Sitecore JavaScript Services

LaMDA Software

Component In Sitecore / Create One Column Component In Sitecore:

Blockchain Technology

Sitecore PowerShell : Part 1

Contact Me/Any Suggestion

Name

Email *

Message *