diff --git a/controller/search.js b/controller/search.js index 271d2899..70d403a8 100644 --- a/controller/search.js +++ b/controller/search.js @@ -17,6 +17,13 @@ function setup( config, backend, query ){ return next(); } + // do not run controller if there are already results + // this was added during libpostal integration. if the libpostal parse/query + // doesn't return anything then fallback to old search-engine-y behavior + if (res && res.hasOwnProperty('data')) { + return next(); + } + var cleanOutput = _.cloneDeep(req.clean); if (logging.isDNT(req)) { cleanOutput = logging.removeFields(cleanOutput); diff --git a/test/unit/controller/search.js b/test/unit/controller/search.js index 3f74fbd7..76f412c8 100644 --- a/test/unit/controller/search.js +++ b/test/unit/controller/search.js @@ -171,6 +171,28 @@ module.exports.tests.timeout = function(test, common) { }); }; +module.exports.tests.existing_results = function(test, common) { + test('res with existing data should not call backend', function(t) { + var backend = function() { + throw new Error('backend should not have been called'); + }; + var controller = setup( fakeDefaultConfig, backend, mockQuery() ); + + var req = { }; + // the existence of `data` means that there are already results so + // don't call the backend/query + var res = { data: [] }; + + var next = function() { + t.deepEqual(res, {data: []}); + t.end(); + }; + controller(req, res, next); + + }); + +}; + module.exports.all = function (tape, common) { function test(name, testFunction) {