Skip to content

Commit

Permalink
Merge pull request #101 from marklise/master
Browse files Browse the repository at this point in the history
MEM-510: Search Pagination.
  • Loading branch information
marklise authored Sep 25, 2017
2 parents 1d0c29d + 2f36165 commit bf16e67
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
11 changes: 8 additions & 3 deletions modules/core/server/controllers/core.dbmodel.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ _.extend (DBModel.prototype, {
// returns a promise, takes keywords, optional sort and populate
//
// -------------------------------------------------------------------------
searchMany : function (keywords, dateRangeStart, dateRangeEnd, project, proponent, ownership, fields, sortby) {
searchMany : function (keywords, dateRangeStart, dateRangeEnd, project, proponent, ownership, fields, sortby, page, limit) {
// console.log ('dbmodel.findMany:', keywords, fields);
var sort = sortby || this.sort;
var self = this;
Expand Down Expand Up @@ -353,10 +353,15 @@ _.extend (DBModel.prototype, {
var owns = ownership.split(',');
q = _.extend (q, { "ownership": { $text: { $search: ownership }}});
}
console.log("q:", q);

// console.log("q:", q);
// console.log("limit:", limit);
// console.log("limit:", Number(limit));
// console.log("page:", Number(page));
// console.log("skip:", page*limit);
self.model.find (q)
.sort (sort)
.skip (page*limit)
.limit (limit)
.populate (self.populate)
.select (fields)
.exec ()
Expand Down
31 changes: 21 additions & 10 deletions modules/search/server/routes/search.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = function (app) {
// console.log("req:", JSON.stringify(req.query));
if (req.query.types === 'document') {
var docController = new DocumentController(opts);
var page = 0;
var limit = 15;
var results = [];
var projects = null;
var p = new ProjectController(opts);
Expand All @@ -25,28 +27,34 @@ module.exports = function (app) {
if (req.query.project) {
projects = req.query.project.split(',');
projectQuery = _.extend (projectQuery, { "_id": {$in : projects}});
console.log("project query:", projectQuery);
// console.log("project query:", projectQuery);
}
if (req.query.projectcode) {
var codes = req.query.projectcode.split(',');
projectQuery = _.extend (projectQuery, { "code": {$in : codes}});
console.log("project query:", projectQuery);
// console.log("project query:", projectQuery);
}
// operator filtering (objectID's are coming in)
if (req.query.proponent) {
var ops = req.query.proponent.split(',');
projectQuery = _.extend (projectQuery, { "proponent": {$in : ops}});
console.log("organization query:", projectQuery);
// console.log("organization query:", projectQuery);
}
// owner filtering (strings are coming in)
if (req.query.ownership) {
projectQuery = _.extend (projectQuery, { $text: { $search: req.query.ownership }});
console.log("ownership query:", projectQuery);
// console.log("ownership query:", projectQuery);
}
if (req.query.page) {
page = parseInt(req.query.page, 10);
}
if (req.query.limit) {
limit = parseInt(req.query.limit, 10);
}
// We're filtering our searches on project and orgs
return p.findMany(projectQuery,"_id type name code ownership proponent")
.then(function (pdata) {
console.log("projects:", pdata.length);
// console.log("projects:", pdata.length);
projects = pdata;
if (projects && projects.length > 0) {
return docController.searchMany(req.query.search,
Expand All @@ -55,7 +63,10 @@ module.exports = function (app) {
req.query.project,
null, // not on this one - we already filtered on the org
null, // not on this one - we already filtered on the ownership
req.query.fields);
req.query.fields,
null, // sort by
page,
limit);
} else {
return [];
}
Expand All @@ -64,16 +75,16 @@ module.exports = function (app) {
_.each(docs, function (doc) {
results.push(doc);
});
console.log("docs", docs.length);
// console.log("docs", docs.length);
return results;
})
.then(function () {
console.log("prjs:", projects.length);
// console.log("prjs:", projects.length);

_.each(results, function (r) {
var found = _.find(projects, "_id", r.project);
var found = _.find(projects, {_id: r.project});
if (found) {
console.log("found the project, binding to document object:", found.code);
// console.log("found the project, binding to document object:", found.code);
r.project = found;
}
});
Expand Down

0 comments on commit bf16e67

Please sign in to comment.