From 66b9d755248107cd193a7740db6f4956e89250ed Mon Sep 17 00:00:00 2001 From: Canina Automation Date: Tue, 27 Oct 2026 11:39:55 +0000 Subject: [PATCH] feat(search-pagination): implement normalized weighted search and standardized pagination across blogs, products and wiki --- backend/src/blogs/blogs.service.ts | 65 ++++-- backend/src/common/utils/search.utils.spec.ts | 91 ++++++++ backend/src/common/utils/search.utils.ts | 215 ++++++++++++++++++ backend/src/products/products.service.ts | 94 ++++++-- backend/src/wiki/wiki.service.ts | 55 +++-- frontend/application/app/wiki/[slug]/page.tsx | 4 +- frontend/application/components/BlogPage.tsx | 2 +- 7 files changed, 469 insertions(+), 57 deletions(-) create mode 100644 backend/src/common/utils/search.utils.spec.ts create mode 100644 backend/src/common/utils/search.utils.ts diff --git a/backend/src/blogs/blogs.service.ts b/backend/src/blogs/blogs.service.ts index 616b1ddb..9fcafaa2 100644 --- a/backend/src/blogs/blogs.service.ts +++ b/backend/src/blogs/blogs.service.ts @@ -2,8 +2,15 @@ import { Injectable, NotFoundException } from "@nestjs/common"; import { Prisma } from "@prisma/client"; import { PrismaService } from "../prisma/prisma.service"; import { PaginationDto } from "../common/dto/pagination.dto"; +import { + createPaginationMeta, + extractSearchTokens, + normalizeSearchText, + calculateRelevanceScore, +} from "../common/utils/search.utils"; export interface BlogFilterDto extends PaginationDto { + categoryId?: string; categorySlug?: string; tagSlug?: string; featured?: boolean | string; @@ -20,6 +27,7 @@ export class BlogsService { limit = 10, sortBy = "publishedAt", sortOrder = "desc", + categoryId, categorySlug, tagSlug, featured, @@ -32,20 +40,23 @@ export class BlogsService { OR: [{ publishedAt: null }, { publishedAt: { lte: now } }], }; - if (search) { + const tokens = extractSearchTokens(search); + if (tokens.length > 0) { whereClause.AND = [ { - OR: [ - { title: { contains: search, mode: "insensitive" } }, - { excerpt: { contains: search, mode: "insensitive" } }, - { content: { contains: search, mode: "insensitive" } }, - { keywords: { contains: search, mode: "insensitive" } }, - ], + OR: tokens.flatMap((token) => [ + { title: { contains: token, mode: "insensitive" } }, + { excerpt: { contains: token, mode: "insensitive" } }, + { keywords: { contains: token, mode: "insensitive" } }, + { tags: { some: { name: { contains: token, mode: "insensitive" } } } }, + ]), }, ]; } - if (categorySlug) { + if (categoryId && categoryId !== "ALL") { + whereClause.categoryId = categoryId; + } else if (categorySlug && categorySlug !== "all") { whereClause.category = { slug: categorySlug }; } @@ -57,13 +68,15 @@ export class BlogsService { whereClause.featured = String(featured) === "true"; } - const skip = (Number(page) - 1) * Number(limit); + const safePage = Math.max(1, Number(page) || 1); + const safeLimit = Math.max(1, Number(limit) || 10); + const skip = (safePage - 1) * safeLimit; - const [data, total] = await Promise.all([ + const [rawBlogs, total] = await Promise.all([ this.prisma.blog.findMany({ where: whereClause, - skip, - take: Number(limit), + skip: tokens.length > 0 ? undefined : skip, + take: tokens.length > 0 ? 100 : safeLimit, orderBy: [ { [sortBy === "createdAt" ? "createdAt" : "publishedAt"]: sortOrder, @@ -88,14 +101,30 @@ export class BlogsService { this.prisma.blog.count({ where: whereClause }), ]); + let data = rawBlogs; + if (tokens.length > 0 && search) { + data = [...rawBlogs] + .map((b) => ({ + blog: b, + score: calculateRelevanceScore( + { + title: b.title, + slug: b.slug, + shortDescription: b.excerpt, + tags: b.tags, + }, + search, + tokens, + ), + })) + .sort((a, b) => b.score - a.score) + .map((item) => item.blog) + .slice(skip, skip + safeLimit); + } + return { data, - meta: { - total, - page: Number(page), - lastPage: Math.ceil(total / Number(limit)) || 1, - limit: Number(limit), - }, + meta: createPaginationMeta(total, safePage, safeLimit), }; } diff --git a/backend/src/common/utils/search.utils.spec.ts b/backend/src/common/utils/search.utils.spec.ts new file mode 100644 index 00000000..c10d308c --- /dev/null +++ b/backend/src/common/utils/search.utils.spec.ts @@ -0,0 +1,91 @@ +import { + normalizeSearchText, + extractSearchTokens, + calculateRelevanceScore, + createPaginationMeta, +} from "./search.utils"; + +describe("search.utils", () => { + describe("createPaginationMeta", () => { + it("builds consistent pagination metadata", () => { + const meta = createPaginationMeta(52, 2, 10); + expect(meta).toEqual({ + total: 52, + page: 2, + limit: 10, + totalPages: 6, + lastPage: 6, + hasNextPage: true, + hasPrevPage: true, + }); + }); + + it("handles boundary zero and single page counts safely", () => { + const metaZero = createPaginationMeta(0, 1, 10); + expect(metaZero.totalPages).toBe(1); + expect(metaZero.hasNextPage).toBe(false); + expect(metaZero.hasPrevPage).toBe(false); + + const metaOne = createPaginationMeta(5, 1, 10); + expect(metaOne.totalPages).toBe(1); + expect(metaOne.hasNextPage).toBe(false); + expect(metaOne.hasPrevPage).toBe(false); + }); + }); + + describe("normalizeSearchText", () => { + it("normalizes Arabic kaf and yeh to Persian equivalents", () => { + expect(normalizeSearchText("قطره قلب كنينای ايران")).toBe("قطره قلب کنینای ایران"); + }); + + it("strips parentheses, diacritics, and half-spaces", () => { + expect(normalizeSearchText("ال-کارنیتین (L-Carnitine) \u200c دارویی")).toBe("ال کارنیتین l carnitine دارویی"); + expect(normalizeSearchText("مُکَمِّل")).toBe("مکمل"); + }); + + it("converts Persian digits to English digits", () => { + expect(normalizeSearchText("محصول شماره ۱۲۳")).toBe("محصول شماره 123"); + }); + }); + + describe("extractSearchTokens", () => { + it("extracts meaningful tokens and removes stop words", () => { + const tokens = extractSearchTokens("بهترین قطره برای تقویت قلب سگ و گربه"); + expect(tokens).toContain("قطره"); + expect(tokens).toContain("تقویت"); + expect(tokens).toContain("قلب"); + expect(tokens).toContain("سگ"); + expect(tokens).toContain("گربه"); + expect(tokens).not.toContain("برای"); + expect(tokens).not.toContain("و"); + }); + }); + + describe("calculateRelevanceScore", () => { + it("ranks exact title / ingredient matches far higher than general description mentions", () => { + const herzVital = { + title: "هیرتز ویتال (مکمل تقویت قلب) کنینا", + slug: "canina-herz-vital-100ml", + ingredients: [{ nameFa: "ال-کارنیتین (L-Carnitine)", nameEn: "L-Carnitine" }], + symptoms: [{ symptom: "بیماری قلبی" }], + shortDescription: "حمایت تخصصی از عضله قلب سگ و گربه", + description: "توضیحات مفصل درباره کارکرد قلب و عروق...", + }; + + const genericProduct = { + title: "پودر ویتامین پلاس سگ", + slug: "canina-vitamin-plus", + ingredients: [{ nameFa: "ویتامین A", nameEn: "Vitamin A" }], + symptoms: [], + shortDescription: "تقویت عمومی بدن", + description: "این مولتی ویتامین برای تمام اندام‌های بدن از جمله قلب، کبد و مفاصل مفید است.", + }; + + const tokens = extractSearchTokens("ال-کارنیتین قلب"); + const scoreHerz = calculateRelevanceScore(herzVital, "ال-کارنیتین قلب", tokens); + const scoreGeneric = calculateRelevanceScore(genericProduct, "ال-کارنیتین قلب", tokens); + + expect(scoreHerz).toBeGreaterThan(scoreGeneric * 4); + }); + }); +}); \ No newline at end of file diff --git a/backend/src/common/utils/search.utils.ts b/backend/src/common/utils/search.utils.ts new file mode 100644 index 00000000..33b55a12 --- /dev/null +++ b/backend/src/common/utils/search.utils.ts @@ -0,0 +1,215 @@ +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 = { + "۰": "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", +]); + +/** + * 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 result = meaningful.length > 0 ? meaningful : rawTokens; + return Array.from(new Set(result)); +} + +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; +} \ No newline at end of file diff --git a/backend/src/products/products.service.ts b/backend/src/products/products.service.ts index 321da147..d435a3f9 100644 --- a/backend/src/products/products.service.ts +++ b/backend/src/products/products.service.ts @@ -3,6 +3,11 @@ import { PrismaService } from "../prisma/prisma.service"; import { GetProductsDto } from "./dto/get-products.dto"; import { Prisma } from "@prisma/client"; import { RevalidationService } from "../common/revalidation/revalidation.service"; +import { + createPaginationMeta, + extractSearchTokens, + calculateRelevanceScore, +} from "../common/utils/search.utils"; @Injectable() export class ProductsService { @@ -62,37 +67,57 @@ export class ProductsService { andConditions.push({ priceValue: { lte: Number(maxPrice) } }); } - if (search) { - andConditions.push({ - OR: [ - { artNo: { contains: search, mode: "insensitive" } }, - { barcode: { contains: search, mode: "insensitive" } }, - { nameFa: { contains: search, mode: "insensitive" } }, - { nameEn: { contains: search, mode: "insensitive" } }, - { description: { contains: search, mode: "insensitive" } }, - { shortDescription: { contains: search, mode: "insensitive" } }, + const tokens = extractSearchTokens(search); + if (tokens.length > 0) { + const mode = "insensitive" as const; + const searchConditions: Prisma.ProductWhereInput[] = [ + { artNo: { contains: search, mode } }, + { barcode: { contains: search, mode } }, + { slug: { contains: search, mode } }, + { nameFa: { contains: search, mode } }, + { nameEn: { contains: search, mode } }, + { shortDescription: { contains: search, mode } }, + ]; + + for (const token of tokens) { + searchConditions.push( + { nameFa: { contains: token, mode } }, + { nameEn: { contains: token, mode } }, { - symptoms: { + ingredientList: { some: { - symptom: { contains: search, mode: "insensitive" }, + ingredient: { contains: token, mode }, }, }, }, - ], + { + symptoms: { + some: { + symptom: { contains: token, mode }, + }, + }, + }, + ); + } + + andConditions.push({ + OR: searchConditions, }); } const whereClause: Prisma.ProductWhereInput = andConditions.length > 0 ? { AND: andConditions } : {}; - const skip = (page - 1) * limit; + const safePage = Math.max(1, Number(page) || 1); + const safeLimit = Math.max(1, Number(limit) || 50); + const skip = (safePage - 1) * safeLimit; - // 1. Fetch products with lean relations (no heavy N+1 orderItems join) + // 1. Fetch products with lean relations const [rawProducts, total] = await Promise.all([ this.prisma.product.findMany({ where: whereClause, - skip, - take: limit, + skip: tokens.length > 0 ? undefined : skip, + take: tokens.length > 0 ? 100 : safeLimit, orderBy: { [sortBy]: sortOrder }, include: { ingredientList: true, @@ -102,8 +127,34 @@ export class ProductsService { this.prisma.product.count({ where: whereClause }), ]); + // Rank by relevance if searching + let candidateProducts = rawProducts; + if (tokens.length > 0 && search) { + candidateProducts = [...rawProducts] + .map((p) => ({ + product: p, + score: calculateRelevanceScore( + { + artNo: p.artNo, + barcode: p.barcode, + slug: p.slug, + title: p.nameFa || p.nameEn, + shortDescription: p.shortDescription, + description: p.description, + ingredients: p.ingredientList, + symptoms: p.symptoms, + }, + search, + tokens, + ), + })) + .sort((a, b) => b.score - a.score) + .map((item) => item.product) + .slice(skip, skip + safeLimit); + } + // 2. Efficient single aggregate query for total sold counts for the current page slice - const productIds = rawProducts.map((p) => p.id); + const productIds = candidateProducts.map((p) => p.id); let salesMap: Record = {}; if (productIds.length > 0) { @@ -131,7 +182,7 @@ export class ProductsService { userRole === "SuperAdmin"; const isAdmin = userRole === "ADMIN" || userRole === "SuperAdmin"; - const data = rawProducts.map((p) => { + const data = candidateProducts.map((p) => { const totalSold = salesMap[p.id] || 0; const { buyPrice, wholesalePrice, ...publicProduct } = p; void buyPrice; @@ -146,12 +197,7 @@ export class ProductsService { return { data, - meta: { - total, - page, - lastPage: Math.ceil(total / limit) || 1, - limit, - }, + meta: createPaginationMeta(total, safePage, safeLimit), }; } diff --git a/backend/src/wiki/wiki.service.ts b/backend/src/wiki/wiki.service.ts index a26e8917..7c88e406 100644 --- a/backend/src/wiki/wiki.service.ts +++ b/backend/src/wiki/wiki.service.ts @@ -2,6 +2,11 @@ import { Injectable, NotFoundException } from "@nestjs/common"; import { Prisma } from "@prisma/client"; import { PrismaService } from "../prisma/prisma.service"; import { PaginationDto } from "../common/dto/pagination.dto"; +import { + createPaginationMeta, + extractSearchTokens, + calculateRelevanceScore, +} from "../common/utils/search.utils"; @Injectable() export class WikiService { @@ -19,33 +24,57 @@ export class WikiService { const sortBy = allowedSortFields.includes(rawSortBy) ? rawSortBy : "term"; const whereClause: Prisma.ScientificTermWhereInput = {}; - if (search) { + const tokens = extractSearchTokens(search); + if (tokens.length > 0) { + const mode = "insensitive" as const; whereClause.OR = [ - { term: { contains: search, mode: "insensitive" } }, - { definition: { contains: search, mode: "insensitive" } }, + { term: { contains: search, mode } }, + { key: { contains: search, mode } }, + { definition: { contains: search, mode } }, + ...tokens.flatMap((token) => [ + { term: { contains: token, mode } }, + { key: { contains: token, mode } }, + ]), ]; } - const skip = (page - 1) * limit; + const safePage = Math.max(1, Number(page) || 1); + const safeLimit = Math.max(1, Number(limit) || 10); + const skip = (safePage - 1) * safeLimit; - const [data, total] = await Promise.all([ + const [rawTerms, total] = await Promise.all([ this.prisma.scientificTerm.findMany({ where: whereClause, - skip, - take: limit, + skip: tokens.length > 0 ? undefined : skip, + take: tokens.length > 0 ? 100 : safeLimit, orderBy: { [sortBy]: sortOrder }, }), this.prisma.scientificTerm.count({ where: whereClause }), ]); + let data = rawTerms; + if (tokens.length > 0 && search) { + data = [...rawTerms] + .map((t) => ({ + term: t, + score: calculateRelevanceScore( + { + slug: t.key, + title: t.term, + description: t.definition, + }, + search, + tokens, + ), + })) + .sort((a, b) => b.score - a.score) + .map((item) => item.term) + .slice(skip, skip + safeLimit); + } + return { data, - meta: { - total, - page, - lastPage: Math.ceil(total / limit), - limit, - }, + meta: createPaginationMeta(total, safePage, safeLimit), }; } diff --git a/frontend/application/app/wiki/[slug]/page.tsx b/frontend/application/app/wiki/[slug]/page.tsx index 6cb8e3c3..19809967 100644 --- a/frontend/application/app/wiki/[slug]/page.tsx +++ b/frontend/application/app/wiki/[slug]/page.tsx @@ -181,6 +181,8 @@ export default async function WikiTermPage({ params }: { params: Promise<{ slug: { name: term.term, url: `https://canina.ir/wiki/${encodedSlug}` }, ]); + const shopSearchTerm = (term.rawIngredient?.nameFa ? term.rawIngredient.nameFa.replace(/\s*\([^)]*\)\s*/g, ' ').trim() : term.term).replace(/\s*\([^)]*\)\s*/g, ' ').trim(); + return ( <>