Browse Source

sorting function based on population and weights plus tests

using-pelias-ngram
Harish Krishna 10 years ago
parent
commit
1a740f16df
  1. 1
      package.json
  2. 8
      query/reverse.js
  3. 5
      query/search.js
  4. 27
      query/sort.js
  5. 6
      test/unit/query/reverse.js
  6. 5
      test/unit/query/search.js

1
package.json

@ -37,6 +37,7 @@
"geojson": "^0.2.0", "geojson": "^0.2.0",
"geojson-extent": "^0.3.1", "geojson-extent": "^0.3.1",
"geopipes-elasticsearch-backend": "git://github.com/geopipes/elasticsearch-backend#sort-scoring-function", "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", "is-object": "^1.0.1",
"pelias-esclient": "0.0.25", "pelias-esclient": "0.0.25",
"toobusy": "^0.2.4" "toobusy": "^0.2.4"

8
query/reverse.js

@ -1,6 +1,7 @@
var logger = require('../src/logger'), var logger = require('../src/logger'),
queries = require('geopipes-elasticsearch-backend').queries; queries = require('geopipes-elasticsearch-backend').queries,
sort = require('../query/sort');
function generate( params ){ function generate( params ){
@ -9,7 +10,10 @@ function generate( params ){
lon: params.lon 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; module.exports = generate;

5
query/search.js

@ -1,6 +1,7 @@
var logger = require('../src/logger'), var logger = require('../src/logger'),
queries = require('geopipes-elasticsearch-backend').queries; queries = require('geopipes-elasticsearch-backend').queries,
sort = require('../query/sort');
function generate( params ){ function generate( params ){
@ -28,6 +29,8 @@ function generate( params ){
} }
}; };
query.sort = query.sort.concat(sort);
return query; return query;
} }

27
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'
}
}
];

6
test/unit/query/reverse.js

@ -11,7 +11,6 @@ module.exports.tests.interface = function(test, common) {
}; };
var sort = [ var sort = [
'_score',
{ {
'_script': { '_script': {
'script': 'if (doc.containsKey(\'population\')) { return doc[\'population\'].value } else { return 0 }', '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': { '_geo_distance': {
'center_point': { 'center_point': {
@ -89,7 +89,7 @@ module.exports.tests.query = function(test, common) {
'unit': 'km' 'unit': 'km'
} }
} }
]), ].concat(sort),
'size': 1, 'size': 1,
'track_scores': true 'track_scores': true
}; };

5
test/unit/query/search.js

@ -225,7 +225,8 @@ module.exports.tests.query = function(test, common) {
} }
} }
}, },
'sort': sort.concat([ 'sort': [
'_score',
{ {
'_geo_distance': { '_geo_distance': {
'center_point': { 'center_point': {
@ -236,7 +237,7 @@ module.exports.tests.query = function(test, common) {
'unit': 'km' 'unit': 'km'
} }
} }
]), ].concat(sort.slice(1)),
'size': 10, 'size': 10,
'track_scores': true 'track_scores': true
}; };

Loading…
Cancel
Save