SearchParameterManager

This is for:

Developer

The SearchParameterManager controller allows restoring parameters that affect the results from a url, for example.

Methods

subscribe

Adds a callback that will be called on state change.

Parameters

  • listener: () ⇒ void

    A callback to be invoked on state change.

Returns Unsubscribe: An unsubscribe function to remove the listener.

Attributes

state

The state relevant to the SearchParameterManager controller.

Properties

Initialize

buildSearchParameterManager

Creates a SearchParameterManager controller instance.

Parameters

  • engine: Engine<Partial<SearchParametersState]

    The headless engine.

  • props: SearchParameterManagerProps

    The configurable SearchParameterManager properties.

Returns SearchParameterManager

SearchParameterManagerProps

The configurable SearchParameterManager properties.

Properties

SearchParameterManagerInitialState

The initial state that should be applied to the SearchParameterManager controller.

Properties

SearchParameters

Properties

  • aq?: string

    The advanced query expression.

  • cf?: Record<string, string[]>

    A record of the category facets, where the key is the facet id, and value is an array containing the parts of the selected path.

  • cq?: string

    The constant query expression.

  • debug?: boolean

    Determines whether to return debug information for a query.

  • df?: Record<string, DateRangeRequest[]>

    A record of the date facets, where the key is the facet id, and value is an array containing the date ranges to request.

  • enableQuerySyntax?: boolean

    Whether to interpret advanced query syntax in the query.

  • f?: Record<string, string[]>

    A record of the facets, where the key is the facet id, and value is an array containing the selected values.

  • firstResult?: number

    A zero-based index of the first result.

  • nf?: Record<string, NumericRangeRequest[]>

    A record of the numeric facets, where the key is the facet id, and value is an array containing the numeric ranges to request.

  • numberOfResults?: number

    The number of results to return.

  • q?: string

    The query.

  • sortCriteria?: string

    The sort expression to order returned results by.

Unsubscribe

Call signatures

  • (): void

Example Implementation

search-parameter-manager.ts

import {
  buildSearchParameterManager,
  buildSearchParameterSerializer,
  Engine,
} from '@coveo/headless';
 
const {serialize, deserialize} = buildSearchParameterSerializer();
 
/**
 * Search parameters should not be restored until all components are registered.
 *
 * Additionally, a search should not be executed until search parameters are restored.
 */
export function bindSearchParametersToURI(engine: Engine) {
  const hash = window.location.hash.slice(1);
  const parameters = deserialize(decodeURIComponent(hash));
 
  const searchParameterManager = buildSearchParameterManager(engine, {
    initialState: {parameters},
  });
 
  return {
    autoUpdateURI: () =>
      searchParameterManager.subscribe(() => {
        window.location.hash = serialize(
          searchParameterManager.state.parameters
        );
      }),
  };
}