Skip to content

Commit

Permalink
feat(nearby): Popularity sort
Browse files Browse the repository at this point in the history
  • Loading branch information
bboure committed Sep 22, 2020
1 parent 893d7a0 commit 5f663ea
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions query/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions query/reverse_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down
32 changes: 32 additions & 0 deletions sanitizer/_sort.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions sanitizer/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')(),
Expand Down

0 comments on commit 5f663ea

Please sign in to comment.