Context

This is for:

Developer

The Context controller injects custom contextual information into the search requests and usage analytics search events sent from a search interface.

Methods

add

Adds (or, if one is already present, replaces) a new context key-value pair.

Parameters

  • contextKey: string

    The context key to add.

  • contextValue: string | string[]

    The context value to add.

remove

Removes a context key from the query.

Parameters

  • key: string

    The context key to remove.

set

Sets the context for the query. This replaces any existing context with the new one.

Parameters

  • ctx: Record<string, ContextValue>

    The context to set for the query.

subscribe

Adds a callback that’s invoked on state change.

Parameters

  • listener: () ⇒ void

    A callback that’s invoked on state change.

Returns Unsubscribe: A function to remove the listener.

Attributes

state

The state of the Context controller.

Properties

  • values: Record<string, ContextValue>

    An object holding the context keys and their values.

Initialize

buildContext

Creates a Context controller instance.

Parameters

  • engine: CoreEngine

    The headless engine.

Returns Context

Unsubscribe

Call signatures

  • (): void

Example Implementation

context.ts

import {buildContext, SearchEngine} from '@coveo/headless';
 
export function setContext(
  engine: SearchEngine,
  ageGroup: string,
  interests: string[]
) {
  buildContext(engine!).set({
    ageGroup,
    interests,
  });
}

Associated Engines

The Context controller is available in the following engines: