FieldSuggestions

This is for:

Developer

Example implementation

field-suggestions.fn.tsx

import {FieldSuggestions as HeadlessFieldSuggestions} from '@coveo/headless';
import {useEffect, useState, FunctionComponent} from 'react';

interface FieldSuggestionsProps {
  controller: HeadlessFieldSuggestions;
}

export const FieldSuggestions: FunctionComponent<FieldSuggestionsProps> = (
  props
) => {
  const {controller} = props;
  const [state, setState] = useState(controller.state);

  useEffect(() => controller.subscribe(() => setState(controller.state)), []);

  const onInput = (text: string) => {
    if (text === '') {
      controller.clear();
      return;
    }
    controller.updateText(text);
  };

  return (
    <div>
      <input onInput={(e) => onInput(e.currentTarget.value)} />
      <ul>
        {state.values.map((facetSearchValue) => (
          <li
            key={facetSearchValue.rawValue}
            onClick={() => controller.select(facetSearchValue)}
          >
            {facetSearchValue.displayValue} ({facetSearchValue.count} results)
          </li>
        ))}
      </ul>
    </div>
  );
};

// usage

/**
 * ```tsx
 * const facetOptions: FacetOptions = {field: 'author'};
 * const options: FieldSuggestionsOptions = {facet: facetOptions};
 * const controller = buildFieldSuggestions(engine, {options});
 *
 * <FieldSuggestions controller={controller} />;
 * ```
 */

The FieldSuggestions controller provides query suggestions based on a particular facet field.

For example, you could use this controller to provide auto-completion suggestions while the end user is typing an item title.

This controller is a wrapper around the basic facet controller search functionality, and thus exposes similar options and properties.

Methods

clear

Resets the query and empties the suggestions.

Requests field suggestions based on a query.

select

Filters the search using the specified value.

If a facet exists with the configured facetId, selects the corresponding facet value.

Parameters

showMoreResults

Shows more field suggestions for the current query.

singleSelect

Filters the search using the specified value, deselecting others.

If a facet exists with the configured facetId, selects the corresponding facet value while deselecting other facet values.

Parameters

updateCaptions

Updates the captions of field suggestions.

Parameters

  • captions: Record<string, string>

    A dictionary that maps field values to field suggestion display names.

updateText

Requests field suggestions based on a query.

Parameters

  • text: string

    The query to search.

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

Properties

  • isLoading: boolean

    Whether the request for field suggestions is in progress.

  • moreValuesAvailable: boolean

    Whether more field suggestions are available.

  • query: string

    The query used to request field suggestions.

  • values: FieldSuggestionsValue[]

    The field suggestions.

Initialize

buildFieldSuggestions

Creates a FieldSuggestions controller instance.

Parameters

  • engine: SearchEngine

    The headless engine.

  • props: FieldSuggestionsProps

    The configurable FieldSuggestions controller properties.

Returns FieldSuggestions

FieldSuggestionsProps

The configurable FieldSuggestions controller properties.

Properties

FieldSuggestionsOptions

The options for the FieldSuggestions controller.

Properties

  • field: string

    The field for which you wish to get field suggestions.

  • allowedValues?: string[]

  • delimitingCharacter?: string

  • facetId?: string

    A unique identifier for the controller. By default, a random unique identifier is generated.

    If a facet shares the same id, then its values are going to be selectable with select and singleSelect.

  • facetSearch?: FieldSuggestionsFacetSearchOptions

    The options related to search.

  • filterFacetCount?: boolean

    Whether to exclude the parents of folded results when estimating the result count for each field suggestion.

    Default: true

  • hasBreadcrumbs?: boolean

  • injectionDepth?: number

  • numberOfValues?: number

    This option has no effect.

  • sortCriteria?: 'score' | 'alphanumeric' | 'occurrences' | 'automatic'

    The criterion to use to sort returned field suggestions. Learn more about sortCriteria values and the default behavior of specific facets in the Search API documentation.

    Default: automatic

FieldSuggestionsFacetSearchOptions

Properties

  • captions?: Record<string, string>

    A dictionary that maps field values to field suggestion display names.

  • numberOfValues?: number

    The maximum number of suggestions to request.

    Default: 10

  • query?: string

    The query with which to request field suggestions.

FieldSuggestionsValue

Properties

  • count: number

    An estimated number of result items matching both the current query and the filter expression that would get generated if this field suggestion was selected.

  • displayValue: string

    The custom field suggestion display name, as specified in the captions argument of the FieldSuggestion controller.

  • rawValue: string

    The original field value, as retrieved from the field in the index.

Unsubscribe

Call signatures

  • (): void