Browse Source

feat: Merge pull request #1097 from pelias/reduce-reverse-maxSize

Reduce minimum result size of reverse Elasticsearch query
pull/1099/head
Julian Simioni 7 years ago committed by GitHub
parent
commit
2cec2d5c6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      middleware/sizeCalculator.js
  2. 2
      routes/v1.js
  3. 49
      test/unit/middleware/sizeCalculator.js
  4. 2
      test/unit/run.js

14
middleware/sizeCalculator.js

@ -2,19 +2,23 @@ var _ = require('lodash');
var SIZE_PADDING = 2; var SIZE_PADDING = 2;
var MIN_QUERY_SIZE = 20; var DEFAULT_MIN_QUERY_SIZE = 20;
/** /**
* Utility for calculating query result size * Utility for calculating query result size
* incorporating padding for dedupe process * incorporating padding for dedupe process
*/ */
function setup() { function setup(min_size) {
if (min_size === undefined) {
min_size = DEFAULT_MIN_QUERY_SIZE;
}
return function setQuerySize(req, res, next) { return function setQuerySize(req, res, next) {
if (_.isUndefined(req.clean) || _.isUndefined(req.clean.size)) { if (_.isUndefined(req.clean) || _.isUndefined(req.clean.size)) {
return next(); return next();
} }
req.clean.querySize = calculateSize(req.clean.size); req.clean.querySize = calculateSize(req.clean.size, min_size);
next(); next();
}; };
} }
@ -25,8 +29,8 @@ function setup() {
* @param {number} cleanSize * @param {number} cleanSize
* @returns {number} * @returns {number}
*/ */
function calculateSize(cleanSize) { function calculateSize(cleanSize, min_size) {
return Math.max(MIN_QUERY_SIZE, cleanSize * SIZE_PADDING); return Math.max(min_size, cleanSize * SIZE_PADDING);
} }
module.exports = setup; module.exports = setup;

2
routes/v1.js

@ -344,7 +344,7 @@ function addRoutes(app, peliasConfig) {
reverse: createRouter([ reverse: createRouter([
sanitizers.reverse.middleware, sanitizers.reverse.middleware,
middleware.requestLanguage, middleware.requestLanguage,
middleware.calcSize(), middleware.calcSize(2),
controllers.search(peliasConfig.api, esclient, queries.reverse, nonCoarseReverseShouldExecute), controllers.search(peliasConfig.api, esclient, queries.reverse, nonCoarseReverseShouldExecute),
controllers.coarse_reverse(pipService, coarseReverseShouldExecute), controllers.coarse_reverse(pipService, coarseReverseShouldExecute),
postProc.distances('point.'), postProc.distances('point.'),

49
test/unit/helper/sizeCalculator.js → test/unit/middleware/sizeCalculator.js

@ -1,18 +1,17 @@
var calcSizeMiddleware = require('../../../middleware/sizeCalculator.js');
var calcSize = require('../../../middleware/sizeCalculator.js')();
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.interface = function(test, common) { module.exports.tests.interface = function(test, common) {
test('interface', function(t) { test('interface', function(t) {
t.equal(typeof calcSize, 'function', 'valid function'); t.equal(typeof calcSizeMiddleware, 'function', 'valid function');
t.end(); t.end();
}); });
}; };
module.exports.tests.valid = function(test, common) { module.exports.tests.valid = function(test, common) {
var req = { clean: {} }; var req = { clean: {} };
function setup(val) { function setupQuery(val) {
if (isNaN(val)) { if (isNaN(val)) {
delete req.clean.size; delete req.clean.size;
} }
@ -23,7 +22,8 @@ module.exports.tests.valid = function(test, common) {
} }
test('size=0', function (t) { test('size=0', function (t) {
setup(0); setupQuery(0);
const calcSize = calcSizeMiddleware();
calcSize(req, {}, function () { calcSize(req, {}, function () {
t.equal(req.clean.querySize, 20); t.equal(req.clean.querySize, 20);
t.end(); t.end();
@ -31,7 +31,8 @@ module.exports.tests.valid = function(test, common) {
}); });
test('size=1', function (t) { test('size=1', function (t) {
setup(1); setupQuery(1);
const calcSize = calcSizeMiddleware();
calcSize(req, {}, function () { calcSize(req, {}, function () {
t.equal(req.clean.querySize, 20); t.equal(req.clean.querySize, 20);
t.end(); t.end();
@ -39,7 +40,8 @@ module.exports.tests.valid = function(test, common) {
}); });
test('size=10', function (t) { test('size=10', function (t) {
setup(10); setupQuery(10);
const calcSize = calcSizeMiddleware();
calcSize(req, {}, function () { calcSize(req, {}, function () {
t.equal(req.clean.querySize, 20); t.equal(req.clean.querySize, 20);
t.end(); t.end();
@ -47,7 +49,8 @@ module.exports.tests.valid = function(test, common) {
}); });
test('size=20', function (t) { test('size=20', function (t) {
setup(20); setupQuery(20);
const calcSize = calcSizeMiddleware();
calcSize(req, {}, function () { calcSize(req, {}, function () {
t.equal(req.clean.querySize, 40); t.equal(req.clean.querySize, 40);
t.end(); t.end();
@ -55,12 +58,40 @@ module.exports.tests.valid = function(test, common) {
}); });
test('no size', function (t) { test('no size', function (t) {
setup(); setupQuery();
const calcSize = calcSizeMiddleware();
calcSize(req, {}, function () { calcSize(req, {}, function () {
t.equal(req.clean.hasOwnProperty('querySize'), false); t.equal(req.clean.hasOwnProperty('querySize'), false);
t.end(); t.end();
}); });
}); });
test('no size, min query size 10', function (t) {
setupQuery();
const calcSize = calcSizeMiddleware(10);
calcSize(req, {}, function () {
t.equal(req.clean.hasOwnProperty('querySize'), false);
t.end();
});
});
test('size 5, min query size 10', function (t) {
setupQuery(5);
const calcSize = calcSizeMiddleware(10);
calcSize(req, {}, function () {
t.equal(req.clean.querySize, 10);
t.end();
});
});
test('size 3, min query size 2', function (t) {
setupQuery(3);
const calcSize = calcSizeMiddleware(2);
calcSize(req, {}, function () {
t.equal(req.clean.querySize, 6);
t.end();
});
});
}; };
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {

2
test/unit/run.js

@ -36,7 +36,6 @@ var tests = [
require('./helper/geojsonify'), require('./helper/geojsonify'),
require('./helper/logging'), require('./helper/logging'),
require('./helper/type_mapping'), require('./helper/type_mapping'),
require('./helper/sizeCalculator'),
require('./helper/stackTraceLine'), require('./helper/stackTraceLine'),
require('./middleware/access_log'), require('./middleware/access_log'),
require('./middleware/accuracy'), require('./middleware/accuracy'),
@ -52,6 +51,7 @@ var tests = [
require('./middleware/parseBBox'), require('./middleware/parseBBox'),
require('./middleware/sendJSON'), require('./middleware/sendJSON'),
require('./middleware/normalizeParentIds'), require('./middleware/normalizeParentIds'),
require('./middleware/sizeCalculator'),
require('./middleware/sortResponseData'), require('./middleware/sortResponseData'),
require('./middleware/trimByGranularity'), require('./middleware/trimByGranularity'),
require('./middleware/trimByGranularityStructured'), require('./middleware/trimByGranularityStructured'),

Loading…
Cancel
Save