Spaces:
Running
Running
Download pages/api/search.js from powerllm/anycoder-cf48624d: direct link, hf CLI and curl.
- Browser
- Download file 2.59 kB
-
https://huggingface.co/spaces/powerllm/anycoder-cf48624d/resolve/main/pages/api/search.js
- Command line
-
hf download hf://spaces/powerllm/anycoder-cf48624d/pages/api/search.js
-
curl -L -o search.js https://huggingface.co/spaces/powerllm/anycoder-cf48624d/resolve/main/pages/api/search.js
2.59 kB
| import { getDb, initializeDatabase } from '../../lib/database' | |
| const CACHE = new Map() | |
| const CACHE_TTL = 60000 | |
| export default function handler(req, res) { | |
| if (req.method !== 'GET') { | |
| return res.status(405).json({ error: 'Method not allowed' }) | |
| } | |
| try { | |
| initializeDatabase() | |
| const db = getDb() | |
| const { q, page = '1', limit = '20' } = req.query | |
| const query = q?.trim() || '' | |
| const pageNum = parseInt(page) || 1 | |
| const limitNum = Math.min(parseInt(limit) || 20, 100) | |
| const offset = (pageNum - 1) * limitNum | |
| const cacheKey = `${query}-${pageNum}-${limitNum}` | |
| const cached = CACHE.get(cacheKey) | |
| if (cached && Date.now() - cached.timestamp < CACHE_TTL) { | |
| return res.status(200).json(cached.data) | |
| } | |
| const startTime = performance.now() | |
| let results = [] | |
| let total = 0 | |
| if (query) { | |
| const countStmt = db.prepare(` | |
| SELECT count(*) as count FROM documents WHERE documents MATCH ? | |
| `) | |
| total = countStmt.get(query).count | |
| const stmt = db.prepare(` | |
| SELECT | |
| r.id, | |
| r.title, | |
| r.content, | |
| r.tags, | |
| r.date, | |
| r.source_url, | |
| r.metadata, | |
| bm25(documents, 1.0, 0.5) as score, | |
| snippet(documents, 0, '<mark>', '</mark>', '...', 32) as snippet | |
| FROM documents | |
| JOIN documents_raw r ON documents.rowid = r.id | |
| WHERE documents MATCH ? | |
| ORDER BY score | |
| LIMIT ? OFFSET ? | |
| `) | |
| results = stmt.all(query, limitNum, offset) | |
| } else { | |
| const countStmt = db.prepare('SELECT count(*) as count FROM documents_raw') | |
| total = countStmt.get().count | |
| const stmt = db.prepare(` | |
| SELECT id, title, content, tags, date, source_url, metadata | |
| FROM documents_raw | |
| ORDER BY date DESC | |
| LIMIT ? OFFSET ? | |
| `) | |
| results = stmt.all(limitNum, offset) | |
| } | |
| const endTime = performance.now() | |
| const responseTime = (endTime - startTime).toFixed(2) | |
| const responseData = { | |
| results, | |
| total, | |
| page: pageNum, | |
| limit: limitNum, | |
| totalPages: Math.ceil(total / limitNum), | |
| time: responseTime | |
| } | |
| CACHE.set(cacheKey, { | |
| data: responseData, | |
| timestamp: Date.now() | |
| }) | |
| while (CACHE.size > 100) { | |
| const firstKey = CACHE.keys().next().value | |
| CACHE.delete(firstKey) | |
| } | |
| res.status(200).json(responseData) | |
| } catch (error) { | |
| console.error('Search error:', error) | |
| res.status(500).json({ error: 'Search failed', details: error.message }) | |
| } | |
| } |