diff --git a/middleware/dedupe.js b/middleware/dedupe.js index 115b82a03..dacd0b055 100644 --- a/middleware/dedupe.js +++ b/middleware/dedupe.js @@ -113,6 +113,9 @@ function isPreferred(existingHit, candidateHit) { // https://github.com/pelias/api/issues/872 if( !_.has(existingHit, 'address_parts.zip') && _.has(candidateHit, 'address_parts.zip') ){ return true; } + // if the existing hit HAS a postcode, and this candidate does NOT, keep the existing hit + if( _.has(existingHit, 'address_parts.zip') && + !_.has(candidateHit, 'address_parts.zip') ){ return false; } // prefer non-canonical sources over canonical ones if( !_.includes(canonical_sources, candidateHit.source) && diff --git a/test/unit/middleware/dedupe.js b/test/unit/middleware/dedupe.js index fbe7014e3..92fd3443b 100644 --- a/test/unit/middleware/dedupe.js +++ b/test/unit/middleware/dedupe.js @@ -396,6 +396,42 @@ module.exports.tests.priority = function(test, common) { }); }); + test('osm with zip takes priority over openaddresses without zip, regardless of order of results', function (t) { + var req = { + clean: { + text: '100 Main St', + size: 100 + } + }; + var res = { + data: [ + { + 'name': { 'default': '100 Main St' }, + 'source': 'openstreetmap', + 'source_id': '654321', + 'layer': 'address', + 'address_parts': { + 'zip': '54321' + } + }, + { + 'name': { 'default': '100 Main St' }, + 'source': 'openaddresses', + 'source_id': '123456', + 'layer': 'address', + 'address_parts': {} + }, + ] + }; + + var expectedCount = 1; + dedupe(req, res, function () { + t.equal(res.data.length, expectedCount, 'results have fewer items than before'); + t.deepEqual(res.data[0].source_id, '654321', 'openstreetmap result with zip won'); + t.end(); + }); + }); + test('works with name aliases', function (t) { var req = { clean: {