Facet (Search Engine) (Deprecated)
Facet (Search Engine) (Deprecated)
|
Headless v1 has been deprecated. We recommend using the latest version of the Coveo Headless library. |
Example implementation
facet.fn.tsx
import {Facet as HeadlessFacet} from '@coveo/headless';
import {useEffect, useState, FunctionComponent} from 'react';
import {FacetSearch} from './facet-search';
interface FacetProps {
controller: HeadlessFacet;
}
export const Facet: FunctionComponent<FacetProps> = (props) => {
const {controller} = props;
const [state, setState] = useState(controller.state);
useEffect(() => controller.subscribe(() => setState(controller.state)), []);
if (!state.values.length) {
return <div>No facet values</div>;
}
return (
<ul>
<li>
<FacetSearch
controller={controller.facetSearch}
facetState={state.facetSearch}
isValueSelected={(facetSearchValue) =>
!!state.values.find(
(facetValue) =>
facetValue.value === facetSearchValue.displayValue &&
controller.isValueSelected(facetValue)
)
}
/>
</li>
<li>
<ul>
{state.values.map((value) => (
<li key={value.value}>
<input
type="checkbox"
checked={controller.isValueSelected(value)}
onChange={() => controller.toggleSelect(value)}
disabled={state.isLoading}
/>
{value.value} ({value.numberOfResults} results)
</li>
))}
</ul>
</li>
</ul>
);
};
// usage
/**
* ```tsx
* const options: FacetOptions = {field: 'objecttype'};
* const controller = buildFacet(engine, {options});
*
* <Facet controller={controller} />;
* ```
*/
facet-search.tsx
import {
FacetSearch as HeadlessFacetSearch,
FacetSearchState,
SpecificFacetSearchResult,
} from '@coveo/headless';
import {FunctionComponent} from 'react';
export interface FacetSearchProps {
controller: HeadlessFacetSearch;
facetState: FacetSearchState;
isValueSelected: (facetSearchValue: SpecificFacetSearchResult) => boolean;
}
export const FacetSearch: FunctionComponent<FacetSearchProps> = (props) => {
const onInput = (text: string) => {
props.controller.updateText(text);
props.controller.search();
};
return (
<div>
<input onInput={(e) => onInput(e.currentTarget.value)} />
<ul>
{props.facetState.values.map((facetSearchValue) => (
<li key={facetSearchValue.rawValue}>
<button
onClick={() => props.controller.select(facetSearchValue)}
disabled={props.isValueSelected(facetSearchValue)}
>
{facetSearchValue.displayValue} ({facetSearchValue.count} results)
</button>
</li>
))}
</ul>
</div>
);
};
Methods
isSortedBy
Checks whether the facet values are sorted according to the specified criterion.
Parameters
-
criterion:
'score' | 'alphanumeric' | 'occurrences' | 'automatic'
The criterion to compare.
Returns boolean
: Whether the facet values are sorted according to the specified criterion.
isValueSelected
Checks whether the specified facet value is selected.
Parameters
-
value:
FacetValue
The facet value to check.
Returns boolean
: Whether the specified facet value is selected.
showMoreValues
Increases the number of values displayed in the facet to the next multiple of the originally configured value.
sortBy
Sorts the facet values according to the specified criterion.
Parameters
-
criterion:
'score' | 'alphanumeric' | 'occurrences' | 'automatic'
The criterion to use for sorting values.
toggleSelect
Toggles the specified facet value.
Parameters
-
selection:
FacetValue
The facet value to toggle.
toggleSingleSelect
Toggles the specified facet value, deselecting others.
Parameters
-
selection:
FacetValue
The facet value to toggle.
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
facetSearch
Provides methods to search the facet’s values.
select
Selects a facet search result.
Parameters
-
value:
SpecificFacetSearchResult
The search result to select.
singleSelect
Selects a search result while deselecting facet values.
Parameters
-
value:
SpecificFacetSearchResult
The search result to select.
state
The state of the Facet
controller.
Properties
-
facetSearch:
FacetSearchState
The state of the facet’s searchbox.
-
canShowLessValues:
boolean
true
if fewer values can be displayed andfalse
otherwise. -
canShowMoreValues:
boolean
true
if there are more values to display andfalse
otherwise. -
enabled:
boolean
Whether the facet is enabled and its values are used to filter search results.
-
facetId:
string
The facet ID.
-
hasActiveValues:
boolean
true
if there’s at least one non-idle value andfalse
otherwise. -
isLoading:
boolean
true
if a search is in progress andfalse
otherwise. -
sortCriterion:
'score' | 'alphanumeric' | 'occurrences' | 'automatic'
The active sortCriterion of the facet.
-
values:
FacetValue[]
The values of the facet.
Initialize
buildFacet
Creates a Facet
controller instance.
Parameters
-
engine:
SearchEngine
The headless engine.
-
props:
FacetProps<FacetOptions>
The configurable
Facet
properties.
Returns Facet
Related types
FacetSearchState
Properties
-
isLoading:
boolean
true
if the facet search is in progress andfalse
otherwise. -
moreValuesAvailable:
boolean
Whether more values are available.
-
query:
string
The current query in the facet search box.
-
values:
SpecificFacetSearchResult[]
The facet search results.
FacetValue
Properties
-
numberOfResults:
number
The number of results that have the facet value.
-
state:
'idle' | 'selected'
Whether a facet value is filtering results (
selected
) or not (idle
). -
value:
string
The facet value.
SpecificFacetSearchResult
Properties
-
count:
number
An estimate of the number of result items matching both the current query and the filter expression that would get generated if the facet value were selected.
-
displayValue:
string
The custom facet value display name, as specified in the
captions
argument of the facet request. -
rawValue:
string
The original facet value, as retrieved from the field in the index.