Browse Source

Use clean.layers to determine types

This allows removing the types middleware and simplifies some code.
pull/407/head
Julian Simioni 9 years ago
parent
commit
79883594b8
  1. 10
      controller/search.js
  2. 18
      middleware/_types.js
  3. 4
      routes/v1.js

10
controller/search.js

@ -11,7 +11,6 @@ function setup( backend, query ){
// do not run controller when a request // do not run controller when a request
// validation error has occurred. // validation error has occurred.
if( req.errors && req.errors.length ){ if( req.errors && req.errors.length ){
delete req.clean.type; //to declutter output
return next(); return next();
} }
@ -25,12 +24,9 @@ function setup( backend, query ){
body: query( req.clean ) body: query( req.clean )
}; };
// set the Elasticsearch types to filter by, // use layers field for filtering by type
// and remove the property from clean so the API if( req.clean.hasOwnProperty('layers') ){
// response output is cleaner cmd.type = req.clean.layers;
if( req.clean.hasOwnProperty('type') ){
cmd.type = req.clean.type;
delete req.clean.type;
} }
logger.debug( '[ES req]', JSON.stringify(cmd) ); logger.debug( '[ES req]', JSON.stringify(cmd) );

18
middleware/_types.js

@ -1,18 +0,0 @@
/**
* Take the layers specified by the layers parameter and use them to set the
* list of Elasticsearch types to filter.
*
* This has to be done outside the layers sanitizer since it doesn't know that
* the layers property is eventualy used to choose the _type.
*/
function middleware(req, res, next) {
req.clean = req.clean || {};
if (req.clean.hasOwnProperty('layers')) {
req.clean.type = req.clean.layers;
}
next();
}
module.exports = middleware;

4
routes/v1.js

@ -12,7 +12,6 @@ var sanitisers = {
/** ----------------------- middleware ------------------------ **/ /** ----------------------- middleware ------------------------ **/
var middleware = { var middleware = {
types: require('../middleware/_types'),
calcSize: require('../middleware/sizeCalculator') calcSize: require('../middleware/sizeCalculator')
}; };
@ -59,7 +58,6 @@ function addRoutes(app, peliasConfig) {
]), ]),
search: createRouter([ search: createRouter([
sanitisers.search.middleware, sanitisers.search.middleware,
middleware.types,
middleware.calcSize(), middleware.calcSize(),
controllers.search(), controllers.search(),
postProc.distances('focus.point.'), postProc.distances('focus.point.'),
@ -72,7 +70,6 @@ function addRoutes(app, peliasConfig) {
]), ]),
autocomplete: createRouter([ autocomplete: createRouter([
sanitisers.autocomplete.middleware, sanitisers.autocomplete.middleware,
middleware.types,
controllers.search(null, require('../query/autocomplete')), controllers.search(null, require('../query/autocomplete')),
postProc.distances('focus.point.'), postProc.distances('focus.point.'),
postProc.confidenceScores(peliasConfig), postProc.confidenceScores(peliasConfig),
@ -84,7 +81,6 @@ function addRoutes(app, peliasConfig) {
]), ]),
reverse: createRouter([ reverse: createRouter([
sanitisers.reverse.middleware, sanitisers.reverse.middleware,
middleware.types,
middleware.calcSize(), middleware.calcSize(),
controllers.search(undefined, reverseQuery), controllers.search(undefined, reverseQuery),
postProc.distances('point.'), postProc.distances('point.'),

Loading…
Cancel
Save