Browse Source

change query module interfaces to allow for additional meta data

pull/674/head
Diana Shkolnikov 8 years ago
parent
commit
3888ae0385
  1. 11
      controller/search.js
  2. 5
      query/autocomplete.js
  3. 5
      query/reverse.js
  4. 6
      query/search.js
  5. 6
      query/search_original.js
  6. 10
      test/unit/mock/search_query.js
  7. 19
      test/unit/query/autocomplete.js
  8. 18
      test/unit/query/reverse.js
  9. 18
      test/unit/query/search.js
  10. 18
      test/unit/query/search_original.js

11
controller/search.js

@ -10,6 +10,11 @@ function setup( config, backend, query ){
backend = backend || require('../src/backend'); backend = backend || require('../src/backend');
query = query || require('../query/search'); query = query || require('../query/search');
// verify that we don't let an old style query object slip through the cracks here
if (typeof query !== 'object' || !query.hasOwnProperty('query_type')) {
throw new Error('Search queries must return an object with query and query_type');
}
function controller( req, res, next ){ function controller( req, res, next ){
// do not run controller when a request // do not run controller when a request
// validation error has occurred. // validation error has occurred.
@ -31,7 +36,7 @@ function setup( config, backend, query ){
// log clean parameters for stats // log clean parameters for stats
logger.info('[req]', 'endpoint=' + req.path, cleanOutput); logger.info('[req]', 'endpoint=' + req.path, cleanOutput);
var query_body = query(req.clean); var query_body = query.query(req.clean);
// if there's no query to call ES with, skip the service // if there's no query to call ES with, skip the service
if (_.isUndefined(query_body)) { if (_.isUndefined(query_body)) {
@ -61,7 +66,9 @@ function setup( config, backend, query ){
// set response data // set response data
else { else {
res.data = docs; res.data = docs;
res.meta = meta; res.meta = meta || {};
// store the query_type for subsequent middleware
res.meta.query_type = query.query_type;
} }
logger.debug('[ES response]', docs); logger.debug('[ES response]', docs);
next(); next();

5
query/autocomplete.js

@ -117,4 +117,7 @@ function generateQuery( clean ){
return query.render( vs ); return query.render( vs );
} }
module.exports = generateQuery; module.exports = {
query: generateQuery,
query_type: 'autocomplete'
};

5
query/reverse.js

@ -74,4 +74,7 @@ function generateQuery( clean ){
return query.render( vs ); return query.render( vs );
} }
module.exports = generateQuery; module.exports = {
query: generateQuery,
query_type: 'reverse'
};

6
query/search.js

@ -134,4 +134,8 @@ function hasStreet(vs) {
return vs.isset('input:street'); return vs.isset('input:street');
} }
module.exports = generateQuery; module.exports = {
query: generateQuery,
// this could later be set to disambiguation when appropriate
query_type: 'fallback'
};

6
query/search_original.js

@ -128,4 +128,8 @@ function generateQuery( clean ){
return query.render( vs ); return query.render( vs );
} }
module.exports = generateQuery;
module.exports = {
query: generateQuery,
query_type: 'original'
};

10
test/unit/mock/search_query.js

@ -1,10 +1,8 @@
function setup(){
return query;
}
function query( clean ){ function query( clean ){
return clean; return clean;
} }
module.exports = setup; module.exports = {
query: query,
query_type: 'mock'
};

19
test/unit/query/autocomplete.js

@ -1,11 +1,24 @@
var query = require('../../../query/autocomplete');
var generate = require('../../../query/autocomplete'); var generate = query.query;
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof generate, 'function', 'valid function'); t.equal(typeof query, 'object', 'valid query object');
t.end();
});
test('valid interface', function(t) {
t.true(query.hasOwnProperty('query'), 'query is valid function');
t.true(query.hasOwnProperty('query_type'), 'query is valid function');
t.end();
});
test('valid interface', function(t) {
t.equal(typeof query.query, 'function', 'valid function');
t.end();
});
test('valid interface', function(t) {
t.equal(typeof query.query_type, 'string', 'valid query_type');
t.end(); t.end();
}); });
}; };

18
test/unit/query/reverse.js

@ -1,10 +1,24 @@
var generate = require('../../../query/reverse'); var query = require('../../../query/reverse');
var generate = query.query;
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof generate, 'function', 'valid function'); t.equal(typeof query, 'object', 'valid query object');
t.end();
});
test('valid interface', function(t) {
t.true(query.hasOwnProperty('query'), 'query is valid function');
t.true(query.hasOwnProperty('query_type'), 'query is valid function');
t.end();
});
test('valid interface', function(t) {
t.equal(typeof query.query, 'function', 'valid function');
t.end();
});
test('valid interface', function(t) {
t.equal(typeof query.query_type, 'string', 'valid query_type');
t.end(); t.end();
}); });
}; };

18
test/unit/query/search.js

@ -1,11 +1,25 @@
var generate = require('../../../query/search'); var query = require('../../../query/search');
var generate = query.query;
var fs = require('fs'); var fs = require('fs');
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof generate, 'function', 'valid function'); t.equal(typeof query, 'object', 'valid query object');
t.end();
});
test('valid interface', function(t) {
t.true(query.hasOwnProperty('query'), 'query is valid function');
t.true(query.hasOwnProperty('query_type'), 'query is valid function');
t.end();
});
test('valid interface', function(t) {
t.equal(typeof query.query, 'function', 'valid function');
t.end();
});
test('valid interface', function(t) {
t.equal(typeof query.query_type, 'string', 'valid query_type');
t.end(); t.end();
}); });
}; };

18
test/unit/query/search_original.js

@ -1,10 +1,24 @@
var generate = require('../../../query/search_original'); var query = require('../../../query/search_original');
var generate = query.query;
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) { test('valid interface', function(t) {
t.equal(typeof generate, 'function', 'valid function'); t.equal(typeof query, 'object', 'valid query object');
t.end();
});
test('valid interface', function(t) {
t.true(query.hasOwnProperty('query'), 'query is valid function');
t.true(query.hasOwnProperty('query_type'), 'query is valid function');
t.end();
});
test('valid interface', function(t) {
t.equal(typeof query.query, 'function', 'valid function');
t.end();
});
test('valid interface', function(t) {
t.equal(typeof query.query_type, 'string', 'valid query_type');
t.end(); t.end();
}); });
}; };

Loading…
Cancel
Save