diff --git a/controller/search.js b/controller/search.js index 36f6c3f1..aa644e47 100644 --- a/controller/search.js +++ b/controller/search.js @@ -13,6 +13,7 @@ function setup( backend, query ){ // backend command var cmd = { index: 'pelias', + searchType: 'dfs_query_then_fetch', body: query( req.clean ) }; diff --git a/package.json b/package.json index ffaa5841..41bde4b6 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "express": "^4.8.8", "geojson": "^0.2.0", "geojson-extent": "^0.3.1", - "geopipes-elasticsearch-backend": "git://github.com/geopipes/elasticsearch-backend#centroid-optional", + "geopipes-elasticsearch-backend": "0.0.11", + "pelias-suggester-pipeline": "2.0.2", "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 4ccc9f6d..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 ){ @@ -14,6 +15,7 @@ function generate( params ){ } var query = queries.distance( centroid, { size: params.size } ); + if (params.bbox) { query = queries.bbox ( centroid, { size: params.size, bbox: params.bbox } ); } @@ -27,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..7102651f --- /dev/null +++ b/query/sort.js @@ -0,0 +1,22 @@ +var population = 'population'; +var weights = require('pelias-suggester-pipeline').weights; + +module.exports = [ + { + '_script': { + 'file': population, + 'type': 'number', + 'order': 'desc' + } + }, + { + '_script': { + 'params': { + 'weights': weights + }, + 'file': 'weights', + 'type': 'number', + 'order': 'desc' + } + } +]; \ No newline at end of file diff --git a/test/unit/controller/search.js b/test/unit/controller/search.js index 19482eb9..7fd53ccb 100644 --- a/test/unit/controller/search.js +++ b/test/unit/controller/search.js @@ -53,7 +53,7 @@ module.exports.tests.functional_success = function(test, common) { test('functional success', function(t) { var backend = mockBackend( 'client/search/ok/1', function( cmd ){ - t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); + t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command'); }); var controller = setup( backend, mockQuery() ); var res = { @@ -78,7 +78,7 @@ module.exports.tests.functional_success = function(test, common) { module.exports.tests.functional_failure = function(test, common) { test('functional failure', function(t) { var backend = mockBackend( 'client/search/fail/1', function( cmd ){ - t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); + t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias', searchType: 'dfs_query_then_fetch' }, 'correct backend command'); }); var controller = setup( backend, mockQuery() ); var next = function( message ){ diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index 85a0720c..26c5e7e9 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -1,5 +1,6 @@ var generate = require('../../../query/reverse'); +var weights = require('pelias-suggester-pipeline').weights; module.exports.tests = {}; @@ -10,6 +11,27 @@ module.exports.tests.interface = function(test, common) { }); }; +var sort = [ + '_score', + { + '_script': { + 'file': 'population', + 'type': 'number', + 'order': 'desc' + } + }, + { + '_script': { + 'params': { + 'weights': weights + }, + 'file': 'weights', + 'type': 'number', + 'order': 'desc' + } + } +]; + module.exports.tests.query = function(test, common) { test('valid query', function(t) { var query = generate({ @@ -43,6 +65,7 @@ module.exports.tests.query = function(test, common) { } }, 'sort': [ + '_score', { '_geo_distance': { 'center_point': { @@ -53,8 +76,9 @@ module.exports.tests.query = function(test, common) { 'unit': 'km' } } - ], - 'size': 1 + ].concat(sort.slice(1)), + 'size': 1, + 'track_scores': true }; t.deepEqual(query, expected, 'valid reverse query'); diff --git a/test/unit/query/search.js b/test/unit/query/search.js index 5138f486..3e9d72f0 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -1,5 +1,6 @@ var generate = require('../../../query/search'); +var weights = require('pelias-suggester-pipeline').weights; module.exports.tests = {}; @@ -10,6 +11,27 @@ module.exports.tests.interface = function(test, common) { }); }; +var sort = [ + '_score', + { + '_script': { + 'file': 'population', + 'type': 'number', + 'order': 'desc' + } + }, + { + '_script': { + 'params': { + 'weights': weights + }, + 'file': 'weights', + 'type': 'number', + 'order': 'desc' + } + } +]; + module.exports.tests.query = function(test, common) { test('valid query', function(t) { var query = generate({ @@ -55,10 +77,11 @@ module.exports.tests.query = function(test, common) { } } }, - 'sort': [], - 'size': 10 + 'sort': sort, + 'size': 10, + 'track_scores': true }; - + t.deepEqual(query, expected, 'valid search query'); t.end(); }); @@ -106,8 +129,9 @@ module.exports.tests.query = function(test, common) { } } }, - 'sort': [], - 'size': 10 + 'sort': sort, + 'size': 10, + 'track_scores': true }; t.deepEqual(query, expected, 'valid search query'); @@ -139,7 +163,9 @@ module.exports.tests.query = function(test, common) { } } }, - 'size': 10 + 'size': 10, + 'sort': sort, + 'track_scores': true }; t.deepEqual(query, expected, 'valid search query'); @@ -186,6 +212,7 @@ module.exports.tests.query = function(test, common) { } }, 'sort': [ + '_score', { '_geo_distance': { 'center_point': { @@ -196,8 +223,9 @@ module.exports.tests.query = function(test, common) { 'unit': 'km' } } - ], - 'size': 10 + ].concat(sort.slice(1)), + 'size': 10, + 'track_scores': true }; t.deepEqual(query, expected, 'valid search query'); diff --git a/test/unit/query/sort.js b/test/unit/query/sort.js new file mode 100644 index 00000000..ad5f9037 --- /dev/null +++ b/test/unit/query/sort.js @@ -0,0 +1,52 @@ + +var generate = require('../../../query/sort'); +var population = 'population'; +var weights = require('pelias-suggester-pipeline').weights; + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('valid interface', function(t) { + t.equal(typeof generate, 'object', 'valid object'); + t.end(); + }); +}; + +var expected = [ + { + '_script': { + 'file': population, + 'type': 'number', + 'order': 'desc' + } + }, + { + '_script': { + 'params': { + 'weights': weights + }, + 'file': 'weights', + 'type': 'number', + 'order': 'desc' + } + } +]; + +module.exports.tests.query = function(test, common) { + test('valid part of query', function(t) { + var sort = generate; + t.deepEqual(sort, expected, 'valid sort part of the query'); + t.end(); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('sort query ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file diff --git a/test/unit/run.js b/test/unit/run.js index 20a5960d..4f5cffde 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -15,6 +15,7 @@ var tests = [ require('./sanitiser/coarse'), require('./query/indeces'), require('./query/suggest'), + require('./query/sort'), require('./query/search'), require('./query/reverse'), require('./helper/geojsonify'),