Browse Source

Merge pull request #16 from pelias/refactor_search

Refactor search
pull/18/head
Peter Johnson @insertcoffee 10 years ago
parent
commit
f0e977371e
  1. 5
      app.js
  2. 65
      middleware/search.js
  3. 54
      query/search.js
  4. 75
      test/unit/query/search.js

5
app.js

@ -8,9 +8,6 @@ app.use( require('./middleware/headers') );
app.use( require('./middleware/cors') );
app.use( require('./middleware/jsonp') );
var middlewares = {};
middlewares.search = require('./middleware/search');
/** ----------------------- sanitisers ----------------------- **/
var sanitisers = {};
@ -34,7 +31,7 @@ app.get( '/', controllers.index() );
app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() );
// search API
app.get( '/search', sanitisers.search.middleware, middlewares.search.middleware, controllers.search() );
app.get( '/search', sanitisers.search.middleware, controllers.search() );
// reverse API
app.get( '/reverse', sanitisers.reverse.middleware, controllers.search(undefined, require('./query/reverse')) );

65
middleware/search.js

@ -1,65 +0,0 @@
function deg2rad(degrees) {
return Math.PI*degrees/180;
}
function rad2deg(radians) {
return 180.0*radians/Math.PI;
}
// Semi-axes of WGS-84 geoidal reference
var WGS84_a = 6378137.0; // Major semiaxis [m]
var WGS84_b = 6356752.3; // Minor semiaxis [m]
// Earth radius at a given latitude, according to the WGS-84 ellipsoid [m]
function WGS84EarthRadius(lat){
// http://en.wikipedia.org/wiki/Earth_radius
var An = WGS84_a*WGS84_a * Math.cos(lat);
var Bn = WGS84_b*WGS84_b * Math.sin(lat);
var Ad = WGS84_a * Math.cos(lat);
var Bd = WGS84_b * Math.sin(lat);
return Math.sqrt( (An*An + Bn*Bn)/(Ad*Ad + Bd*Bd) );
}
// Bounding box surrounding the point at given coordinates,
// assuming local approximation of Earth surface as a sphere
// of radius given by WGS84
function boundingBox(latitudeInDegrees, longitudeInDegrees, halfSideInKm) {
var lat = deg2rad(latitudeInDegrees);
var lon = deg2rad(longitudeInDegrees);
var halfSide = 1000*halfSideInKm;
// Radius of Earth at given latitude
var radius = WGS84EarthRadius(lat);
// Radius of the parallel at given latitude
var pradius = radius*Math.cos(lat);
var latMin = lat - halfSide/radius;
var latMax = lat + halfSide/radius;
var lonMin = lon - halfSide/pradius;
var lonMax = lon + halfSide/pradius;
return {
'bottom_left': {
'lat': rad2deg(latMin),
'lon': rad2deg(lonMin)
},
'top_right': {
'lat': rad2deg(latMax),
'lon': rad2deg(lonMax)
}
};
}
// middleware
function middleware(req, res, next){
req.clean = req.clean || {};
// ideally, bbox should be part of the req (and not to be calculated)
// TBD
req.clean.bbox = boundingBox(req.query.lat, req.query.lon, 20);
next();
}
// middleware
module.exports.middleware = middleware

54
query/search.js

@ -1,46 +1,26 @@
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":{
"query_string" : {
"query": params.input,
"fields": ['name.default'],
"default_operator": 'OR'
}
},
"filter": {
"geo_bounding_box": {
"center_point": {
"bottom_left": {
"lat": params.bbox.bottom_left.lat,
"lon": params.bbox.bottom_left.lon
},
"top_right": {
"lat": params.bbox.top_right.lat,
"lon": params.bbox.top_right.lon
}
}
}
},
"sort" : [{
"_geo_distance" : {
"center_point" : {
"lat": params.lat,
"lon": params.lon
},
"order": 'asc',
"unit": 'km'
}
}],
"size": params.size
var centroid = {
lat: params.lat,
lon: params.lon
};
// logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) );
return cmd;
var query = queries.distance( centroid, { size: params.size } );
// add search condition to distance query
query.query.filtered.query = {
query_string : {
query: params.input,
fields: ['name.default'],
default_operator: 'OR'
}
};
return query;
}
module.exports = generate;

75
test/unit/query/search.js

@ -27,41 +27,54 @@ module.exports.tests.query = function(test, common) {
},
layers: ['test']
});
var expected = {
query:{
query_string : {
query: 'test',
fields: ['name.default'],
default_operator: 'OR'
}
},
filter: {
geo_bounding_box: {
center_point: {
bottom_left: {
lat: 11.51053655297385,
lon: -103.16362455862279
},
top_right: {
lat: 47.472183447026154,
lon: -61.84881544137721
}
"query": {
"filtered": {
"query": {
"query_string": {
"query": "test",
"fields": [
"name.default"
],
"default_operator": "OR"
}
}
},
sort : [{
_geo_distance : {
center_point : {
lat: 29.49136,
lon: -82.50622
},
order: 'asc',
unit: 'km'
},
"filter": {
"bool": {
"must": [
{
"geo_distance": {
"distance": "50km",
"distance_type": "plane",
"optimize_bbox": "indexed",
"_cache": true,
"center_point": {
"lat": "29.49",
"lon": "-82.51"
}
}
}
]
}
}],
size: 10
}
}
},
"sort": [
{
"_geo_distance": {
"center_point": {
"lat": 29.49136,
"lon": -82.50622
},
"order": "asc",
"unit": "km"
}
}
],
"size": 10
};
t.deepEqual(query, expected, 'valid search query');
t.end();
});

Loading…
Cancel
Save