From fcd1e6889d02e127973f51ff5e92560dc00f020c Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 14 Jan 2015 18:18:30 -0500 Subject: [PATCH] testing with suggest_old to compare performance --- app.js | 2 + controller/suggest_old.js | 153 ++++++++++++++++++++++++++++++++++++++ query/suggest_old.js | 44 +++++++++++ service/suggest_old.js | 28 +++++++ 4 files changed, 227 insertions(+) create mode 100644 controller/suggest_old.js create mode 100644 query/suggest_old.js create mode 100644 service/suggest_old.js diff --git a/app.js b/app.js index e77bc3a2..18bd5a31 100644 --- a/app.js +++ b/app.js @@ -22,6 +22,7 @@ var controllers = {}; controllers.index = require('./controller/index'); controllers.doc = require('./controller/doc'); controllers.suggest = require('./controller/suggest'); +controllers.suggest_old = require('./controller/suggest_old'); controllers.search = require('./controller/search'); /** ----------------------- routes ----------------------- **/ @@ -34,6 +35,7 @@ app.get( '/doc', sanitisers.doc.middleware, controllers.doc() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); +app.get( '/suggest_old', sanitisers.suggest.middleware, controllers.suggest_old() ); app.get( '/suggest/nearby', sanitisers.suggest.middleware, controllers.suggest(undefined, undefined, require('./helper/queryMixer').suggest_nearby) ); diff --git a/controller/suggest_old.js b/controller/suggest_old.js new file mode 100644 index 00000000..cbbd374e --- /dev/null +++ b/controller/suggest_old.js @@ -0,0 +1,153 @@ + +var service = { + suggest: require('../service/suggest_old'), + mget: require('../service/mget') +}; +var geojsonify = require('../helper/geojsonify').search; +var async = require('async'); + +function setup( backend, query ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + query = query || require('../query/suggest_old'); + + function controller( req, res, next ){ + + var cmd = { + index: 'pelias', + body: query( req.clean ) + }; + + var SIZE = req.clean.size || 10; + + var query_backend = function(cmd, callback) { + // query backend + service.suggest( backend, cmd, function( err, docs ){ + + // error handler + if( err ){ return next( err ); } + callback(null, docs); + }); + }; + + var dedup = function(combined) { + var unique_ids = []; + return combined.filter(function(item, pos) { + if (unique_ids.indexOf(item.text) === -1) { + unique_ids.push(item.text); + return true; + } + return false; + }); + }; + + var reply = function(docs) { + + // convert docs to geojson + var geojson = geojsonify( docs ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); + }; + + var respond = function(data) { + + // no documents suggested, return empty array to avoid ActionRequestValidationException + if( !Array.isArray( data ) || !data.length ){ + return reply([]); + } + + // map suggester output to mget query + var query = data.map( function( doc ) { + var idParts = doc.text.split(':'); + return { + _index: 'pelias', + _type: idParts[0], + _id: idParts.slice(1).join(':') + }; + }); + + service.mget( backend, query, function( err, docs ){ + + // error handler + if( err ){ return next( err ); } + + // reply + return reply( docs ); + + }); + + }; + + if (req.clean.input) { + var async_query; + + // admin only + req.admin = {}; + for (var k in req.clean) { req.admin[k] = req.clean[k]; } + req.admin.layers = ['admin0','admin1','admin2']; + + if (req.clean.input.length < 4 && isNaN(parseInt(req.clean.input, 10))) { + async_query = { + admin_3p: function(callback){ + cmd.body = query( req.admin, 3 ); + query_backend(cmd, callback); + }, + admin_1p: function(callback){ + cmd.body = query( req.admin, 1 ); + query_backend(cmd, callback); + }, + all_3p: function(callback) { + cmd.body = query( req.clean, 3 ); + query_backend(cmd, callback); + } + }; + } else { + async_query = { + all_5p: function(callback){ + cmd.body = query( req.clean, 5); + query_backend(cmd, callback); + }, + all_3p: function(callback){ + cmd.body = query( req.clean, 3); + query_backend(cmd, callback); + }, + all_1p: function(callback){ + cmd.body = query( req.clean, 1 ); + query_backend(cmd, callback); + }, + admin_1p: function(callback){ + cmd.body = query( req.admin ); + query_backend(cmd, callback); + } + }; + } + + async.parallel(async_query, function(err, results) { + // results is equal to: {a: docs, b: docs, c: docs} + var splice_length = parseInt((SIZE / Object.keys(results).length), 10); + var results_keys = Object.keys(async_query); + + var combined = []; + results_keys.forEach(function(key){ + combined = combined.concat(results[key].splice(0,splice_length)); + }); + combined = dedup(combined); + respond(combined); + }); + } else { + query_backend(cmd, function(err, results) { + respond(results); + }); + } + + } + + return controller; +} + +module.exports = setup; \ No newline at end of file diff --git a/query/suggest_old.js b/query/suggest_old.js new file mode 100644 index 00000000..29f2f0b3 --- /dev/null +++ b/query/suggest_old.js @@ -0,0 +1,44 @@ + +var logger = require('../src/logger'); + +// Build pelias suggest query +function generate( params, precision ){ + + var getPrecision = function(zoom) { + switch (true) { + case (zoom > 15): + return 5; // zoom: >= 16 + case (zoom > 9): + return 4; // zoom: 10-15 + case (zoom > 5): + return 3; // zoom: 6-9 + case (zoom > 3): + return 2; // zoom: 4-5 + default: + return 1; // zoom: 1-3 or when zoom: undefined + } + }; + + var cmd = { + 'pelias' : { + 'text' : params.input, + 'completion' : { + 'size' : params.size, + 'field' : 'suggest', + 'context': { + 'dataset': params.layers, + 'location': { + 'value': [ params.lon, params.lat ], + 'precision': precision || getPrecision(params.zoom) + } + } + } + } + }; + + // logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) ); + return cmd; + +} + +module.exports = generate; \ No newline at end of file diff --git a/service/suggest_old.js b/service/suggest_old.js new file mode 100644 index 00000000..fdf82fe9 --- /dev/null +++ b/service/suggest_old.js @@ -0,0 +1,28 @@ + +/** + + cmd can be any valid ES suggest command + +**/ + +function service( backend, cmd, cb ){ + + // query new backend + backend().client.suggest( cmd, function( err, data ){ + + // handle backend errors + if( err ){ return cb( err ); } + + // map returned documents + var docs = []; + if( data && Array.isArray( data.pelias ) && data.pelias.length ){ + docs = data.pelias[0].options || []; + } + + // fire callback + return cb( null, docs ); + }); + +} + +module.exports = service; \ No newline at end of file