import { NextResponse } from 'next/server'; import { listArticles } from '@/lib/engine'; // GET /api/search?q=foo — простой поиск по title/excerpt/tags export async function GET(req) { const q = (req.nextUrl.searchParams.get('q') || '').trim().toLowerCase(); if (!q || q.length < 2) return NextResponse.json([]); try { const all = await listArticles({ limit: 100 }); const tokens = q.split(/\s+/).filter(Boolean); const matched = all .map(a => { const haystack = [a.title, a.excerpt, (a.tags || []).join(' ')] .filter(Boolean).join(' ').toLowerCase(); const score = tokens.reduce((acc, t) => acc + (haystack.includes(t) ? 1 : 0), 0); return { ...a, score }; }) .filter(a => a.score > 0) .sort((a, b) => b.score - a.score) .slice(0, 12); return NextResponse.json(matched); } catch (err) { return NextResponse.json({ error: err.message }, { status: 500 }); } }