diff --git a/package.json b/package.json index 73742830..f54aa0e8 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "geojson": "^0.2.0", "geojson-extent": "^0.3.1", "geopipes-elasticsearch-backend": "git://github.com/geopipes/elasticsearch-backend#sort-scoring-function", + "pelias-model": "git://github.com/pelias/model#multiplier", "is-object": "^1.0.1", "pelias-esclient": "0.0.25", "toobusy": "^0.2.4" diff --git a/query/reverse.js b/query/reverse.js index 3ada51be..35b86ade 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -1,6 +1,7 @@ var logger = require('../src/logger'), - queries = require('geopipes-elasticsearch-backend').queries; + queries = require('geopipes-elasticsearch-backend').queries, + sort = require('../query/sort'); function generate( params ){ @@ -9,7 +10,10 @@ function generate( params ){ lon: params.lon }; - return queries.distance( centroid, { size: params.size || 1 } ); + var query = queries.distance( centroid, { size: params.size || 1 } ); + query.sort = query.sort.concat(sort); + + return query; } module.exports = generate; \ No newline at end of file diff --git a/query/search.js b/query/search.js index 524db577..9d9916d3 100644 --- a/query/search.js +++ b/query/search.js @@ -1,6 +1,7 @@ var logger = require('../src/logger'), - queries = require('geopipes-elasticsearch-backend').queries; + queries = require('geopipes-elasticsearch-backend').queries, + sort = require('../query/sort'); function generate( params ){ @@ -28,6 +29,8 @@ function generate( params ){ } }; + query.sort = query.sort.concat(sort); + return query; } diff --git a/query/sort.js b/query/sort.js new file mode 100644 index 00000000..90eb265b --- /dev/null +++ b/query/sort.js @@ -0,0 +1,27 @@ +var population = 'population'; +var weights = require('pelias-model').weights; + +module.exports = [ + { + '_script': { + 'script': 'if (doc.containsKey(\''+ population + '\'))' + + ' { return doc[\'' + population + '\'].value }' + + ' else { return 0 }', + 'type': 'number', + 'order': 'desc' + } + }, + { + '_script': { + 'params': { + 'weights': weights + }, + 'script': 'if (doc.containsKey(\'_type\')) { '+ + 'type=doc[\'_type\'].value; '+ + 'return ( type in weights ) ? weights[ type ] : 0 }' + + 'else { return 0 }', + 'type': 'number', + 'order': 'desc' + } + } +]; \ No newline at end of file diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index b04a3368..fb423895 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -11,7 +11,6 @@ module.exports.tests.interface = function(test, common) { }; var sort = [ - '_score', { '_script': { 'script': 'if (doc.containsKey(\'population\')) { return doc[\'population\'].value } else { return 0 }', @@ -78,7 +77,8 @@ module.exports.tests.query = function(test, common) { } } }, - 'sort': sort.concat([ + 'sort': [ + '_score', { '_geo_distance': { 'center_point': { @@ -89,7 +89,7 @@ module.exports.tests.query = function(test, common) { 'unit': 'km' } } - ]), + ].concat(sort), 'size': 1, 'track_scores': true }; diff --git a/test/unit/query/search.js b/test/unit/query/search.js index f7042139..dc1f504e 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -225,7 +225,8 @@ module.exports.tests.query = function(test, common) { } } }, - 'sort': sort.concat([ + 'sort': [ + '_score', { '_geo_distance': { 'center_point': { @@ -236,7 +237,7 @@ module.exports.tests.query = function(test, common) { 'unit': 'km' } } - ]), + ].concat(sort.slice(1)), 'size': 10, 'track_scores': true };