diff options
| author | Damani Pradyumn <kumar.damani@mail.utoronto.ca> | 2016-07-24 20:00:14 +0000 |
|---|---|---|
| committer | Damani Pradyumn <kumar.damani@mail.utoronto.ca> | 2016-07-24 20:00:14 +0000 |
| commit | 75c7904f370d8decacc3aaebbbdc5419b21eee64 (patch) | |
| tree | 7130b1e91b4cbb0a4d28f0534bfead8e4fddaf5f /node_simple.js | |
| parent | bb66f6a9f7d6a2f9d75ab7d42483339618615e03 (diff) | |
improved search by course code
Diffstat (limited to 'node_simple.js')
| -rw-r--r-- | node_simple.js | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/node_simple.js b/node_simple.js index 8e7620f..6117b01 100644 --- a/node_simple.js +++ b/node_simple.js @@ -109,6 +109,52 @@ exports.remove_course = function (course_code, callback) { }; +exports.search_exams = function ( token, callback ) { + + mongoFactory.getConnection(uri).then(function (db) { + var exams = db.collection('exams'); + + exams.find( + { $text: { $search: token } }, + { score: { $meta: "textScore" } } // the strength of the exams + ).sort({ year: -1}).toArray( function (err, docs) { // order by year + if (err) throw err; + + else { // get the title + // callback(true, docs); + // console.log(docs); + exports.find_course(token.toUpperCase(), function (result, data) { + + // console.log("here"); + + if (result == true) { + // append the title from data to each exam object from docs + docs.forEach(function (doc) { + doc.title = data[0].title; + }); + + // if (debug_mode == true){ + // console.log(docs); + // console.log(data); + // } + /*callback(docs); // send back the data*/ + } + else if (result == false) { // no such course was found + console.log("This course has not been added to the database."); + } + callback(true, docs); // send back the data + + }); + } + }); + + // db.close(); + + }).catch(function (err) { + console.error(err); + }); +}; + /*This function will remove the user given user_name from the users table * and the logins table. So far. @@ -836,9 +882,13 @@ exports.get_all_exams = function (course_code, callback) { // get the exams table var exam_collection = db.collection('exams'); + exam_collection.createIndex( // make the following fields searchable + { + "course_code":"text" + }); // search exams table with given course code exam_collection.find( - { course_code: course_code } + { $text: { $search: course_code } } ).sort({ year: -1}).toArray( function (err, docs) { // order by year if (err) throw err; @@ -1080,10 +1130,13 @@ exports.find_course = function (course_code, callback) { // fetch the courses table var courses = db.collection('courses'); - + courses.createIndex( // make the following fields searchable + { + "course_code":"text" + }); // look for the courses courses.find( - { course_code: course_code } + { $text: { $search: course_code } } ).toArray(function (err, docs) { if (err) throw err; // if this course doesnt exist.... add it (via add_course call) |
