@@ -4,7 +4,7 @@ const { index } = require('./connectVectorDB.js');
4
4
const connectDatabase = require ( '../database/database.js' ) ;
5
5
6
6
const makeLower = ( text ) => {
7
- if ( ! text ) return '' ;
7
+ if ( ! text ) return '' ;
8
8
return text
9
9
. split ( ' ' )
10
10
. map ( ( word ) => word . toLowerCase ( ) )
@@ -19,39 +19,71 @@ const add_batch_data_to_vdb = async () => {
19
19
console . log ( "No jobs found." ) ;
20
20
return ;
21
21
}
22
-
23
- let modifiedJobs = jobs . map ( ( job ) => ( {
24
- id : job . _id . toString ( ) ,
25
- description :makeLower ( job . jobRole ) + " " + makeLower ( job . jobDescription )
26
- } ) ) ;
27
-
28
- for ( let job of modifiedJobs ) {
29
- await index . upsert ( {
30
- id : job . id ,
31
- data : job . description
32
- } ) ;
22
+
23
+ for ( let job of jobs ) {
24
+ const queryText = makeLower ( job . jobRole ) + " " + makeLower ( job . jobDescription ) ;
25
+ const fetchedJobIds = await index . query ( {
26
+ data : queryText ,
27
+ topK : 5 ,
28
+ includeVectors : false ,
29
+ includeMetadata : false ,
30
+ } ) ;
31
+
32
+ if ( ! fetchedJobIds || ! Array . isArray ( fetchedJobIds ) ) {
33
+ console . log ( fetchedJobIds ) ;
34
+ throw new Error (
35
+ "Invalid response format: 'result' is undefined or not an array"
36
+ ) ;
37
+ }
38
+
39
+ const similarJobids = fetchedJobIds . map ( ( job ) => {
40
+ if ( ! job . id ) throw new Error ( "Missing 'id' field in one of the results" ) ;
41
+ return mongoose . Types . ObjectId ( job . id ) ;
42
+ } ) ;
43
+ job . similarJobs = similarJobids ;
44
+ await job . save ( ) ;
33
45
}
34
46
console . log ( "batch job successfully added to the vector database." ) ;
35
47
36
48
} catch ( error ) {
37
49
console . error ( "Error while adding data to vector DB:" , error ) ;
38
50
}
39
- finally {
51
+ finally {
40
52
mongoose . connection . close ( ) ;
41
53
}
42
54
} ;
43
55
44
- const addJobDataToVDB = async ( jobId ) => {
56
+ const addJobDataToVDB = async ( jobId ) => {
45
57
try {
46
58
const job = await JobApplicationForm . findById ( jobId ) . select ( '_id jobRole jobDescription' ) ;
47
59
if ( ! job || job . length === 0 ) {
48
60
console . log ( "No jobs found." ) ;
49
61
return ;
50
62
}
51
63
64
+ const queryText = makeLower ( job . jobRole ) + " " + makeLower ( job . jobDescription ) ;
65
+ const fetchedJobIds = await index . query ( {
66
+ data : queryText ,
67
+ topK : 5 ,
68
+ includeVectors : false ,
69
+ includeMetadata : false ,
70
+ } ) ;
71
+
72
+ if ( ! fetchedJobIds || ! Array . isArray ( fetchedJobIds ) ) {
73
+ console . log ( fetchedJobIds ) ;
74
+ throw new Error (
75
+ "Invalid response format: 'result' is undefined or not an array"
76
+ ) ;
77
+ }
78
+
79
+ const similarJobids = fetchedJobIds . map ( ( job ) => {
80
+ if ( ! job . id ) throw new Error ( "Missing 'id' field in one of the results" ) ;
81
+ return mongoose . Types . ObjectId ( job . id ) ;
82
+ } ) ;
83
+
52
84
const modifiedJob = {
53
85
id : job . _id . toString ( ) ,
54
- description : makeLower ( job . jobRole ) + " " + makeLower ( job . jobDescription ) ,
86
+ description : queryText ,
55
87
} ;
56
88
57
89
await index . upsert ( {
@@ -60,6 +92,7 @@ const addJobDataToVDB = async(jobId) =>{
60
92
} ) ;
61
93
62
94
console . log ( "Job successfully added to the vector database." ) ;
95
+ return similarJobids ;
63
96
64
97
} catch ( error ) {
65
98
console . error ( "Error while adding data to vector DB:" , error ) ;
0 commit comments