Skip to content

Commit

Permalink
feat: add support to focus.gid
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Jul 25, 2023
1 parent 6bde165 commit 0ed239f
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 34 deletions.
12 changes: 10 additions & 2 deletions query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var views = {
boost_exact_matches: require('./view/boost_exact_matches'),
max_character_count_layer_filter: require('./view/max_character_count_layer_filter'),
focus_point_filter: require('./view/focus_point_distance_filter'),
focus_country: require('./view/focus_country')
focus_multi_match: require('./view/focus_multi_match')
};

// add abbrevations for the fields pelias/parser is able to detect.
Expand Down Expand Up @@ -53,7 +53,8 @@ query.score( views.admin_multi_match_last( adminFields ), 'must');
query.score( peliasQuery.view.focus( peliasQuery.view.leaf.match_all ) );
query.score( peliasQuery.view.popularity( peliasQuery.view.leaf.match_all ) );
query.score( peliasQuery.view.population( peliasQuery.view.leaf.match_all ) );
query.score( views.focus_country );
query.score( views.focus_multi_match('focus_country') );
query.score( views.focus_multi_match('focus_gid') );
query.score( views.custom_boosts( config.get('api.customBoosts') ) );

// non-scoring hard filters
Expand Down Expand Up @@ -101,6 +102,13 @@ function generateQuery( clean ){
});
}

// focus gid
if( _.isString(clean['focus.gid']) && !_.isEmpty(clean['focus.gid']) ){
vs.set({
'multi_match:focus_gid:input': clean['focus.gid']
});
}

