0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-17 03:42:43 +00:00

use $text instead

This commit is contained in:
Víctor Losada Hernández
2024-05-06 22:59:09 +02:00
parent 752b2caaf4
commit e75c556443

View File

@@ -7,26 +7,63 @@ const archive = {
/* Searches for matching title, also attempts to partial match */ /* Searches for matching title, also attempts to partial match */
findBrews: async (req, res, next) => { findBrews: async (req, res, next) => {
try { try {
/*
const dbName = HomebrewModel.db.name;
console.log("Database Name:", dbName);
const collectionName = HomebrewModel.collection.name;
console.log("Collection Name:", collectionName);
*/
console.table(req.query); console.table(req.query);
const title = req.query.title || ''; const title = req.query.title || '';
const page = Math.max(parseInt(req.query.page) || 1, 1); const page = Math.max(parseInt(req.query.page) || 1, 1);
const minPageSize = 6; const minPageSize = 6;
const pageSize = Math.max(parseInt(req.query.size) || 10, minPageSize); const pageSize = Math.max(
parseInt(req.query.size) || 10,
minPageSize
);
const skip = (page - 1) * pageSize; const skip = (page - 1) * pageSize;
const titleQuery = { const brewsQuery = {
title : { $regex: decodeURIComponent(title), $options: 'i' },
$or: [], $or: [],
published : true published: true,
}; };
if (req.query.legacy === 'true') { if (req.query.legacy === 'true') {
titleQuery.$or.push({ renderer: 'legacy' }); brewsQuery.$or.push({ renderer: 'legacy' });
}; }
if (req.query.v3 === 'true') { if (req.query.v3 === 'true') {
titleQuery.$or.push({ renderer: 'V3' }); brewsQuery.$or.push({ renderer: 'V3' });
}
// If user wants to use RegEx it needs to format like /text/
const titleConditionsArray =
title.startsWith('/') && title.endsWith('/')
? [
{
'title': {
$regex: title.slice(1, -1),
$options: 'i',
},
},
]
: buildTitleConditions(title);
function buildTitleConditions(inputString) {
return [
{
$text: {
$search: inputString,
$caseSensitive: false,
},
},
];
}
const titleQuery = {
$and: [brewsQuery, ...titleConditionsArray],
}; };
const projection = { const projection = {
@@ -41,21 +78,28 @@ const archive = {
.maxTimeMS(5000) .maxTimeMS(5000)
.exec(); .exec();
const totalBrews = await HomebrewModel.countDocuments(titleQuery).maxTimeMS(5000); const totalBrews = await HomebrewModel.countDocuments(
titleQuery
).maxTimeMS(5000);
const totalPages = Math.ceil(totalBrews / pageSize); const totalPages = Math.ceil(totalBrews / pageSize);
//console.log('Total brews: ', totalBrews); console.log('Total brews: ', totalBrews);
//console.log('Total pages: ', totalPages); console.log('Total pages: ', totalPages);
return res.json({ brews, page, totalPages, totalBrews }); return res.json({ brews, page, totalPages, totalBrews });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
if (error.response && error.response.status === 503) { if (error.response && error.response.status === 503) {
return res.status(503).json({ errorCode: '503', message: 'Service Unavailable' }); return res
.status(503)
.json({ errorCode: '503', message: 'Service Unavailable' });
} else { } else {
return res.status(500).json({ errorCode: '500', message: 'Internal Server Error' }); return res.status(500).json({
} errorCode: '500',
message: 'Internal Server Error',
});
} }
} }
},
}; };
router.get('/api/archive', asyncHandler(archive.findBrews)); router.get('/api/archive', asyncHandler(archive.findBrews));