From 75c7904f370d8decacc3aaebbbdc5419b21eee64 Mon Sep 17 00:00:00 2001 From: Damani Pradyumn Date: Sun, 24 Jul 2016 16:00:14 -0400 Subject: improved search by course code --- node_simple.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- testImports.js | 10 ++++++++-- 2 files changed, 64 insertions(+), 5 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) diff --git a/testImports.js b/testImports.js index 6e80262..0041b02 100644 --- a/testImports.js +++ b/testImports.js @@ -40,14 +40,14 @@ var questions_array = ["this is q1", "this is q2"]; });*/ -dbFile.retrieve_userSolutions_history("some_user name", function (bool, result) { +/*dbFile.retrieve_userSolutions_history("some_user name", function (bool, result) { if (bool == false) { console.log(result); } else { console.log(result); } -}); +});*/ /*dbFile.retrieve_userSolutions_count("some_user name", function (bool, result) { if (!bool) console.log(result); @@ -145,3 +145,9 @@ dbFile.retrieveFollows("some_user names", function (bool, result) { /*dbFile.remove_user("some_user name", function (bool, mssg) { console.log(mssg); });*/ + + +/* +dbFile.search_exams("cSc240", function (bool, mssg) { + console.log(mssg); +});*/ -- cgit v1.2.3