Skip to main content

searchCorrect

Typo-correction suggestions for a query, by the project's search dictionary.

Description​

Returns the typo-corrected form of a query without running the search, so a storefront can offer a "did you mean" suggestion (GET /api/content/search/correct). Resolves to an ISearchCorrectResponse: the original query, the corrected query (null when there is nothing to correct) and a per-word breakdown.

Each token carries from and to; to is null for a word that was left as is. The scoring fields (distance, docFreq, reason) are present only for a word that was actually corrected.

.searchCorrect(

, ,

);

Parameters schema​

Schema

query(required): string
Query to correct.
example: "fish ladr"

langCode: string
Language code. Default: "en_US". The dictionary is per language.
example: "en_US"

scope: string
Which record set the correction is drawn from. Omitted means the whole project.

Examples​

Minimal example​

const response = await Search.searchCorrect('prodct');

"Did you mean" suggestion​

const result = await Search.searchCorrect('fish ladr');

if ('statusCode' in result) {
console.error(result.message);
} else if (result.corrected) {
// Offer the corrected query to the user, then run the search on it.
console.log(`Did you mean: ${result.corrected}?`);
const products = await Search.globalSearch(result.corrected, ['products'], 'visible');
}

Example response​

{
"query": "fish ladr",
"corrected": "fish ladder",
"tokens": [
{
"from": "fish",
"to": null
},
{
"from": "ladr",
"to": "ladder",
"distance": 2,
"docFreq": 8,
"reason": "docFreq"
}
]
}

Response schema​

Schema: ISearchCorrectResponse

query: string
The query as it came in.
example: "fish ladr"

corrected: string | null
The corrected query, or null when there is nothing to correct.
example: "fish ladder"

tokens: ISearchCorrectToken[]
Per-word breakdown of the correction.

tokens.from: string
The word as it came in the query.
example: "ladr"

tokens.to: string | null
The correction, or null when the word was left as is.
example: "ladder"

tokens.distance: number
Number of edits (Damerau–Levenshtein, OSA) between from and to. Present only when the word was corrected.
example: 2

tokens.docFreq: number
In how many documents of the search scope the correction occurs. Present only when the word was corrected.
example: 8

tokens.reason: 'pair' | 'cooccurrence' | 'prefix' | 'docFreq'
How the correction was chosen. Present only when the word was corrected.
example: "docFreq"

An uncorrected word's token has only from and to (to: null); distance, docFreq and reason appear only on a corrected word. corrected is null when nothing in the query was changed.

The dictionary is per language. langCode defaults to the language set in defineOneEntry. To search on the corrected query, pass it to globalSearch (or set its correct flag to correct and search in one call).