Skip to content

Commit

Permalink
Implemented full text search for universities, institutes & workingGr…
Browse files Browse the repository at this point in the history
…oups in member-client #15
  • Loading branch information
nicho90 committed May 5, 2017
1 parent 1981bc4 commit dc4e77a
Show file tree
Hide file tree
Showing 31 changed files with 1,243 additions and 221 deletions.
28 changes: 17 additions & 11 deletions controllers/documents/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ exports.request = function(req, res) {
}
},
function(client, done, member, callback) {
console.log(req.body);

var query;
var params = [];
Expand Down Expand Up @@ -179,19 +178,26 @@ exports.request = function(req, res) {
}

// Prepare search-query
var search_text = req.body.search_text;
var search_array = search_text.split(' ');
var search_query_text = "";
for(var i=0; i<search_array.length; i++){
if(i !== search_array.length-1){
search_query_text = search_query_text + search_array[i] + ":*|";
} else {
search_query_text = search_query_text + search_array[i] + ":*";
if(!req.body.search_text || req.body.search_text === ""){
callback(new Error("No search text found"), 400);
} else {
var search_text = req.body.search_text;
var search_array = search_text.split(' ');
var search_query_text = "";

for(var i=0; i<search_array.length; i++){
if(i !== search_array.length-1){
search_query_text = search_query_text + search_array[i] + ":*|";
} else {
search_query_text = search_query_text + search_array[i] + ":*";
}
}

params.push(search_query_text);

callback(null, client, done, query, params);
}
params.push(search_query_text);

callback(null, client, done, query, params);
},
function(client, done, query, params, callback) {
// Database query
Expand Down
83 changes: 83 additions & 0 deletions controllers/institutes/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var async = require('async');
var colors = require('colors');
var pg = require('pg');
var types = require('pg').types;
types.setTypeParser(1700, 'text', parseFloat);
var _ = require('underscore');
var pool = require('../../server.js').pool;

var fs = require("fs");
var dir = "/../../sql/queries/institutes/";
var query_search_institutes = fs.readFileSync(__dirname + dir + 'search.sql', 'utf8').toString();


// SEARCH
exports.request = function(req, res) {

async.waterfall([
function(callback){
// Connect to database
pool.connect(function(err, client, done) {
if(err) {
callback(err, 500);
} else {
callback(null, client, done);
}
});
},
function(client, done, callback) {
// Preparing parameters
var params = [];

// Pagination parameters
params.push(Number(req.query.offset) || null);
params.push(Number(req.query.limit) || null);

// Sorting
params.push(req.query.orderby || 'name.asc');

// Filter by former status
params.push(String(req.query.former));

// Prepare search-query
if(!req.body.search_text || req.body.search_text === ""){
callback(new Error("No search text found"), 400);
} else {
var search_text = req.body.search_text;
var search_array = search_text.split(' ');
var search_query_text = "";

for(var i=0; i<search_array.length; i++){
if(i !== search_array.length-1){
search_query_text = search_query_text + search_array[i] + ":*|";
} else {
search_query_text = search_query_text + search_array[i] + ":*";
}
}

params.push(search_query_text);

callback(null, client, done, params);
}

},
function(client, done, params, callback) {
// Database query
client.query(query_search_institutes, params, function(err, result) {
done();
if (err) {
callback(err, 500);
} else {
callback(null, 200, result.rows);
}
});
}
], function(err, code, result) {
if(err){
console.error(colors.red(err));
res.status(code).send(err.message);
} else {
res.status(code).send(result);
}
});
};
106 changes: 106 additions & 0 deletions controllers/institutes/search_by_university.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
var async = require('async');
var colors = require('colors');
var pg = require('pg');
var types = require('pg').types;
types.setTypeParser(1700, 'text', parseFloat);
var _ = require('underscore');
var pool = require('../../server.js').pool;

var fs = require("fs");
var dir_1 = "/../../sql/queries/universities/";
var dir_2 = "/../../sql/queries/institutes/";
var query_get_university = fs.readFileSync(__dirname + dir_1 + 'get.sql', 'utf8').toString();
var query_search_institutes_by_university = fs.readFileSync(__dirname + dir_2 + 'search_by_university.sql', 'utf8').toString();


// SEARCH BY UNIVERSITY
exports.request = function(req, res) {

async.waterfall([
function(callback){
// Connect to database
pool.connect(function(err, client, done) {
if(err) {
callback(err, 500);
} else {
callback(null, client, done);
}
});
},
function(client, done, callback) {
// Database query
client.query(query_get_university, [
req.params.university_id
], function(err, result) {
done();
if (err) {
callback(err, 500);
} else {
// Check if University exists
if (result.rows.length === 0) {
callback(new Error("University not found"), 404);
} else {
callback(null, client, done);
}
}
});
},
function(client, done, callback) {
// Preparing parameters
var params = [];

// Pagination parameters
params.push(Number(req.query.offset) || null);
params.push(Number(req.query.limit) || null);

// Sorting
params.push(req.query.orderby || 'name.asc');

// Filter by former status
params.push(String(req.query.former));

// Filter by university
params.push(req.params.university_id);

// Prepare search-query
if(!req.body.search_text || req.body.search_text === ""){
callback(new Error("No search text found"), 400);
} else {
var search_text = req.body.search_text;
var search_array = search_text.split(' ');
var search_query_text = "";

for(var i=0; i<search_array.length; i++){
if(i !== search_array.length-1){
search_query_text = search_query_text + search_array[i] + ":*|";
} else {
search_query_text = search_query_text + search_array[i] + ":*";
}
}

params.push(search_query_text);

callback(null, client, done, params);
}

},
function(client, done, params, callback) {
// Database query
client.query(query_search_institutes_by_university, params, function(err, result) {
done();
if (err) {
callback(err, 500);
} else {
callback(null, 200, result.rows);
}
});
}
], function(err, code, result) {
if(err){
console.error(colors.red(err));
res.status(code).send(err.message);
} else {
res.status(code).send(result);
}
});
};
80 changes: 80 additions & 0 deletions controllers/universities/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var async = require('async');
var colors = require('colors');
var pg = require('pg');
var types = require('pg').types;
types.setTypeParser(1700, 'text', parseFloat);
var _ = require('underscore');
var pool = require('../../server.js').pool;

var fs = require("fs");
var dir = "/../../sql/queries/universities/";
var query_search_universities = fs.readFileSync(__dirname + dir + 'search.sql', 'utf8').toString();


// SEARCH
exports.request = function(req, res) {

async.waterfall([
function(callback){
// Connect to database
pool.connect(function(err, client, done) {
if(err) {
callback(err, 500);
} else {
callback(null, client, done);
}
});
},
function(client, done, callback) {
// Preparing parameters
var params = [];

// Pagination parameters
params.push(Number(req.query.offset) || null);
params.push(Number(req.query.limit) || null);

// Sorting
params.push(req.query.orderby || 'name.asc');

// Prepare search-query
if(!req.body.search_text || req.body.search_text === ""){
callback(new Error("No search text found"), 400);
} else {
var search_text = req.body.search_text;
var search_array = search_text.split(' ');
var search_query_text = "";

for(var i=0; i<search_array.length; i++){
if(i !== search_array.length-1){
search_query_text = search_query_text + search_array[i] + ":*|";
} else {
search_query_text = search_query_text + search_array[i] + ":*";
}
}

params.push(search_query_text);

callback(null, client, done, params);
}

},
function(client, done, params, callback) {
// Database query
client.query(query_search_universities, params, function(err, result) {
done();
if (err) {
callback(err, 500);
} else {
callback(null, 200, result.rows);
}
});
}
], function(err, code, result) {
if(err){
console.error(colors.red(err));
res.status(code).send(err.message);
} else {
res.status(code).send(result);
}
});
};
Loading

0 comments on commit dc4e77a

Please sign in to comment.