// pass the input tokens to the views so they can choose which tokens
// are relevant for their specific function.
if( _.isArray( clean.tokens ) ){
Expand Down
5 changes: 5 additions & 0 deletions query/autocomplete_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ module.exports = _.merge({}, peliasQuery.defaults, {
'multi_match:focus_country:fields': ['parent.country_a', 'parent.dependency_a'],
'multi_match:focus_country:boost': 1.5,

// these options affect the `focus.gid` hard filter
'multi_match:focus_gid:analyzer': 'standard',
'multi_match:focus_gid:fields': ['parent.*_id'],
'multi_match:focus_gid:boost': 1.5,

'admin:country:analyzer': 'peliasAdmin',
'admin:country:field': 'parent.country.ngram',
'admin:country:boost': 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const peliasQuery = require('pelias-query');

module.exports = function (vs) {
const view_name = 'focus_country';

module.exports = (view_name) => (vs) => {
const input = vs.var(`multi_match:${view_name}:input`).get();

if (!input || input.length < 1) {
Expand Down
3 changes: 1 addition & 2 deletions sanitizer/_countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const _ = require('lodash');
const nonEmptyString = (v) => _.isString(v) && !_.isEmpty(v);
const iso3166 = require('../helper/iso3166');

const _sanitize = (key) =>
(raw, clean) => {
const _sanitize = (key) => (raw, clean) => {
// error & warning messages
const messages = { errors: [], warnings: [] };

Expand Down
22 changes: 11 additions & 11 deletions sanitizer/_boundary_gid.js → sanitizer/_gids.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const _ = require('lodash');

function _sanitize(raw, clean) {
const _sanitize = (key) => (raw, clean) => {
// error & warning messages
var messages = { errors: [], warnings: [] };

// target input param
var boundary_gid = raw['boundary.gid'];
var boundary_gid = raw[`${key}.gid`];

// param 'boundary.gid' is optional and should not
// param `${key}.gid` is optional and should not
// error when simply not set by the user
// must be valid string
if (!_.isNil(boundary_gid)) {
if (!_.isString(boundary_gid) || _.isEmpty(boundary_gid)) {
messages.errors.push('boundary.gid is not a string');
messages.errors.push(`${key}.gid is not a string`);
}
else {
// boundary gid should take the form of source:layer:id,
Expand All @@ -22,7 +22,7 @@ function _sanitize(raw, clean) {
return x !== '';
});
if ( _.inRange(fields.length, 3, 5) ) {
clean['boundary.gid'] = fields.slice(2).join(':');
clean[`${key}.gid`] = fields.slice(2).join(':');
}
else {
messages.errors.push(boundary_gid + ' does not follow source:layer:id format');
Expand All @@ -31,13 +31,13 @@ function _sanitize(raw, clean) {
}

return messages;
}
};

function _expected(){
return [{ name: 'boundary.gid' }];
function _expected(key) {
return () => [{ name: `${key}.gid` }];
}

module.exports = () => ({
sanitize: _sanitize,
expected: _expected
module.exports = (key = 'boundary') => ({
sanitize: _sanitize(key),
expected: _expected(key)
});
7 changes: 5 additions & 2 deletions sanitizer/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const paramGroups = [
['boundary.circle.lon', 'boundary.circle.lat'],
['boundary.circle.radius'],
['boundary.country'],
['boundary.gid']
['boundary.gid'],
['focus.country'],
['focus.gid']
];

// middleware
Expand All @@ -45,7 +47,8 @@ module.exports.middleware = (_api_pelias_config) => {
focus_country: require('../sanitizer/_countries')('focus'),
categories: require('../sanitizer/_categories')(),
request_language: require('../sanitizer/_request_language')(),
boundary_gid: require('../sanitizer/_boundary_gid')()
boundary_gid: require('../sanitizer/_gids')('boundary'),
focus_gid: require('../sanitizer/_gids')('focus')
};

return ( req, res, next ) => {
Expand Down
2 changes: 1 addition & 1 deletion sanitizer/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports.middleware = (_api_pelias_config) => {
geo_reverse: require('../sanitizer/_geo_reverse')(),
boundary_country: require('../sanitizer/_countries')('boundary'),
request_language: require('../sanitizer/_request_language')(),
boundary_gid: require('../sanitizer/_boundary_gid')()
boundary_gid: require('../sanitizer/_gids')('boundary')
};

// middleware
Expand Down
2 changes: 1 addition & 1 deletion sanitizer/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports.middleware = (_api_pelias_config) => {
// this can go away once geonames has been abrogated
geonames_warnings: require('../sanitizer/_geonames_warnings')(),
request_language: require('../sanitizer/_request_language')(),
boundary_gid: require('../sanitizer/_boundary_gid')()
boundary_gid: require('../sanitizer/_gids')('boundary')
};

return ( req, res, next ) => {
Expand Down
2 changes: 1 addition & 1 deletion sanitizer/structured_geocoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports.middleware = (_api_pelias_config) => {
boundary_country: require('../sanitizer/_countries')('boundary'),
categories: require('../sanitizer/_categories')(),
request_language: require('../sanitizer/_request_language')(),
boundary_gid: require('../sanitizer/_boundary_gid')()
boundary_gid: require('../sanitizer/_gids')('boundary')
};

return ( req, res, next ) => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var tests = [
require('./sanitizer/_text_pelias_parser'),
require('./sanitizer/_tokenizer'),
require('./sanitizer/_categories'),
require('./sanitizer/_boundary_gid'),
require('./sanitizer/_gids'),
require('./sanitizer/nearby'),
require('./sanitizer/autocomplete'),
require('./sanitizer/structured_geocoding'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var sanitizer = require('../../../sanitizer/_boundary_gid')();
const gids = require('../../../sanitizer/_gids');
const sanitizer = gids();

module.exports.tests = {};

Expand Down Expand Up @@ -96,6 +97,13 @@ module.exports.tests.sanitize_boundary_gid = function(test, common) {
t.end();
});

test('return an array of expected custom parameters in object form for validation', (t) => {
const expected = [{ name: 'custom-name.gid' }];
const validParameters = gids('custom-name').expected();
t.deepEquals(validParameters, expected);
t.end();
});

};

module.exports.all = function (tape, common) {
Expand Down
7 changes: 4 additions & 3 deletions test/unit/sanitizer/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ module.exports.tests.sanitizers = function(test, common) {
}
};
},
'../sanitizer/_boundary_gid': () => {
'../sanitizer/_gids': (key) => {
return {
sanitize: () => {
called_sanitizers.push('_boundary_gid');
called_sanitizers.push(`_${key}_gid`);
return { errors: [], warnings: [] };
}
};
Expand All @@ -163,7 +163,8 @@ module.exports.tests.sanitizers = function(test, common) {
'_focus_country',
'_categories',
'_request_language',
'_boundary_gid'
'_boundary_gid',
'_focus_gid'
];

const req = {};
Expand Down
4 changes: 2 additions & 2 deletions test/unit/sanitizer/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ module.exports.tests.sanitize = function(test, common) {
}
};
},
'../sanitizer/_boundary_gid': () => {
'../sanitizer/_gids': (key) => {
return {
sanitize: () => {
called_sanitizers.push('_boundary_gid');
called_sanitizers.push(`_${key}_gid`);
return { errors: [], warnings: [] };
}
};
Expand Down
4 changes: 2 additions & 2 deletions test/unit/sanitizer/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ module.exports.tests.sanitize = (test, common) => {
}
};
},
'../sanitizer/_boundary_gid': () => {
'../sanitizer/_gids': (key) => {
return {
sanitize: () => {
called_sanitizers.push('_boundary_gid');
called_sanitizers.push(`_${key}_gid`);
return { errors: [], warnings: [] };
}
};
Expand Down
4 changes: 2 additions & 2 deletions test/unit/sanitizer/structured_geocoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ module.exports.tests.sanitize = function(test, common) {
}
};
},
'../sanitizer/_boundary_gid': () => {
'../sanitizer/_gids': (key) => {
return {
sanitize: () => {
called_sanitizers.push('_boundary_gid');
called_sanitizers.push(`_${key}_gid`);
return { errors: [], warnings: [] };
}
};
Expand Down

0 comments on commit 0ed239f

Please sign in to comment.