From 5f663ea8b3b20102377481307a1cf1fd176e421b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt?= Date: Fri, 10 Jul 2020 14:31:19 +0200 Subject: [PATCH] feat(nearby): Popularity sort --- query/reverse.js | 5 +++++ query/reverse_defaults.js | 2 ++ sanitizer/_sort.js | 32 ++++++++++++++++++++++++++++++++ sanitizer/nearby.js | 1 + 4 files changed, 40 insertions(+) create mode 100644 sanitizer/_sort.js diff --git a/query/reverse.js b/query/reverse.js index 6f145680b..aaf2f708c 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -12,6 +12,7 @@ var query = new peliasQuery.layout.FilteredBooleanQuery(); // scoring boost query.sort( peliasQuery.view.sort_distance ); +query.sort( peliasQuery.view.sort_popularity ); // non-scoring hard filters query.filter( peliasQuery.view.boundary_circle ); @@ -96,6 +97,10 @@ function generateQuery( clean ){ vs.var('input:categories', clean.categories); } + if (_.isString(clean.sort)) { + vs.var('sort:field', clean.sort); + } + return { type: 'reverse', body: query.render(vs) diff --git a/query/reverse_defaults.js b/query/reverse_defaults.js index 2bca3a59a..ddeef69cd 100644 --- a/query/reverse_defaults.js +++ b/query/reverse_defaults.js @@ -9,6 +9,8 @@ module.exports = _.merge({}, peliasQuery.defaults, { 'centroid:field': 'center_point', + 'sort:field': 'distance', + 'sort:distance:order': 'asc', 'sort:distance:distance_type': 'plane', diff --git a/sanitizer/_sort.js b/sanitizer/_sort.js new file mode 100644 index 000000000..6b737b85d --- /dev/null +++ b/sanitizer/_sort.js @@ -0,0 +1,32 @@ +const _ = require('lodash'); +const DEFAULT_SORT = 'distance'; + +const allowed_values = ['distance', 'popularity']; + +function _setup(){ + + return { + sanitize: function _sanitize( raw, clean ){ + + // error & warning messages + var messages = { errors: [], warnings: [] }; + + clean.sort = raw.sort; + + if( clean.sort && !allowed_values.includes(clean.sort) ){ + messages.warnings.push('invalid \'sort\', using \'distance\''); + clean.sort = DEFAULT_SORT; + } + + return messages; + }, + + expected: function _expected() { + // add size as a valid parameter + return [{ name: 'sort' }]; + } + }; +} + +// export function +module.exports = _setup; diff --git a/sanitizer/nearby.js b/sanitizer/nearby.js index e2103248d..e2e050100 100644 --- a/sanitizer/nearby.js +++ b/sanitizer/nearby.js @@ -12,6 +12,7 @@ module.exports.middleware = (_api_pelias_config) => { sources_and_layers: require('../sanitizer/_sources_and_layers')(), geonames_deprecation: require('../sanitizer/_geonames_deprecation')(), size: require('../sanitizer/_size')(/* use defaults*/), + sort: require('../sanitizer/_sort')(), private: require('../sanitizer/_flag_bool')('private', false), geo_reverse: require('../sanitizer/_geo_reverse')(), boundary_country: require('../sanitizer/_boundary_country')(),