From 6b0b124d8f2a1cf08263be32a244f0e90697da3c Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Fri, 28 Apr 2017 12:13:25 -0400 Subject: [PATCH] Always query for a minimum of 20 results from Elasticsearch Fallback and interpolation queries rely on several results coming back from Elasticsearch to ensure the best result is returned. It was possible that queries with `size=1` would not return enough results from Elasticsearch. This change ensures even with `size=1` a sufficient number of results are returned. Fixes https://github.com/pelias/pelias/issues/562 --- middleware/sizeCalculator.js | 11 ++++------- test/unit/helper/sizeCalculator.js | 12 ++++++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/middleware/sizeCalculator.js b/middleware/sizeCalculator.js index 88334d3b..69409e45 100644 --- a/middleware/sizeCalculator.js +++ b/middleware/sizeCalculator.js @@ -2,6 +2,8 @@ var _ = require('lodash'); var SIZE_PADDING = 2; +var MIN_QUERY_SIZE = 20; + /** * Utility for calculating query result size * incorporating padding for dedupe process @@ -24,12 +26,7 @@ function setup() { * @returns {number} */ function calculateSize(cleanSize) { - switch (cleanSize || 1) { - case 1: - return 1; - default: - return cleanSize * SIZE_PADDING; - } + return Math.max(MIN_QUERY_SIZE, cleanSize * SIZE_PADDING); } -module.exports = setup; \ No newline at end of file +module.exports = setup; diff --git a/test/unit/helper/sizeCalculator.js b/test/unit/helper/sizeCalculator.js index 41d854d4..6c8d8d22 100644 --- a/test/unit/helper/sizeCalculator.js +++ b/test/unit/helper/sizeCalculator.js @@ -25,7 +25,7 @@ module.exports.tests.valid = function(test, common) { test('size=0', function (t) { setup(0); calcSize(req, {}, function () { - t.equal(req.clean.querySize, 1); + t.equal(req.clean.querySize, 20); t.end(); }); }); @@ -33,7 +33,7 @@ module.exports.tests.valid = function(test, common) { test('size=1', function (t) { setup(1); calcSize(req, {}, function () { - t.equal(req.clean.querySize, 1); + t.equal(req.clean.querySize, 20); t.end(); }); }); @@ -46,6 +46,14 @@ module.exports.tests.valid = function(test, common) { }); }); + test('size=20', function (t) { + setup(20); + calcSize(req, {}, function () { + t.equal(req.clean.querySize, 40); + t.end(); + }); + }); + test('no size', function (t) { setup(); calcSize(req, {}, function () {