Skip to content

Commit

Permalink
feat: add support to focus.country
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Jul 25, 2023
1 parent c70c7b5 commit 6bde165
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 30 deletions.
11 changes: 10 additions & 1 deletion query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var views = {
phrase_first_tokens_only: require('./view/phrase_first_tokens_only'),
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_point_filter: require('./view/focus_point_distance_filter'),
focus_country: require('./view/focus_country')
};

// add abbrevations for the fields pelias/parser is able to detect.
Expand Down Expand Up @@ -52,6 +53,7 @@ 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.custom_boosts( config.get('api.customBoosts') ) );

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

// focus country
if( _.isArray(clean['focus.country']) && !_.isEmpty(clean['focus.country']) ){
vs.set({
'multi_match:focus_country:input': clean['focus.country'].join(' ')
});
}

// 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 @@ -76,6 +76,11 @@ module.exports = _.merge({}, peliasQuery.defaults, {
'multi_match:boundary_country:analyzer': 'standard',
'multi_match:boundary_country:fields': ['parent.country_a', 'parent.dependency_a'],

// these options affect the `focus.country` hard filter
'multi_match:focus_country:analyzer': 'standard',
'multi_match:focus_country:fields': ['parent.country_a', 'parent.dependency_a'],
'multi_match:focus_country:boost': 1.5,

'admin:country:analyzer': 'peliasAdmin',
'admin:country:field': 'parent.country.ngram',
'admin:country:boost': 1,
Expand Down
18 changes: 18 additions & 0 deletions query/view/focus_country.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const peliasQuery = require('pelias-query');

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

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

if (!input || input.length < 1) {
return null;
}

return {
'function_score': {
'query': peliasQuery.view.leaf.multi_match(view_name)(vs),
'score_mode': 'multiply',
},
};
};
25 changes: 13 additions & 12 deletions sanitizer/_boundary_country.js → sanitizer/_countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ const _ = require('lodash');
const nonEmptyString = (v) => _.isString(v) && !_.isEmpty(v);
const iso3166 = require('../helper/iso3166');

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

// target input param
let countries = raw['boundary.country'];
let countries = raw[`${key}.country`];

// param 'boundary.country' is optional and should not
// param `*.country` is optional and should not
// error when simply not set by the user
if (!_.isNil(countries)) {

// must be valid string
if (!nonEmptyString(countries)) {
messages.errors.push('boundary.country is not a string');
messages.errors.push(`${key}.country is not a string`);
} else {
// support for multi countries
countries = countries.split(',').filter(nonEmptyString);
const invalidIsoCodes = countries.filter(country => !containsIsoCode(country));

// country list must contains at least one element
if (_.isArray(countries) && _.isEmpty(countries)) {
messages.errors.push('boundary.country is empty');
messages.errors.push(`${key}.country is empty`);
}

// must be a valid ISO 3166 code
Expand All @@ -35,23 +36,23 @@ function _sanitize(raw, clean) {
else {
// the only way for boundary.country to be assigned is if input is
// a string and a known ISO2 or ISO3
clean['boundary.country'] = countries.map(country => iso3166.iso3Code(country));
clean[`${key}.country`] = countries.map(country => iso3166.iso3Code(country));
}
}
}

return messages;
}
};

function containsIsoCode(isoCode) {
return iso3166.isISO2Code(isoCode) || iso3166.isISO3Code(isoCode);
}

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

