Skip to content

Commit

Permalink
ft<DBIndex>:create search index for xjob title
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-py committed Dec 4, 2024
1 parent 0b6fd41 commit 56cf5f7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 62 deletions.
19 changes: 13 additions & 6 deletions jobsfeed/xjob.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.getXjobs = async (req, res) => {
let { page, limit } = req.query;

page = page ? +page : 0;
limit = limit ? +limit : 25;
limit = limit ? +limit : 40;
// console.log(page, limit);

const data = await Xjob.paginate(
Expand All @@ -22,7 +22,7 @@ exports.getXjobs = async (req, res) => {
page: page,
limit: limit
}
);
).sort({ createdAt: -1 });

const jobs = data.docs;

Expand Down Expand Up @@ -69,10 +69,17 @@ exports.searchJobs = async (req, res) => {
});
}

const result = await Xjob.find(
{ $text: { $search: q } },
{ score: { $meta: 'textScore' } }
).sort({ score: { $meta: 'textScore' } });
const result = await Xjob.aggregate([
{
$search: {
index: 'title_search',
text: {
query: q,
path: 'title'
}
}
}
]);

return res.status(200).json({
status: 'success',
Expand Down
117 changes: 61 additions & 56 deletions model/xjobs.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,69 @@
const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-paginate-v2');

const xjobSchema = new mongoose.Schema({
title: {
type: String,
required: false
const xjobSchema = new mongoose.Schema(
{
title: {
type: String,
required: false
},
description: {
type: String,
required: false
},
companyName: {
type: String,
required: false
},
companyLogo: {
type: String,
required: false
},
minSalary: {
type: Number,
required: false
},
maxSalary: {
type: Number,
required: false
},
seniority: {
type: Array,
required: false
},
categories: {
type: Array,
required: false
},
publishedDate: {
type: String,
required: false
},
expiryDate: {
type: String,
required: false
},
applicationLink: {
type: String,
required: false
},
jobType: {
type: String,
required: false
},
workModel: {
type: String,
required: false
},
source: {
type: String,
required: false
}
},
description: {
type: String,
required: false
},
companyName: {
type: String,
required: false
},
companyLogo: {
type: String,
required: false
},
minSalary: {
type: Number,
required: false
},
maxSalary: {
type: Number,
required: false
},
seniority: {
type: Array,
required: false
},
categories: {
type: Array,
required: false
},
publishedDate: {
type: String,
required: false
},
expiryDate: {
type: String,
required: false
},
applicationLink: {
type: String,
required: false
},
jobType: {
type: String,
required: false
},
workModel: {
type: String,
required: false
},
source: {
type: String,
required: false
{
timestamps: true
}
});
);

xjobSchema.plugin(mongoosePaginate);

Expand Down

0 comments on commit 56cf5f7

Please sign in to comment.