Search API
The Search API that uses the SearchService class allows Content SDK apps to query SitecoreAI search indexes to create tailored search experiences for different content types and user needs. This enables quick retrieval of content items and operations such as sorting, paginating, limiting results, etc. It abstracts the underlying search implementation and provides a consistent API.
The API has the following capabilities:
-
Use generic types to model documents for better IDE support and type safety
-
Sort results and use dot notation for nested properties
-
Limit and paginate results
-
Use custom fetch options for advanced use cases
Configuration
To initialize the SearchService class, you can create an instance of it using a configuration object as shown.
The object expects the SITECORE_EDGE_CONTEXT_ID that you can retrieve from the Developer settings tab of your environment in the SitecoreAI Deploy App. After initialization, you can use the search method of the SearchService to retrieve results from SitecoreAI.
Supported parameters
The search method expects the ID of the search index and also supports several other parameters used to sort, paginate, limit, and offset results.
|
Parameter |
Type |
Description |
|---|---|---|
|
|
string |
The ID of the SitecoreAI search index to retrieve results from. Required |
|
|
string |
Use phrases to search the index for specific content. |
|
|
Object or Array of objects |
Specify an object or array of objects containing the name and order to sort results by. |
|
|
number |
Specify the limit to restrict the number of results returned. |
|
|
number |
Specify a number to drop that many results. Use with |
Examples
The following sections demonstrate examples using the SearchService for common scenarios.
Simple search
A basic search only requires the searchIndexId. The default limit is 10, so it returns only 10 results.
Search with keyphrase
You can also search using a keyphrase as shown:
Search with a generic type
The SearchService supports TypeScript generics, allowing you to define the shape of your search document for type safety and better IDE support.
The following example demonstrates using a type called Article to retrieve results and display its contents. This ensures predictable results.
Search with basic sorting
The SearchService supports sorting results using a single object specifying the name and order. The following example sorts the search results by the publishDate in the descending order (most recent first).
Search with multi-field sorting
You can also specify multiple fields to sort. Instead of a single object, you should specify this as an array of objects. The following example sorts the search results by category in the ascending order, followed by price in ascending order, and finally by rating in the descending order.
Search with type sorting
You can sort fields when you're using generics. TypeScript verifies the existence of the sort field in this case.
Search with nested sorting
Using dot notation (.), you can sort nested properties.
Search with limited results
To limit the number of results returned, you can use the limit property. The default value is 10 and the maximum value allowed is 500.
Search with pagination
Use the offset parameter to implement pagination. Combined with limit, you can fetch results in pages.
Search with custom fetch options
The SearchService allows you to customize the underlying fetch request by passing a RequestInit object as the second parameter. This is useful for adding custom headers, setting timeouts, or configuring other request options.
Custom headers
You can set custom headers using the headers object containing key-value pairs of the header name and value.
Request timeout
You can set a custom request timeout using the signal object that references the AbortController's signal property.
Error handling
The SearchService throws specific errors that you should handle appropriately: