From 425fdf2e88f05b7931b196ffa998ace8d6104f16 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 25 Sep 2014 16:50:06 +0100 Subject: [PATCH] refactor reverse query --- query/reverse.js | 30 ++++++------------------ test/unit/query/reverse.js | 48 +++++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/query/reverse.js b/query/reverse.js index 65a27500..21b755da 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -1,31 +1,15 @@ -var logger = require('../src/logger'); -// Build pelias search query +var logger = require('../src/logger'), + queries = require('geopipes-elasticsearch-backend').queries; + function generate( params ){ - var cmd = { - "query":{ - "filtered" : { - "query" : { - "match_all" : {} - }, - "filter" : { - "geo_distance" : { - "distance" : "1km", - "center_point" : { - "lat": params.lat, - "lon": params.lon - } - } - } - } - }, - "size": 1 + var centroid = { + lat: params.lat, + lon: params.lon }; - // logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) ); - return cmd; - + return queries.distance( centroid, { size: 1 } ); } module.exports = generate; \ No newline at end of file diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index 283b976e..4b54b646 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -15,26 +15,48 @@ module.exports.tests.query = function(test, common) { var query = generate({ lat: 29.49136, lon: -82.50622 }); + var expected = { - query:{ - filtered : { - query : { - match_all : {} + 'query': { + 'filtered': { + 'query': { + 'match_all': {} }, - filter : { - geo_distance : { - distance : '1km', - center_point : { - lat: 29.49136, - lon: -82.50622 + 'filter': { + 'bool': { + 'must': [ + { + 'geo_distance': { + 'distance': '50km', + 'distance_type': 'plane', + 'optimize_bbox': 'indexed', + '_cache': true, + 'center_point': { + 'lat': '29.49', + 'lon': '-82.51' + } } - } + } + ] + } } } }, - size: 1 + 'sort': [ + { + '_geo_distance': { + 'center_point': { + 'lat': 29.49136, + 'lon': -82.50622 + }, + 'order': 'asc', + 'unit': 'km' + } + } + ], + 'size': 1 }; - + t.deepEqual(query, expected, 'valid reverse query'); t.end(); });