From 8669d183d5c24f3c9b081c2f087fa8c08f3f91d5 Mon Sep 17 00:00:00 2001 From: Canina Automation Date: Tue, 27 Oct 2026 11:49:15 +0000 Subject: [PATCH] feat(search): add bidirectional domain aliases (taurine/taurin, carnitine) and include ingredientCardSlug --- backend/src/common/utils/search.utils.ts | 36 ++++++++++++++++++++++-- backend/src/products/products.service.ts | 5 +++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/backend/src/common/utils/search.utils.ts b/backend/src/common/utils/search.utils.ts index 33b55a12..de433da6 100644 --- a/backend/src/common/utils/search.utils.ts +++ b/backend/src/common/utils/search.utils.ts @@ -42,6 +42,26 @@ export const COMMON_STOP_WORDS = new Set([ "the", "a", "an", "and", "or", "for", "with", "in", "on", "at", "to", "of", ]); +export const SEARCH_EXPANSION_MAP: Record = { + "تائورین": ["تورین", "taurine", "taurin"], + "تورین": ["تائورین", "taurine", "taurin"], + "taurine": ["تائورین", "تورین", "taurin"], + "taurin": ["تائورین", "تورین", "taurine"], + "کارنیتین": ["carnitine", "l-carnitine"], + "carnitine": ["کارنیتین", "l-carnitine"], + "گلوکزامین": ["گلوکوزامین", "glucosamine"], + "گلوکوزامین": ["گلوکزامین", "glucosamine"], + "glucosamine": ["گلوکوزامین", "گلوکزامین"], + "امگا": ["omega", "امگا ۳", "سالمون", "salmon"], + "omega": ["امگا", "امگا ۳", "salmon"], + "کندرویتین": ["chondroitin"], + "chondroitin": ["کندرویتین"], + "بیوتین": ["biotin"], + "biotin": ["بیوتین"], + "کلسیم": ["calcium"], + "calcium": ["کلسیم"], +}; + /** * Normalizes Persian and English text by standardizing characters, * removing diacritics, half-spaces, punctuation, and folding case. @@ -93,8 +113,20 @@ export function extractSearchTokens(rawQuery?: string | null): string[] { ); // If filtering left nothing, return original non-empty tokens - const result = meaningful.length > 0 ? meaningful : rawTokens; - return Array.from(new Set(result)); + const baseTokens = meaningful.length > 0 ? meaningful : rawTokens; + + // Expand with domain aliases (e.g. تائورین <-> تورین <-> taurine) + const expanded = new Set(baseTokens); + for (const token of baseTokens) { + const directAliases = SEARCH_EXPANSION_MAP[token] || SEARCH_EXPANSION_MAP[token.toLowerCase()]; + if (directAliases) { + for (const alias of directAliases) { + expanded.add(normalizeSearchText(alias)); + } + } + } + + return Array.from(expanded).filter((t) => t.length >= 2); } export interface SearchScoreItem { diff --git a/backend/src/products/products.service.ts b/backend/src/products/products.service.ts index d435a3f9..cb0af5b3 100644 --- a/backend/src/products/products.service.ts +++ b/backend/src/products/products.service.ts @@ -86,7 +86,10 @@ export class ProductsService { { ingredientList: { some: { - ingredient: { contains: token, mode }, + OR: [ + { ingredient: { contains: token, mode } }, + { ingredientCardSlug: { contains: token, mode } }, + ], }, }, },