From 15a3d4f4899ca4bb2cabc69cdd9c5c032c8fe883 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 31 Oct 2018 16:49:56 +0100 Subject: [PATCH] feat(inter_layer_deduping): dedupe between layers and prefer some layers over others --- helper/diffPlaces.js | 4 + middleware/dedupe.js | 35 ++++++--- test/unit/middleware/dedupe.js | 140 +++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 11 deletions(-) diff --git a/helper/diffPlaces.js b/helper/diffPlaces.js index dfe0e5db..e42d4525 100644 --- a/helper/diffPlaces.js +++ b/helper/diffPlaces.js @@ -14,6 +14,10 @@ function isLayerDifferent(item1, item2){ ( item2.layer === 'venue' || !_.includes( canonicalLayers, item2.layer ) ) ){ return false; } + // consider some layers to be synonymous + if( _.includes( placeTypes, item1.layer ) && _.includes( placeTypes, item2.layer ) ){ + return false; + } return true; } return false; diff --git a/middleware/dedupe.js b/middleware/dedupe.js index 18054335..be0c8413 100644 --- a/middleware/dedupe.js +++ b/middleware/dedupe.js @@ -4,6 +4,10 @@ const isDifferent = require('../helper/diffPlaces').isDifferent; const canonical_sources = require('../helper/type_mapping').canonical_sources; const field = require('../helper/fieldValue'); +// when performing inter-layer deduping, layers coming earlier in this list take +// preference to those appearing later or not at all. +const layerPreferences = [ 'locality', 'country', 'localadmin', 'region', 'neighbourhood' ]; + function dedupeResults(req, res, next) { // do nothing if request data is invalid @@ -79,19 +83,28 @@ function isPreferred(existingHit, candidateHit) { if( !_.includes(canonical_sources, candidateHit.source) && _.includes(canonical_sources, existingHit.source) ){ return true; } + // prefer certain layers over others + if( existingHit.layer !== candidateHit.layer && _.isArray( layerPreferences ) ){ + for( let i=0; i