Skip to content

Commit

Permalink
searchResults decoupled from solr client
Browse files Browse the repository at this point in the history
  • Loading branch information
schatzopoulos committed Oct 24, 2016
1 parent 6ce2973 commit eb3b5e9
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 155 deletions.
7 changes: 4 additions & 3 deletions application/controllers/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Handles the requests by executing stuff and replying to the client. Uses promise
const boom = require('boom'), //Boom gives us some predefined http codes and proper responses
solrClient = require('../solr/solrClient'),
helper = require('../solr/helper'),
initIndex = require('../solr/initIndex');
initIndex = require('../solr/initIndex'),
searchResults = require('../solr/searchResults');

module.exports = {

Expand All @@ -17,10 +18,10 @@ module.exports = {
// parse query params
helper.parse(request.params.queryparams).then( (queryparams) => {
// fetch results from SOLR
solrClient.get(queryparams).then( (results) => {
searchResults.get(queryparams).then( (results) => {
reply(results);
}).catch( (error) => {
request.log('solrClient.get', error);
request.log('searchResults.get', error);
reply(boom.badImplementation());
});
}).catch( (error) => {
Expand Down
23 changes: 23 additions & 0 deletions application/controllers/suggest_handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Handles the requests by executing stuff and replying to the client. Uses promises to get stuff done.
*/

'use strict';

const boom = require('boom'), //Boom gives us some predefined http codes and proper responses
suggestions = require('../solr/suggestions');

module.exports = {

// Suggest users for autosuggest functionality or INTERNAL_SERVER_ERROR
findUsers: function(request, reply){
suggestions.findUsers(request.params.q).then( (results) => {
reply(results);
}).catch( (error) => {
request.log('suggestions.findUsers', error);
reply(boom.badImplementation());
});

}

};
102 changes: 59 additions & 43 deletions application/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,69 @@ Each route implementes a basic parameter/payload validation and a swagger API do
'use strict';

const Joi = require('joi'),
handlers = require('./controllers/handler');
handlers = require('./controllers/handler'),
suggestHandlers = require('./controllers/suggest_handlers');

module.exports = function(server) {

// get query results from SOLR
server.route({
method: 'GET',
path: '/get/{queryparams}',
handler: handlers.getResults,
config: {
validate: {
params: {
queryparams: Joi.string()
},
},
tags: ['api'],
description: 'Get SOLR search results'
}
});
server.route({
method: 'GET',
path: '/get/{queryparams}',
handler: handlers.getResults,
config: {
validate: {
params: {
queryparams: Joi.string()
},
},
tags: ['api'],
description: 'Get SOLR search results'
}
});

// index all data from db
server.route({
method: 'GET',
path: '/index',
handler: handlers.indexAll,
config: {
validate: {
params: {
},
},
tags: ['api'],
description: 'Index all data from DB in SOLR'
}
});
// index all data from db
server.route({
method: 'GET',
path: '/index',
handler: handlers.indexAll,
config: {
validate: {
params: {
},
},
tags: ['api'],
description: 'Index all data from DB in SOLR'
}
});

// index all data from db
server.route({
method: 'GET',
path: '/delete',
handler: handlers.deleteAll,
config: {
validate: {
params: {
},
},
tags: ['api'],
description: 'Delete all documents from SOLR'
}
});
// index all data from db
server.route({
method: 'GET',
path: '/delete',
handler: handlers.deleteAll,
config: {
validate: {
params: {
},
},
tags: ['api'],
description: 'Delete all documents from SOLR'
}
});

server.route({
method: 'GET',
path: '/suggest/users/{q}',
handler: suggestHandlers.findUsers,
config: {
validate: {
params: {
q: Joi.string()
},
},
tags: ['api'],
description: 'Get autosuggest results for users'
}
});
};
12 changes: 7 additions & 5 deletions application/solr/objectCollections/slides.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {

// form child docs
for(let i=0; i<slideDbObj.revisions.length; i++){
solrClient.query('solr_id:slide_revision_' + slideDbObj._id + '-' + slideDbObj.revisions[i].id).then( (res) => {
solrClient.query('q=solr_id:slide_revision_' + slideDbObj._id + '-' + slideDbObj.revisions[i].id).then( (res) => {
if(res.numFound > 0){
// console.log('solr_id:slide_revision_' + slideDbObj._id + '-' + slideDbObj.revisions[i].id + ' is existing');
this.updateSlideRevision(slideDbObj._id, slideDbObj.revisions[i]);
Expand All @@ -34,6 +34,8 @@ module.exports = {
// console.log('solr_id:slide_revision_' + slideDbObj._id + '-' + slideDbObj.revisions[i].id + ' is new');
this.newSlideRevision(slideDbObj._id, slideDbObj.revisions[i]);
}
}).catch( (err) => {
console.log('solrClient.query:' + err);
});
}
},
Expand All @@ -46,18 +48,18 @@ module.exports = {
if(prop.indexOf('revisions') >= 0){
for(let i in slideUpdateObj.data.$set.revisions){
let rev = slideUpdateObj.data.$set.revisions[i];
solrClient.query('solr_id:slide_revision_' + slideUpdateObj.targetId + '-' + rev.id).then( (result) => {
solrClient.query('q=solr_id:slide_revision_' + slideUpdateObj.targetId + '-' + rev.id).then( (result) => {
// update existing slide revision
if(result.numFound > 0){
// console.log('solr_id:slide_revision_' + slideUpdateObj.targetId + '-' + rev.id + ' is existing');
this.updateSlideRevision(slideUpdateObj.targetId, rev);
}
// new slide revision
else{
// console.log('solr_id:slide_revision_' + slideUpdateObj.targetId + '-' + rev.id + ' is new');
this.newSlideRevision(slideUpdateObj.targetId, rev);
}
});
}).catch( (err) => {
console.log('solrClient.query:' + err);
});;
}
}
else{
Expand Down
11 changes: 5 additions & 6 deletions application/solr/objectCollections/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
rootDoc.organization = userDbObj.organization;
rootDoc.kind = 'user';

console.log('new ' + JSON.stringify(rootDoc));
// solrClient.addDocs(rootDoc).then( (result) => solrClient.commit() );
// console.log('new ' + JSON.stringify(rootDoc));
solrClient.addDocs(rootDoc).then( (result) => solrClient.commit() );
},

update: function(userDbObj){
Expand All @@ -35,7 +35,7 @@ module.exports = {
prop === 'surname' ||
prop === 'forename' ||
prop === 'email' ||
prop === 'organization'){ //do not store active in root deck
prop === 'organization'){

updateObj[prop] = {'set': userDbObj.data.$set[prop]};
}
Expand All @@ -45,12 +45,11 @@ module.exports = {
// change made to root doc
if(!co.isEmpty(updateObj)){
updateObj.solr_id = 'user_' + userDbObj.targetId;
console.log('update ' + JSON.stringify(updateObj));
// console.log('update ' + JSON.stringify(updateObj));

// solrClient.addDocs(updateObj).then( (result) => solrClient.commit() );
solrClient.addDocs(updateObj).then( (result) => solrClient.commit() );
}
}

}

};
72 changes: 72 additions & 0 deletions application/solr/searchResults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

const solrClient = require('./solrClient'),
helper = require('./helper');


module.exports = {

get: function (params) {
let promise = new Promise( (resolve, reject) => {

let solr_params = helper.getSolrParameters(params);

let queryString = '';

let rootQ = '';
// query only child docs
if(params.hasOwnProperty('fields')){
rootQ = (solr_params.rootQ !== '') ? solr_params.rootQ + ' AND ' : '';
}
// query both parent and child docs
else{
rootQ = '(' + solr_params.childQ +' AND (kind:slide OR kind:deck)) OR ';
}

let childQAndFQ = solr_params.childQ;
childQAndFQ += (solr_params.childFQ !== '') ? (' AND ' + solr_params.childFQ) : '';

queryString =
'q=' + rootQ + '{!join from=solr_parent_id to=solr_id score=max v=\'' + childQAndFQ + '\'}' +
'&fq=' + solr_params.rootFQ +
'&fl=*,revisions:[subquery]' +
'&revisions.q=' + solr_params.childQ + ' AND {!terms f=solr_parent_id v=$row.solr_id}' +
'&revisions.fq=' + solr_params.childFQ +
// '&revisions.sort=id asc' +
'&rows=50&wt=json';


// let requestUri = 'http://' + solrUri + queryString;
solrClient.query(queryString).then( (resp) => {
this.checkResponse(resp).then( (res) => {

for(let i in res){
resp.docs[res[i].item].revisions = res[i];
}
resolve(resp);
}).catch( (error) => {
reject('in checkResponse. solrResponse: ' + solrResponse);
});
}).catch( (err) => {
reject(err);
});
});
return promise;
},
checkResponse: function(response){
let promises = [];

for (let i in response.docs){
let curDoc = response.docs[i];

// if results has no children
if(curDoc.revisions.numFound === 0){

// fetch active child
let query = 'q=solr_parent_id:' + curDoc.solr_id + ' AND active:true';
promises.push(solrClient.query(query, i));
}
}
return Promise.all(promises);
},
};

0 comments on commit eb3b5e9

Please sign in to comment.