From c548a73cb968831cd55aab6758d3d31b008660e8 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 29 Oct 2018 17:02:02 -0400 Subject: [PATCH 1/2] fix(confidence): Update query type names in confidence score code https://github.com/pelias/api/commit/4adf4b3dd75057a67378f73a224309fe6dc7ffde renamed some queries to be quite a bit more informative, however it wasn't obvious that these query names were used elsewhere in the code. With those changes, no confidence score middleware was running, which this should help fix. --- controller/placeholder.js | 2 +- middleware/confidenceScore.js | 2 +- middleware/confidenceScoreFallback.js | 2 +- test/unit/controller/placeholder.js | 42 +++++++++---------- test/unit/middleware/confidenceScore.js | 14 +++---- .../middleware/confidenceScoreFallback.js | 30 ++++++------- 6 files changed, 46 insertions(+), 46 deletions(-) diff --git a/controller/placeholder.js b/controller/placeholder.js index 19c6fae9..9d4263fa 100644 --- a/controller/placeholder.js +++ b/controller/placeholder.js @@ -250,7 +250,7 @@ function setup(placeholderService, do_geometric_filters_apply, should_execute) { // boundary.country filter must happen after synthesis since multiple // lineages may produce different country docs res.meta = { - query_type: 'fallback' + query_type: 'search_fallback' }; res.data = results diff --git a/middleware/confidenceScore.js b/middleware/confidenceScore.js index 49597251..f9d3ede1 100644 --- a/middleware/confidenceScore.js +++ b/middleware/confidenceScore.js @@ -29,7 +29,7 @@ function computeScores(req, res, next) { // do nothing if no result data set or if query is not of the original variety if (check.undefined(req.clean) || check.undefined(res) || check.undefined(res.data) || check.undefined(res.meta) || - res.meta.query_type !== 'original') { + res.meta.query_type !== 'search_original') { return next(); } diff --git a/middleware/confidenceScoreFallback.js b/middleware/confidenceScoreFallback.js index 96d378b8..7e03afae 100644 --- a/middleware/confidenceScoreFallback.js +++ b/middleware/confidenceScoreFallback.js @@ -22,7 +22,7 @@ function computeScores(req, res, next) { // later add disambiguation to this list if (check.undefined(req.clean) || check.undefined(res) || check.undefined(res.data) || check.undefined(res.meta) || - res.meta.query_type !== 'fallback') { + res.meta.query_type !== 'search_fallback') { return next(); } diff --git a/test/unit/controller/placeholder.js b/test/unit/controller/placeholder.js index 6d9df491..54a1bf7c 100644 --- a/test/unit/controller/placeholder.js +++ b/test/unit/controller/placeholder.js @@ -212,7 +212,7 @@ module.exports.tests.success = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -330,7 +330,7 @@ module.exports.tests.success = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -393,7 +393,7 @@ module.exports.tests.success = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -453,7 +453,7 @@ module.exports.tests.success = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -518,7 +518,7 @@ module.exports.tests.success = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -583,7 +583,7 @@ module.exports.tests.success = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -756,7 +756,7 @@ module.exports.tests.result_filtering = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -874,7 +874,7 @@ module.exports.tests.result_filtering = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -1075,7 +1075,7 @@ module.exports.tests.result_filtering = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -1191,7 +1191,7 @@ module.exports.tests.result_filtering = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -1345,7 +1345,7 @@ module.exports.tests.result_filtering = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -1480,7 +1480,7 @@ module.exports.tests.result_filtering = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -1629,7 +1629,7 @@ module.exports.tests.result_filtering = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -1779,7 +1779,7 @@ module.exports.tests.result_filtering = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -1906,7 +1906,7 @@ module.exports.tests.lineage_errors = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -1980,7 +1980,7 @@ module.exports.tests.lineage_errors = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -2053,7 +2053,7 @@ module.exports.tests.lineage_errors = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -2113,7 +2113,7 @@ module.exports.tests.geometry_errors = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -2172,7 +2172,7 @@ module.exports.tests.centroid_errors = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -2232,7 +2232,7 @@ module.exports.tests.centroid_errors = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { @@ -2302,7 +2302,7 @@ module.exports.tests.boundingbox_errors = (test, common) => { controller(req, res, () => { const expected_res = { meta: { - query_type: 'fallback' + query_type: 'search_fallback' }, data: [ { diff --git a/test/unit/middleware/confidenceScore.js b/test/unit/middleware/confidenceScore.js index 316b2de6..32504e1e 100644 --- a/test/unit/middleware/confidenceScore.js +++ b/test/unit/middleware/confidenceScore.js @@ -47,7 +47,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'original' + query_type: 'search_original' } }; @@ -89,7 +89,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'original' + query_type: 'search_original' } }; @@ -125,7 +125,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'original' + query_type: 'search_original' } }; @@ -134,7 +134,7 @@ module.exports.tests.confidenceScore = function(test, common) { t.end(); }); - test('should only work for original query_type', function(t) { + test('should only work for search_original query_type', function(t) { var req = { clean: { text: '123 Main St, City, NM', @@ -161,7 +161,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -191,7 +191,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'original' + query_type: 'search_original' } }; @@ -223,7 +223,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'original' + query_type: 'search_original' } }; diff --git a/test/unit/middleware/confidenceScoreFallback.js b/test/unit/middleware/confidenceScoreFallback.js index 7bca1c33..13c5dd01 100644 --- a/test/unit/middleware/confidenceScoreFallback.js +++ b/test/unit/middleware/confidenceScoreFallback.js @@ -47,7 +47,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'original' + query_type: 'search_fallback' } }; @@ -89,7 +89,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -127,7 +127,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -163,7 +163,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -197,7 +197,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -230,7 +230,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -265,7 +265,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -300,7 +300,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -336,7 +336,7 @@ module.exports.tests.confidenceScore = function(test, common) { }], meta: { scores: [10], - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -360,7 +360,7 @@ module.exports.tests.confidenceScore = function(test, common) { layer: 'locality' }], meta: { - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -384,7 +384,7 @@ module.exports.tests.confidenceScore = function(test, common) { layer: 'localadmin' }], meta: { - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -408,7 +408,7 @@ module.exports.tests.confidenceScore = function(test, common) { layer: 'region' }], meta: { - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -433,7 +433,7 @@ module.exports.tests.confidenceScore = function(test, common) { layer: 'country' }], meta: { - query_type: 'fallback' + query_type: 'search_fallback' } }; @@ -461,7 +461,7 @@ module.exports.tests.confidenceScore = function(test, common) { layer: 'postalcode' }], meta: { - query_type: 'fallback' + query_type: 'search_fallback' } }; confidenceScore(req, res, function() {}); @@ -485,7 +485,7 @@ module.exports.tests.confidenceScore = function(test, common) { layer: 'postalcode' }], meta: { - query_type: 'fallback' + query_type: 'search_fallback' } }; confidenceScore(req, res, function() {}); From 2a668612edc1ce383835f84b6bbf9da60bb40c7b Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Mon, 29 Oct 2018 17:29:54 -0400 Subject: [PATCH 2/2] feat(confidence): Add support for new query names --- middleware/confidenceScoreFallback.js | 7 +- .../middleware/confidenceScoreFallback.js | 76 +++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/middleware/confidenceScoreFallback.js b/middleware/confidenceScoreFallback.js index 7e03afae..a8380b15 100644 --- a/middleware/confidenceScoreFallback.js +++ b/middleware/confidenceScoreFallback.js @@ -21,8 +21,11 @@ function computeScores(req, res, next) { // do nothing if no result data set or if the query is not of the fallback variety // later add disambiguation to this list if (check.undefined(req.clean) || check.undefined(res) || - check.undefined(res.data) || check.undefined(res.meta) || - res.meta.query_type !== 'search_fallback') { + check.undefined(res.data) || check.undefined(res.meta)) { + return next(); + } + + if (['search_fallback', 'address_search_with_ids', 'structured'].includes(res.meta.queryType)) { return next(); } diff --git a/test/unit/middleware/confidenceScoreFallback.js b/test/unit/middleware/confidenceScoreFallback.js index 13c5dd01..78871fd9 100644 --- a/test/unit/middleware/confidenceScoreFallback.js +++ b/test/unit/middleware/confidenceScoreFallback.js @@ -137,6 +137,82 @@ module.exports.tests.confidenceScore = function(test, common) { t.end(); }); + test('placeholder style search queries should get confidence score', function(t) { + var req = { + clean: { + text: '123 Main St, City, NM', + parsed_text: { + number: 123, + street: 'Main St', + city: 'City', + state: 'NM' + } + } + }; + var res = { + data: [{ + _score: 10, + found: true, + value: 1, + layer: 'address', + center_point: { lat: 100.1, lon: -50.5 }, + name: { default: 'test name1' }, + parent: { + country: ['country1'], + region: ['region1'], + county: ['city1'] + } + }], + meta: { + scores: [10], + query_type: 'address_search_with_ids' + } + }; + + confidenceScore(req, res, function() {}); + t.equal(res.data[0].confidence, 1.0, 'max score was set'); + t.equal(res.data[0].match_type, 'exact', 'exact match indicated'); + t.end(); + }); + + test('structured search queries should get confidence score', function(t) { + var req = { + clean: { + text: '123 Main St, City, NM', + parsed_text: { + number: 123, + street: 'Main St', + city: 'City', + state: 'NM' + } + } + }; + var res = { + data: [{ + _score: 10, + found: true, + value: 1, + layer: 'address', + center_point: { lat: 100.1, lon: -50.5 }, + name: { default: 'test name1' }, + parent: { + country: ['country1'], + region: ['region1'], + county: ['city1'] + } + }], + meta: { + scores: [10], + query_type: 'structured' + } + }; + + confidenceScore(req, res, function() {}); + t.equal(res.data[0].confidence, 1.0, 'max score was set'); + t.equal(res.data[0].match_type, 'exact', 'exact match indicated'); + t.end(); + }); + test('no fallback street query should have max score', function(t) { var req = { clean: {