Files
canina/backend/src/common/utils/search.utils.ts
T

247 lines
8.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export interface PaginationMeta {
total: number;
page: number;
limit: number;
totalPages: number;
lastPage: number;
hasNextPage: boolean;
hasPrevPage: boolean;
}
export function createPaginationMeta(
total: number,
page: number,
limit: number,
): PaginationMeta {
const safeTotal = Math.max(0, Number(total) || 0);
const safeLimit = Math.max(1, Number(limit) || 10);
const safePage = Math.max(1, Number(page) || 1);
const totalPages = Math.ceil(safeTotal / safeLimit) || 1;
return {
total: safeTotal,
page: safePage,
limit: safeLimit,
totalPages,
lastPage: totalPages,
hasNextPage: safePage < totalPages,
hasPrevPage: safePage > 1,
};
}
const PERSIAN_ARABIC_DIGITS: Record<string, string> = {
"۰": "0", "۱": "1", "۲": "2", "۳": "3", "۴": "4",
"۵": "5", "۶": "6", "۷": "7", "۸": "8", "۹": "9",
"٠": "0", "١": "1", "٢": "2", "٣": "3", "٤": "4",
"٥": "5", "٦": "6", "٧": "7", "٨": "8", "٩": "9",
};
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<string, string[]> = {
"تائورین": ["تورین", "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.
*/
export function normalizeSearchText(text?: string | null): string {
if (!text) return "";
let s = String(text);
// Convert Persian/Arabic digits
s = s.replace(/[۰-۹٠-٩]/g, (ch) => PERSIAN_ARABIC_DIGITS[ch] || ch);
// Normalize Arabic letters to standard Persian/English
s = s
.replace(/[يى]/g, "ی")
.replace(/[ك]/g, "ک")
.replace(/[ة]/g, "ه")
.replace(/[آأإٱ]/g, "ا")
.replace(/[ؤ]/g, "و")
.replace(/[ئ]/g, "ی");
// Remove Arabic/Persian diacritics (fatha, damma, kasra, tanwin, sukun, tashdid)
s = s.replace(/[\u064B-\u065F\u0670]/g, "");
// Replace half-space, non-breaking space, parentheses, brackets, quotes, punctuation with single space
s = s.replace(/[\u200B-\u200D\uFEFF\u00A0\u200E\u200F_~`!@#$%^&*()+=[\]{}|\\;:'",.<>/?،؛؟]/g, " ");
// Collapse hyphens surrounded by spaces or replace hyphen with space
s = s.replace(/-/g, " ");
// Lowercase & collapse whitespace
return s.toLowerCase().replace(/\s+/g, " ").trim();
}
/**
* Extracts meaningful tokens from a query, eliminating stop words
* unless the entire query only consists of stop words.
*/
export function extractSearchTokens(rawQuery?: string | null): string[] {
const normalized = normalizeSearchText(rawQuery);
if (!normalized) return [];
const rawTokens = normalized.split(" ").filter((t) => t.length > 0);
if (rawTokens.length === 0) return [];
// Filter out stop words
const meaningful = rawTokens.filter(
(t) => !COMMON_STOP_WORDS.has(t) && t.length >= 2,
);
// If filtering left nothing, return original non-empty tokens
const baseTokens = meaningful.length > 0 ? meaningful : rawTokens;
// Expand with domain aliases (e.g. تائورین <-> تورین <-> taurine)
const expanded = new Set<string>(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 {
artNo?: string | null;
barcode?: string | null;
slug?: string | null;
title?: string | null;
shortDescription?: string | null;
description?: string | null;
tags?: Array<{ name?: string | null } | string> | null;
ingredients?: Array<
| { ingredient?: string | null; nameFa?: string | null; nameEn?: string | null }
| string
> | null;
symptoms?: Array<{ symptom?: string | null } | string> | null;
}
/**
* Calculates a relevance score for a product or blog item against search tokens.
*/
export function calculateRelevanceScore(
item: SearchScoreItem,
rawQuery: string,
tokens: string[],
): number {
if (!rawQuery || tokens.length === 0) return 0;
const normalizedQuery = normalizeSearchText(rawQuery);
const normalizedTitle = normalizeSearchText(item.title);
const normalizedArtNo = normalizeSearchText(item.artNo);
const normalizedBarcode = normalizeSearchText(item.barcode);
const normalizedSlug = normalizeSearchText(item.slug);
let score = 0;
// Exact matches get massive priority
if (normalizedArtNo && normalizedArtNo === normalizedQuery) score += 200;
if (normalizedBarcode && normalizedBarcode === normalizedQuery) score += 180;
if (normalizedTitle && normalizedTitle === normalizedQuery) score += 150;
if (normalizedSlug && normalizedSlug === normalizedQuery) score += 140;
// Title starts with full query
if (normalizedTitle && normalizedTitle.startsWith(normalizedQuery)) score += 80;
// Title contains exact full query phrase
if (normalizedTitle && normalizedTitle.includes(normalizedQuery)) score += 60;
// Token level matching in title (highest relevance)
const titleTokens = normalizedTitle.split(" ");
for (const token of tokens) {
if (titleTokens.includes(token)) {
score += 35;
} else if (normalizedTitle.includes(token)) {
score += 20;
}
}
// Ingredient / scientific terms matching
if (item.ingredients && Array.isArray(item.ingredients)) {
for (const ing of item.ingredients) {
let ingName = "";
if (typeof ing === "string") {
ingName = ing;
} else if (ing) {
ingName = `${ing.ingredient || ""} ${ing.nameFa || ""} ${ing.nameEn || ""}`.trim();
}
const normIng = normalizeSearchText(ingName);
if (normIng.includes(normalizedQuery)) score += 40;
for (const token of tokens) {
if (normIng.includes(token)) score += 15;
}
}
}
// Symptoms matching
if (item.symptoms && Array.isArray(item.symptoms)) {
for (const sym of item.symptoms) {
const symName = typeof sym === "string" ? sym : sym.symptom || "";
const normSym = normalizeSearchText(symName);
if (normSym.includes(normalizedQuery)) score += 30;
for (const token of tokens) {
if (normSym.includes(token)) score += 10;
}
}
}
// Tags matching
if (item.tags && Array.isArray(item.tags)) {
for (const tag of item.tags) {
const tagName = typeof tag === "string" ? tag : tag.name || "";
const normTag = normalizeSearchText(tagName);
if (normTag.includes(normalizedQuery)) score += 25;
for (const token of tokens) {
if (normTag.includes(token)) score += 8;
}
}
}
// Short description matching
const normShort = normalizeSearchText(item.shortDescription);
if (normShort) {
for (const token of tokens) {
if (normShort.includes(token)) score += 5;
}
}
// Long description (low weight, capped so it never overwhelms title/ingredients)
const normDesc = normalizeSearchText(item.description);
if (normDesc) {
let descHits = 0;
for (const token of tokens) {
if (normDesc.includes(token)) descHits += 1;
}
score += Math.min(descHits * 2, 6);
}
return score;
}