module.exports = () => ({
sanitize: _sanitize,
expected: _expected
module.exports = (key = 'boundary') => ({
sanitize: _sanitize(key),
expected: _expected(key)
});
3 changes: 2 additions & 1 deletion sanitizer/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ module.exports.middleware = (_api_pelias_config) => {
sources_and_layers: require('../sanitizer/_sources_and_layers')(),
private: require('../sanitizer/_flag_bool')('private', false),
geo_autocomplete: require('../sanitizer/_geo_autocomplete')(),
boundary_country: require('../sanitizer/_boundary_country')(),
boundary_country: require('../sanitizer/_countries')('boundary'),
focus_country: require('../sanitizer/_countries')('focus'),
categories: require('../sanitizer/_categories')(),
request_language: require('../sanitizer/_request_language')(),
boundary_gid: require('../sanitizer/_boundary_gid')()
Expand Down
2 changes: 1 addition & 1 deletion sanitizer/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports.middleware = (_api_pelias_config) => {
size: require('../sanitizer/_size')(/* use defaults*/),
private: require('../sanitizer/_flag_bool')('private', false),
geo_reverse: require('../sanitizer/_geo_reverse')(),
boundary_country: require('../sanitizer/_boundary_country')(),
boundary_country: require('../sanitizer/_countries')('boundary'),
categories: require('../sanitizer/_categories')(),
request_language: require('../sanitizer/_request_language')()
};
Expand Down
2 changes: 1 addition & 1 deletion sanitizer/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports.middleware = (_api_pelias_config) => {
size: require('../sanitizer/_size')(/* use defaults*/),
private: require('../sanitizer/_flag_bool')('private', false),
geo_reverse: require('../sanitizer/_geo_reverse')(),
boundary_country: require('../sanitizer/_boundary_country')(),
boundary_country: require('../sanitizer/_countries')('boundary'),
request_language: require('../sanitizer/_request_language')(),
boundary_gid: require('../sanitizer/_boundary_gid')()
};
Expand Down
2 changes: 1 addition & 1 deletion sanitizer/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports.middleware = (_api_pelias_config) => {
sources_and_layers: require('../sanitizer/_sources_and_layers')(),
private: require('../sanitizer/_flag_bool')('private', false),
geo_search: require('../sanitizer/_geo_search')(),
boundary_country: require('../sanitizer/_boundary_country')(),
boundary_country: require('../sanitizer/_countries')('boundary'),
categories: require('../sanitizer/_categories')(),
// this can go away once geonames has been abrogated
geonames_warnings: require('../sanitizer/_geonames_warnings')(),
Expand Down
2 changes: 1 addition & 1 deletion sanitizer/structured_geocoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports.middleware = (_api_pelias_config) => {
sources_and_layers: require('../sanitizer/_sources_and_layers')(),
private: require('../sanitizer/_flag_bool')('private', false),
geo_search: require('../sanitizer/_geo_search')(),
boundary_country: require('../sanitizer/_boundary_country')(),
boundary_country: require('../sanitizer/_countries')('boundary'),
categories: require('../sanitizer/_categories')(),
request_language: require('../sanitizer/_request_language')(),
boundary_gid: require('../sanitizer/_boundary_gid')()
Expand Down
2 changes: 1 addition & 1 deletion test/unit/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var tests = [
require('./query/text_parser'),
require('./query/view/boost_sources_and_layers'),
require('./query/view/max_character_count_layer_filter'),
require('./sanitizer/_boundary_country'),
require('./sanitizer/_countries'),
require('./sanitizer/_debug'),
require('./sanitizer/_default_parameters'),
require('./sanitizer/_flag_bool'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var sanitizer = require('../../../sanitizer/_boundary_country')();
const countries = require('../../../sanitizer/_countries');
const sanitizer = countries();

module.exports.tests = {};

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

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

};

module.exports.all = function (tape, common) {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/sanitizer/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ module.exports.tests.sanitizers = function(test, common) {
}
};
},
'../sanitizer/_boundary_country': function () {
'../sanitizer/_countries': function (key) {
return {
sanitize: () => {
called_sanitizers.push('_boundary_country');
called_sanitizers.push(`_${key}_country`);
return { errors: [], warnings: [] };
}
};
Expand Down Expand Up @@ -160,6 +160,7 @@ module.exports.tests.sanitizers = function(test, common) {
'_flag_bool',
'_geo_autocomplete',
'_boundary_country',
'_focus_country',
'_categories',
'_request_language',
'_boundary_gid'
Expand Down
4 changes: 2 additions & 2 deletions test/unit/sanitizer/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ module.exports.tests.sanitize = function(test, common) {
}
};
},
'../sanitizer/_boundary_country': function () {
'../sanitizer/_countries': function (key) {
return {
sanitize: () => {
called_sanitizers.push('_boundary_country');
called_sanitizers.push(`_${key}_country`);
return { errors: [], warnings: [] };
}
};
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 @@ -86,10 +86,10 @@ module.exports.tests.sanitize = function(test, common) {
}
};
},
'../sanitizer/_boundary_country': function () {
'../sanitizer/_countries': function (key) {
return {
sanitize: () => {
called_sanitizers.push('_boundary_country');
called_sanitizers.push(`_${key}_country`);
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 @@ -102,10 +102,10 @@ module.exports.tests.sanitize = (test, common) => {
}
};
},
'../sanitizer/_boundary_country': function () {
'../sanitizer/_countries': function (key) {
return {
sanitize: () => {
called_sanitizers.push('_boundary_country');
called_sanitizers.push(`_${key}_country`);
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 @@ -114,10 +114,10 @@ module.exports.tests.sanitize = function(test, common) {
}
};
},
'../sanitizer/_boundary_country': function () {
'../sanitizer/_countries': function (key) {
return {
sanitize: () => {
called_sanitizers.push('_boundary_country');
called_sanitizers.push(`_${key}_country`);
return { errors: [], warnings: [] };
}
};
Expand Down

0 comments on commit 6bde165

Please sign in to comment.