Implementing Search page
Configuration
To create a search application, call the init
function with your configuration. This will create a new Preact application that renders on the specified contentCssSelector
. It will also bind to the input element identified by the provided inputCssSelector
and execute a search upon form submission.
Serp query parameter flavors
In the example above, we supply serp query parameters as an object. Additionally, the serpQuery
parameter can also be supplied as a function. The function flavor can be used for building complex query parameters and provides access to other pre-defined configuration parameters. Section below shows an example of serpQuery
supplied as a function which provides the product variation id by accessing the pre-defined variationId
method from the default configuration.
The full list of Configuration options is documented here
Search page path
When serpPath
parameter is specified, the application will redirect to the specified search page after a search is conducted. Otherwise, the search will be rendered on the same page without changing the URL path.
Search page redirect
When serpPathRedirect
parameter is set to true
, the application after search submission will redirect the page to the search page specified in serpPath
and fetch the page. Default behaviour will rewrite browser history only to the specified path, without fetching the page.
Unbinding existing search input
To prevent events from firing on an existing input, you need to provide the CSS selector of the form that the input is in to the initialization configuration. When optional fromCssSelector
is passed, it will unbind the form and the elements inside from existing events. Additionally, formUnbindDelay
in milliseconds as value can be passed to delay the unbinding functionality.
Serp component
The search results page component should render a full search page using the provided app state. A minimal example might look like this:
Automatic URL Parameter Compression
When the compressUrlParameters
flag is set to true
, it automatically applies the URL parameter compression functions for filters, sort and pagination.
@nosto/preact
library has pre-built functions for changing search url format:
Description | Example | |
---|---|---|
| Replaces | Before:
|
| Returns shorter | Before:
|
| Compresses | Before:
|
Query parameter mapping
In addition to the compressUrlParameters
flag the serpUrlMapping
should be used to control the mapping from URL parameter keys to paths in the internal query object. The default looks like this:
The key is the internal path in the query model and the value is the query parameter name that should be used in the URL.
Features
Faceted navigation
Stats facet
The stats facet returns the minimum and maximum values of numerical fields from search results. This functionality is especially useful when creating interactive elements such as sliders and range selectors. For instance, a price slider can use these min-max values to define its adjustable range, providing a simple way for users to filter products within a specific price range. Similarly, these values are utilized in the RangeSelector to define the overall scope of range selections, allowing for the configuration of selection precision through the range size parameter.
Range Slider
Utilize the useRangeSlider
hook to generate useful context for rendering range inputs. Additionally, employ the component to generate the interactive slider itself. These tools together facilitate the creation of dynamic and interactive range sliders for your application.
Range Selector
If you require an alternative method where values are selected through radio buttons rather than a slider, consider using useRangeSelector
hook. This tool allows users to choose from predefined range intervals with radio buttons, offering a different interaction style.
The range size parameter in the useRangeSelector
hook specifies the size of each interval in the range and determines the total number of range items displayed. Additionally, it automatically rounds the minimum value down to ensure intervals are aligned with the specified range size.
For example, if the minimum product price in the current catalog is 230, and the maximum product price is 1000, the range size of 200 will adjust the starting point to 200 and create intervals displayed under the "Price" filter as follows:
200 - 400
400 - 600
600 - 800
800 - 1000
Terms facet
The terms facet returns field terms for all products found in the search. This feature analyzes the content of each product and extracts meaningful terms. These terms can then be used to filter or refine search results, providing users with a more accurate and targeted product search.
You can use the toggleProductFilter
function to toggle any filter value. This function will either add the filter value if it's not already applied or remove it if it's currently active, thus providing an efficient way to manipulate product filters in your application.
Pagination
Use the usePagination
hook to generate useful context for rendering any desired pagination. Utilize the width parameter to adjust how many page options should be visible. Also, remember to scroll to the top on each page change to ensure a seamless navigation experience for users.
Infinite Scroll
Nosto search-templates library provides a simple out-of-the-box solution to implement infinite scroll functionality. Simply wrapping your product rendering with the <InfiniteScroll>
component is generally enough.
As the user scrolls the page down, the wrapper will detect it using the IntersectionObserver
. If it is not supported by the user's browser, a 'load more' button will be shown instead.
Persistent Search Cache
When using infinite scroll, consider enabling persistent search cache as well. When this feature is enabled, the latest search API response will be automatically cached and stored in the browser's session storage.
This improves the user experience significantly when the user navigates from a product details page back into the search results using the browser's 'back' function. The data necessary to display the products is already available, and the user will see the products immediately, without waiting for them to load again.
This feature is useful for both paginated and infinite scroll, but the benefits are significantly more visible with the latter.
Product actions
Since the code editor utilizes the Preact framework, it offers significant flexibility in customizing behavior or integrating the search page with existing elements on your site. For instance, you can implement actions such as 'Add to Cart', 'Wishlist', or 'Quick View'.
Handling native results
Nosto will attempt to display the original search results in case Nosto service is unavailable or can't be reached. In addition, the original products are made available for the SEO crawlers, improving the page's ranking in the search engines. To make it possible, it's recommended to hide the original search results instead of removing or blocking them.
The best approach is to add ns-content-hidden
class name to the same element you are targeting with contentCssSelector
or categoryCssSelector
. This class name will be stripped away by Nosto automatically as soon as the script is initialized.
In addition, you should define CSS to hide the target element:
Analytics
Search automatically tracks to Google Analytics & Nosto Analytics when using SerpElement
component.
Component parameters:
hit | Product object. |
as | Element to render |
onClick (optional) | Additional onClick callback (tracking callback is already implemented in the component). |
The SerpElement
component supports any other HTML attribute, e.g. class.
Fallback Functionality
The search page incorporates built-in fallback functionality, allowing users to customize the behavior in case the search service encounters issues. To activate this feature, modify the initialization configuration to include the fallback: true
key-value pair.
Enabling Fallback
To enable fallback functionality, include the following code in the initialization configuration:
Once fallback is enabled, if the search request fails to retrieve data, the search functionality will be temporarily disabled for 10 minutes, and the original content Nosto has overridden will be restored.
Alternative Fallback Behaviour
If the behaviour described above is undesirable, the configuration supports an alternative option. Fallback mode can be set to fallback: 'legacy'
, in which case the user will see a page reload if the search request fails. After that, Nosto will not attempt to override the original search results or category pages for 10 minutes.
This behaviour has been the default fallback behaviour before August 20, 2024.
Customizing Fallback Location
Additionally, it's possible to customize the location to which users are redirected when the search functionality is unavailable. This customization involves specifying functions for both the search engine results page (SERP) and category pages.
SERP Fallback
To redirect users to a specific location when the search engine is down, define a function for serpFallback
. This function accepts one parameter containing information about the current search query, including the query itself.
Category Fallback
Similarly, for category pages, define a function for categoryFallback
. This function also accepts one parameter containing information about the current query, including the category ID or Path.
By customizing these fallback locations, you can enhance the user experience by providing them with alternative navigation options if the search functionality is temporarily unavailable.
Search engine configuration
Nosto Search engine is relevant out of the box and search API can be used without any initial setup. Nosto Dashboard can be used to further tune search engine configuration:
Searchable Fields - manage which fields are used for search and their priorities,
Facets - create facets (filtering options) for search results page,
Ranking and Personalization Ranking and Personalization - manage how results are ranked,
Synonyms, Redirects, and other search features are also managed through Nosto Dashboard (my.nosto.com).
Last updated