1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-21 14:19:42 +02:00
codex.docs/src/backend/routes/api/search.ts

55 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-08-11 18:28:15 +03:00
import express, { Request, Response } from 'express';
import Search from '../../controllers/search';
const router = express.Router();
/**
* GET /search/:searchString
*
* Search given words in all documents
*/
router.get('/search', async (req: Request, res: Response) => {
try {
const searchString = req.query.text as string;
/** Start measuring search time */
const startTime = performance.now();
const search = new Search();
const searchResponse = await search.query(searchString);
/** End measuring search time */
const endTime = performance.now();
/** Show search time */
const searchItem = (endTime - startTime).toFixed(6);
console.log(`🔎 "${searchString}" ⏱ ${searchItem} ms`);
const compactedPages = searchResponse.pages.map(page => {
return {
_id: page._id,
title: page.title,
uri: page.uri,
// body: page.body,
// parent: page.parent,
};
});
res.json({
success: true,
result: {
completions: searchResponse.completions,
pages: compactedPages,
time: searchItem,
},
});
} catch (err) {
res.status(400).json({
success: false,
error: (err as Error).message,
});
}
});
export default router;