From c0f20aabd34dadccee402a17377be1228b602a5d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 9 Sep 2015 15:40:52 +0200 Subject: [PATCH] refactor: clean up sorting conditions --- package.json | 2 +- query/reverse.js | 3 +-- query/search.js | 13 +++++----- query/sort.js | 28 --------------------- test/unit/query/sort.js | 55 ----------------------------------------- test/unit/run.js | 1 - 6 files changed, 8 insertions(+), 94 deletions(-) delete mode 100644 query/sort.js delete mode 100644 test/unit/query/sort.js diff --git a/package.json b/package.json index a8769401..a1475a8e 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "pelias-config": "^1.0.1", "pelias-esclient": "0.0.25", "pelias-logger": "^0.0.8", - "pelias-query": "^1.4.0", + "pelias-query": "^1.5.0", "pelias-schema": "1.0.0", "pelias-suggester-pipeline": "2.0.2", "stats-lite": "^1.0.3", diff --git a/query/reverse.js b/query/reverse.js index 8aca4d94..724dbb8e 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -1,7 +1,6 @@ var peliasQuery = require('pelias-query'), - defaults = require('./defaults'), - sort = require('./sort'); + defaults = require('./defaults'); //------------------------------ // reverse geocode query diff --git a/query/search.js b/query/search.js index fa9b25d2..ea9f252b 100644 --- a/query/search.js +++ b/query/search.js @@ -1,7 +1,6 @@ var peliasQuery = require('pelias-query'), defaults = require('./defaults'), - sort = require('../query/sort'), adminFields = require('../helper/adminFields')(); //------------------------------ @@ -36,6 +35,11 @@ query.score( peliasQuery.view.admin('neighborhood') ); query.filter( peliasQuery.view.boundary_circle ); query.filter( peliasQuery.view.boundary_rect ); +// groovy scripts used to handle tie-breaking +query.sort( peliasQuery.view.sort_numeric_script('admin_boost') ); +query.sort( peliasQuery.view.sort_numeric_script('popularity') ); +query.sort( peliasQuery.view.sort_numeric_script('population') ); + // -------------------------------- /** @@ -183,12 +187,7 @@ function generateQuery( clean ){ } } - var result = query.render( vs ); - - // @todo: remove unnessesary sort conditions - result.sort = result.sort.concat( sort( clean ) ); - - return result; + return query.render( vs ); } module.exports = generateQuery; diff --git a/query/sort.js b/query/sort.js deleted file mode 100644 index 8ffff81d..00000000 --- a/query/sort.js +++ /dev/null @@ -1,28 +0,0 @@ - -module.exports = function( params ){ - var scriptsConfig = [ - { - '_script': { - 'file': 'admin_boost', - 'type': 'number', - 'order': 'desc' - } - }, - { - '_script': { - 'file': 'popularity', - 'type': 'number', - 'order': 'desc' - } - }, - { - '_script': { - 'file': 'population', - 'type': 'number', - 'order': 'desc' - } - } - ]; - - return scriptsConfig; -}; diff --git a/test/unit/query/sort.js b/test/unit/query/sort.js deleted file mode 100644 index e170cdad..00000000 --- a/test/unit/query/sort.js +++ /dev/null @@ -1,55 +0,0 @@ - -var generate = require('../../../query/sort'); - -module.exports.tests = {}; - -module.exports.tests.interface = function(test, common) { - test('valid interface', function(t) { - t.equal(typeof generate(), 'object', 'valid object'); - t.equal(typeof generate({input: 'foobar'}), 'object', 'valid object'); - t.end(); - }); -}; - -var expected = [ - { - '_script': { - 'file': 'admin_boost', - 'type': 'number', - 'order': 'desc' - } - }, - { - '_script': { - 'file': 'popularity', - 'type': 'number', - 'order': 'desc' - } - }, - { - '_script': { - 'file': 'population', - 'type': 'number', - 'order': 'desc' - } - } -]; - -module.exports.tests.query = function(test, common) { - test('valid part of query', function(t) { - t.deepEqual(generate(), expected, 'valid sort part of the query'); - t.deepEqual(generate( {} ), 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); - } -}; diff --git a/test/unit/run.js b/test/unit/run.js index cfd99715..2ca9be3e 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -13,7 +13,6 @@ var tests = [ require('./sanitiser/reverse'), require('./sanitiser/place'), require('./query/types'), - require('./query/sort'), require('./query/search'), require('./query/autocomplete'), require('./query/reverse'),