From 19a20de9e01fd2785f39cbc4b7bfea29f562a912 Mon Sep 17 00:00:00 2001 From: "mansoor.sajjad" Date: Mon, 29 Aug 2022 14:28:28 +0200 Subject: [PATCH 1/3] Allow custom layers by removing the restriction for non-coarse layers only --- query/reverse.js | 3 +-- query/reverse_defaults.js | 1 - test/unit/query/reverse.js | 21 --------------------- 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/query/reverse.js b/query/reverse.js index 6f145680b..b10d00824 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -39,8 +39,7 @@ function generateQuery( clean ){ // layers if( _.isArray(clean.layers) && !_.isEmpty(clean.layers) ) { - // only include non-coarse layers - vs.var( 'layers', _.intersection(clean.layers, ['address', 'street', 'venue'])); + vs.var( 'layers', clean.layers); } // focus point to score by distance diff --git a/query/reverse_defaults.js b/query/reverse_defaults.js index 2bca3a59a..727c361a6 100644 --- a/query/reverse_defaults.js +++ b/query/reverse_defaults.js @@ -5,7 +5,6 @@ module.exports = _.merge({}, peliasQuery.defaults, { 'size': 1, 'track_scores': true, - 'layers': ['venue', 'address', 'street'], 'centroid:field': 'center_point', diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index 9813bf2f6..aac2bba46 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -209,27 +209,6 @@ module.exports.tests.layers = (test, common) => { }); - test('non-empty array clean.layers should only set non-coarse layers in vs', t => { - const clean = { - layers: all_layers - }; - - const query = proxyquire('../../../query/reverse', { - 'pelias-query': { - layout: { - FilteredBooleanQuery: MockQuery - }, - view: views, - Vars: require('pelias-query').Vars - }, - './reverse_defaults': {} - })(clean); - - t.deepEquals(query.body.vs.var('layers').toString(), ['address', 'venue', 'street']); - t.end(); - - }); - }; module.exports.tests.focus_point = (test, common) => { From af4af6ac46cb475d9e12665dfc5c7e92633a4a58 Mon Sep 17 00:00:00 2001 From: "mansoor.sajjad" Date: Thu, 29 Sep 2022 14:25:44 +0200 Subject: [PATCH 2/3] Using the coarse layers from configuration instead of using hard-corded non-coarse layers, inorder to support the custom layers for reverse endpoint. --- query/reverse.js | 3 +- test/unit/query/reverse.js | 63 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/query/reverse.js b/query/reverse.js index b10d00824..fb019871b 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -1,5 +1,6 @@ const _ = require('lodash'); const peliasQuery = require('pelias-query'); +const peliasConfig = require('pelias-config').generate(); const defaults = require('./reverse_defaults'); //------------------------------ @@ -39,7 +40,7 @@ function generateQuery( clean ){ // layers if( _.isArray(clean.layers) && !_.isEmpty(clean.layers) ) { - vs.var( 'layers', clean.layers); + vs.var( 'layers', _.difference(clean.layers, peliasConfig.api.targets.layer_aliases.coarse)); } // focus point to score by distance diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index aac2bba46..fdbbe1124 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -57,8 +57,7 @@ module.exports.tests.query = (test, common) => { t.notOk(query.body.vs.isset('input:categories')); t.notOk(query.body.vs.isset('boundary:gid')); - t.deepEquals(query.body.score_functions, [ - ]); + t.deepEquals(query.body.score_functions, []); t.deepEquals(query.body.filter_functions, [ 'boundary_circle view', @@ -209,6 +208,66 @@ module.exports.tests.layers = (test, common) => { }); + test('non-empty array clean.layers should only set non-coarse layers in vs, using user-defined configuration for coarse layers', t => { + const clean = { + layers: all_layers + }; + + const config_with_coarse_layers = { + generate: function () { + return { + api: { + targets: { + layer_aliases: { + coarse: [ + 'country', + 'county', + 'locality' + ] + } + } + } + }; + } + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-config': config_with_coarse_layers, + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.deepEquals(query.body.vs.var('layers').toString(), _.difference(clean.layers, ['country', 'county', 'locality'])); + t.end(); + + }); + + test('non-empty array clean.layers should only set non-coarse layers in vs, using default configuration for coarse layers', t => { + const clean = { + layers: all_layers + }; + + const query = proxyquire('../../../query/reverse', { + 'pelias-query': { + layout: { + FilteredBooleanQuery: MockQuery + }, + view: views, + Vars: require('pelias-query').Vars + }, + './reverse_defaults': {} + })(clean); + + t.deepEquals(query.body.vs.var('layers').toString(), [ 'address', 'venue', 'street' ]); + t.end(); + + }); }; module.exports.tests.focus_point = (test, common) => { From 0300b90163648f7d7f2dc8ff7a99724792661306 Mon Sep 17 00:00:00 2001 From: "mansoor.sajjad" Date: Thu, 29 Sep 2022 22:09:57 +0200 Subject: [PATCH 3/3] Using the get method on the config. --- query/reverse.js | 2 +- test/unit/query/reverse.js | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/query/reverse.js b/query/reverse.js index fb019871b..db9889fe5 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -40,7 +40,7 @@ function generateQuery( clean ){ // layers if( _.isArray(clean.layers) && !_.isEmpty(clean.layers) ) { - vs.var( 'layers', _.difference(clean.layers, peliasConfig.api.targets.layer_aliases.coarse)); + vs.var( 'layers', _.difference(clean.layers, peliasConfig.get('api.targets.layer_aliases.coarse', []))); } // focus point to score by distance diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index fdbbe1124..6455127ab 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -213,20 +213,25 @@ module.exports.tests.layers = (test, common) => { layers: all_layers }; + const coarseLayers = [ + 'country', + 'county', + 'locality' + ]; + const config_with_coarse_layers = { generate: function () { return { api: { targets: { layer_aliases: { - coarse: [ - 'country', - 'county', - 'locality' - ] + coarse: coarseLayers } } - } + }, + get: function () { + return coarseLayers; + }, }; } };