Skip to content

Commit

Permalink
ADD: search updated
Browse files Browse the repository at this point in the history
  • Loading branch information
alanansari committed Nov 26, 2023
1 parent 7b6632f commit 6bbd0dc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
31 changes: 4 additions & 27 deletions controller/homeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,35 +184,12 @@ const getMyItems = async (req, res, next) => {

const searchItems = async (req, res, next) => {
try {
const find = req.query.find || "";
let page = parseInt(req.query.page) || 1;
let limit = parseInt(req.query.limit) || 10;
const text = req.query.find || "";

if (page <= 0) page = 1;
page = page - 1;
if (limit < 0) limit = 0;
const results = await Item.aggregate([
{ $match:{name: {$regex: text ,'$options': 'i'},status:'COLLECTED_AKG'} }
]);

const regexQuery = find
.split(" ")
.map((term) => `${term}*`)
.join(" ");

const results = await Item.find(
{
$text: { $search: regexQuery },
status: "COLLECTED_AKG",
},
{
score: { $meta: "textScore" },
_id: 1,
name: 1,
description: 1,
images: 1,
}
)
.sort({ score: { $meta: "textScore" } })
.skip(page * limit)
.limit(limit);
return res.status(200).json({ success: true, items: results });
} catch (err) {
next(err);
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (cluster.isPrimary) {
} else {
const app = express();
app.use(fileUpload({
useTempFiles:false,
useTempFiles:true,
limits:{fileSize: 50*1024*1024}
}));

Expand Down

0 comments on commit 6bbd0dc

Please sign in to comment.