From 1efcba2e216c2aa632a088d867105bd370effd84 Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Thu, 10 Oct 2024 13:20:59 -0400 Subject: [PATCH] feat(didyoumean): allow shifting queryCorrectionMode on the fly (#4305) https://coveord.atlassian.net/browse/KIT-3482 --------- Co-authored-by: Felix Perron-Brault --- .../atomic-did-you-mean.tsx | 7 ++++++- .../headless-core-did-you-mean.test.ts | 9 +++++++++ .../did-you-mean/headless-core-did-you-mean.ts | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/atomic/src/components/search/atomic-did-you-mean/atomic-did-you-mean.tsx b/packages/atomic/src/components/search/atomic-did-you-mean/atomic-did-you-mean.tsx index eb6d510c8f9..565a7cb04be 100644 --- a/packages/atomic/src/components/search/atomic-did-you-mean/atomic-did-you-mean.tsx +++ b/packages/atomic/src/components/search/atomic-did-you-mean/atomic-did-you-mean.tsx @@ -6,7 +6,7 @@ import { buildQueryTrigger, QueryTriggerState, } from '@coveo/headless'; -import {Component, h, Prop, State} from '@stencil/core'; +import {Component, h, Prop, State, Watch} from '@stencil/core'; import { BindStateToController, InitializableComponent, @@ -68,6 +68,11 @@ export class AtomicDidYouMean implements InitializableComponent { @Prop({reflect: true}) public queryCorrectionMode: 'legacy' | 'next' = 'next'; + @Watch('queryCorrectionMode') + public updateQueryCorrectionMode() { + this.didYouMean.updateQueryCorrectionMode(this.queryCorrectionMode); + } + public initialize() { this.didYouMean = buildDidYouMean(this.bindings.engine, { options: { diff --git a/packages/headless/src/controllers/core/did-you-mean/headless-core-did-you-mean.test.ts b/packages/headless/src/controllers/core/did-you-mean/headless-core-did-you-mean.test.ts index dcc1b3f11cc..8acd164094a 100644 --- a/packages/headless/src/controllers/core/did-you-mean/headless-core-did-you-mean.test.ts +++ b/packages/headless/src/controllers/core/did-you-mean/headless-core-did-you-mean.test.ts @@ -71,6 +71,15 @@ describe('did you mean', () => { expect(applyDidYouMeanCorrection).toHaveBeenCalledWith('bar'); }); + it('should allow to update the query correction mode', () => { + const initialState = createMockState(); + initialState.didYouMean.queryCorrectionMode = 'legacy'; + initDidYouMean({}, initialState); + dym.updateQueryCorrectionMode('next'); + + expect(engine.dispatch).toHaveBeenCalledWith(setCorrectionMode('next')); + }); + it('should dispatch disableAutomaticQueryCorrection at initialization when specified', () => { initDidYouMean({options: {automaticallyCorrectQuery: false}}); expect(disableAutomaticQueryCorrection).toHaveBeenCalledTimes(1); diff --git a/packages/headless/src/controllers/core/did-you-mean/headless-core-did-you-mean.ts b/packages/headless/src/controllers/core/did-you-mean/headless-core-did-you-mean.ts index 744058a678a..4e341cbf93d 100644 --- a/packages/headless/src/controllers/core/did-you-mean/headless-core-did-you-mean.ts +++ b/packages/headless/src/controllers/core/did-you-mean/headless-core-did-you-mean.ts @@ -28,6 +28,8 @@ export interface DidYouMeanProps { options?: DidYouMeanOptions; } +type QueryCorrectionMode = 'legacy' | 'next'; + export interface DidYouMeanOptions { /** * Whether to automatically apply corrections for queries that would otherwise return no results. @@ -46,7 +48,7 @@ export interface DidYouMeanOptions { * * Default value is `next`. */ - queryCorrectionMode?: 'legacy' | 'next'; + queryCorrectionMode?: QueryCorrectionMode; } export interface DidYouMean extends Controller { /** @@ -54,6 +56,17 @@ export interface DidYouMean extends Controller { */ applyCorrection(): void; + /** + * Update which query correction system to use + * + * `legacy`: Query correction is powered by the legacy index system. This system relies on an algorithm using solely the index content to compute the suggested terms. + * `next`: Query correction is powered by a machine learning system, requiring a valid query suggestion model configured in your Coveo environment to function properly. This system relies on machine learning algorithms to compute the suggested terms. + * + * @param queryCorrectionMode - the query correction mode to use + * + */ + updateQueryCorrectionMode(queryCorrectionMode: QueryCorrectionMode): void; + /** * The state of the `DidYouMean` controller. */ @@ -138,6 +151,9 @@ export function buildCoreDidYouMean( applyDidYouMeanCorrection(this.state.queryCorrection.correctedQuery) ); }, + updateQueryCorrectionMode(queryCorrectionMode: QueryCorrectionMode) { + dispatch(setCorrectionMode(queryCorrectionMode)); + }, }; }