1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-19 13:19:42 +02:00
codex.docs/src/backend/routes/api/search.ts
2022-09-07 18:40:28 +03:00

58 lines
1.4 KiB
TypeScript

import express, { Request, Response } from 'express';
import Search from '../../controllers/search.js';
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);
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,
section: page.section,
anchor: page.anchor,
shortBody: page.shortBody,
};
});
res.json({
success: true,
result: {
suggestions: searchResponse.suggestions,
pages: compactedPages,
// time: searchItem,
},
});
} catch (err) {
res.status(400).json({
success: false,
error: (err as Error).message,
});
}
});
export default router;