2022-08-11 18:28:15 +03:00
|
|
|
import express, { Request, Response } from 'express';
|
2022-08-26 15:10:53 +03:00
|
|
|
import Search from '../../controllers/search.js';
|
2022-08-11 18:28:15 +03:00
|
|
|
|
|
|
|
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 */
|
2022-08-26 15:10:53 +03:00
|
|
|
// const startTime = performance.now();
|
2022-08-11 18:28:15 +03:00
|
|
|
|
2022-09-07 18:40:28 +03:00
|
|
|
// const search = new Search();
|
|
|
|
//
|
|
|
|
// const searchResponse = await search.query(searchString);
|
2022-08-26 15:08:36 +03:00
|
|
|
|
2022-09-07 18:40:28 +03:00
|
|
|
const searchResponse = await Search.query(searchString);
|
2022-08-11 18:28:15 +03:00
|
|
|
|
|
|
|
/** End measuring search time */
|
2022-08-26 15:10:53 +03:00
|
|
|
// const endTime = performance.now();
|
2022-08-11 18:28:15 +03:00
|
|
|
|
|
|
|
/** Show search time */
|
2022-08-26 15:10:53 +03:00
|
|
|
// const searchItem = (endTime - startTime).toFixed(6);
|
|
|
|
// console.log(`🔎 "${searchString}" ⏱ ${searchItem} ms`);
|
2022-08-11 18:28:15 +03:00
|
|
|
|
|
|
|
const compactedPages = searchResponse.pages.map(page => {
|
|
|
|
return {
|
|
|
|
_id: page._id,
|
|
|
|
title: page.title,
|
|
|
|
uri: page.uri,
|
2022-08-23 14:12:15 +03:00
|
|
|
section: page.section,
|
|
|
|
anchor: page.anchor,
|
2022-08-15 18:53:57 +03:00
|
|
|
shortBody: page.shortBody,
|
2022-08-11 18:28:15 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
result: {
|
2022-08-15 18:53:57 +03:00
|
|
|
suggestions: searchResponse.suggestions,
|
2022-08-11 18:28:15 +03:00
|
|
|
pages: compactedPages,
|
2022-08-26 15:10:53 +03:00
|
|
|
// time: searchItem,
|
2022-08-11 18:28:15 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
res.status(400).json({
|
|
|
|
success: false,
|
|
|
|
error: (err as Error).message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|