diff --git a/controller/search.js b/controller/search.js new file mode 100644 index 00000000..dd021120 --- /dev/null +++ b/controller/search.js @@ -0,0 +1,36 @@ + +var query = require('../query/search'), + backend = require('../src/backend'); + +function controller( req, res, next ){ + + // backend command + var cmd = { + index: 'pelias', + body: query( req.clean ) + }; + + // query backend + backend().client.search( cmd, function( err, data ){ + + var docs = []; + + // handle backend errors + if( err ){ return next( err ); } + + if( data && data.hits && data.hits.total){ + docs = data.hits.hits.map( function( hit ){ + return hit._source; + }); + } + + // respond + return res.status(200).json({ + date: new Date().getTime(), + body: docs + }); + }); + +} + +module.exports = controller; \ No newline at end of file diff --git a/docs/search/success.md b/docs/search/success.md new file mode 100644 index 00000000..31e47a39 --- /dev/null +++ b/docs/search/success.md @@ -0,0 +1,349 @@ +# valid search query + +*Generated: Wed Sep 17 2014 16:05:27 GMT-0400 (EDT)* +## Request +```javascript +{ + "protocol": "http:", + "host": "localhost", + "method": "GET", + "port": 3100, + "path": "/search?input=z&lat=0&lon=0" +} +``` + +## Response +```javascript +Status: 200 +{ + "x-powered-by": "mapzen", + "charset": "utf8", + "cache-control": "public,max-age=60", + "server": "Pelias/0.0.0", + "access-control-allow-origin": "*", + "access-control-allow-methods": "GET", + "access-control-allow-headers": "X-Requested-With,content-type", + "access-control-allow-credentials": "true", + "content-type": "application/json; charset=utf-8", + "content-length": "4459", + "etag": "W/\"PY7aB5FehG8OpkYGve7zQg==\"", + "date": "Wed, 17 Sep 2014 20:05:27 GMT", + "connection": "close" +} +``` +```javascript +{ + "date": 1410984327401, + "body": [ + { + "name": { + "default": "Lazy C-Z Reservoir" + }, + "admin0": "United States", + "admin1": "Texas", + "admin2": "Brazoria County", + "center_point": { + "lat": "29.23763", + "lon": "-95.32676" + }, + "suggest": { + "input": [ + "lazy c-z reservoir" + ], + "payload": { + "id": "geoname/4705663", + "geo": "-95.32676,29.23763" + }, + "output": "Lazy C-Z Reservoir, Brazoria County, United States" + } + }, + { + "name": { + "default": "Z Loggans Bar" + }, + "admin0": "United States", + "admin1": "Alabama", + "admin2": "Hale County", + "center_point": { + "lat": "32.89596", + "lon": "-87.80501" + }, + "suggest": { + "input": [ + "z loggans bar" + ], + "payload": { + "id": "geoname/4098848", + "geo": "-87.80501,32.89596" + }, + "output": "Z Loggans Bar, Hale County, United States" + } + }, + { + "name": { + "default": "Z Loggans Landing (historical)" + }, + "admin0": "United States", + "admin1": "Alabama", + "admin2": "Greene County", + "center_point": { + "lat": "32.89929", + "lon": "-87.80418" + }, + "suggest": { + "input": [ + "z loggans landing (historical)" + ], + "payload": { + "id": "geoname/4098849", + "geo": "-87.80418,32.89929" + }, + "output": "Z Loggans Landing (historical), Greene County, United States" + } + }, + { + "name": { + "default": "Piney Z Lake" + }, + "admin0": "United States", + "admin1": "Florida", + "admin2": "Leon County", + "center_point": { + "lat": "30.44353", + "lon": "-84.19184" + }, + "suggest": { + "input": [ + "piney z lake" + ], + "payload": { + "id": "geoname/4168700", + "geo": "-84.19184,30.44353" + }, + "output": "Piney Z Lake, Leon County, United States" + } + }, + { + "name": { + "default": "H Z Smith Lake" + }, + "admin0": "United States", + "admin1": "Georgia", + "admin2": "Bulloch County", + "center_point": { + "lat": "32.47721", + "lon": "-81.83116" + }, + "suggest": { + "input": [ + "h z smith lake" + ], + "payload": { + "id": "geoname/4198672", + "geo": "-81.83116,32.47721" + }, + "output": "H Z Smith Lake, Bulloch County, United States" + } + }, + { + "name": { + "default": "J Z Miller House" + }, + "admin0": "United States", + "admin1": "Texas", + "admin2": "Bell County", + "center_point": { + "lat": "31.06324", + "lon": "-97.45945" + }, + "suggest": { + "input": [ + "j z miller house" + ], + "payload": { + "id": "geoname/4700378", + "geo": "-97.45945,31.06324" + }, + "output": "J Z Miller House, Bell County, United States" + } + }, + { + "name": { + "default": "Z Kingsley Grant" + }, + "admin0": "United States", + "admin1": "Florida", + "admin2": "Duval County", + "center_point": { + "lat": "30.24191", + "lon": "-81.62204" + }, + "suggest": { + "input": [ + "z kingsley grant" + ], + "payload": { + "id": "geoname/4178927", + "geo": "-81.62204,30.24191" + }, + "output": "Z Kingsley Grant, Duval County, United States" + } + }, + { + "name": { + "default": "L-Z Bravo Airport" + }, + "admin0": "United States", + "admin1": "Missouri", + "admin2": "Monroe County", + "center_point": { + "lat": "39.63615", + "lon": "-92.16712" + }, + "suggest": { + "input": [ + "l-z bravo airport" + ], + "payload": { + "id": "geoname/4393970", + "geo": "-92.16712,39.63615" + }, + "output": "L-Z Bravo Airport, Monroe County, United States" + } + }, + { + "name": { + "default": "Z L Madden Elementary School" + }, + "admin0": "United States", + "admin1": "South Carolina", + "admin2": "Spartanburg County", + "center_point": { + "lat": "34.97207", + "lon": "-81.95149" + }, + "suggest": { + "input": [ + "z l madden elementary school" + ], + "payload": { + "id": "geoname/4601756", + "geo": "-81.95149,34.97207" + }, + "output": "Z L Madden Elementary School, Spartanburg County, United States" + } + }, + { + "name": { + "default": "H Z Smith Lake Dam" + }, + "admin0": "United States", + "admin1": "Georgia", + "admin2": "Bulloch County", + "center_point": { + "lat": "32.47684", + "lon": "-81.8315" + }, + "suggest": { + "input": [ + "h z smith lake dam" + ], + "payload": { + "id": "geoname/4198673", + "geo": "-81.8315,32.47684" + }, + "output": "H Z Smith Lake Dam, Bulloch County, United States" + } + }, + { + "name": { + "default": "J Z George High School" + }, + "admin0": "United States", + "admin1": "Mississippi", + "admin2": "Carroll County", + "center_point": { + "lat": "33.52095", + "lon": "-89.92064" + }, + "suggest": { + "input": [ + "j z george high school" + ], + "payload": { + "id": "geoname/4431378", + "geo": "-89.92064,33.52095" + }, + "output": "J Z George High School, Carroll County, United States" + } + }, + { + "name": { + "default": "Colonel Z Williams Landing (historical)" + }, + "admin0": "United States", + "admin1": "Alabama", + "admin2": "Russell County", + "center_point": { + "lat": "32.0646", + "lon": "-85.05604" + }, + "suggest": { + "input": [ + "colonel z williams landing (historical)" + ], + "payload": { + "id": "geoname/4056398", + "geo": "-85.05604,32.0646" + }, + "output": "Colonel Z Williams Landing (historical), Russell County, United States" + } + }, + { + "name": { + "default": "Z M Jack Stell Field Airport", + "alt0": "CRT", + "alt1": "KCRT" + }, + "admin0": "United States", + "admin1": "Arkansas", + "admin2": "Ashley County", + "center_point": { + "lat": "33.17269", + "lon": "-91.88504" + }, + "suggest": { + "input": [ + "z m jack stell field airport", + "crt", + "kcrt" + ], + "payload": { + "id": "geoname/4107205", + "geo": "-91.88504,33.17269" + }, + "output": "Z M Jack Stell Field Airport, Ashley County, United States" + } + } + ] +} +``` + +## Tests + +### ✓ 200 ok +```javascript +response.statusCode.should.equal 200 +``` + +### ✓ valid response +```javascript +now = new Date().getTime() +should.exist json +should.not.exist json.error +should.exist json.date +json.date.should.be.within now-2000, now+2000 +should.exist json.body +json.body.should.be.instanceof Array +``` + diff --git a/index.js b/index.js index e74fed10..39207611 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,10 @@ app.use( require('./middleware/jsonp') ); app.get( '/', require('./controller/index') ); // suggest API -app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest') ); +app.get( '/suggest', require('./sanitiser/sanitise'), require('./controller/suggest') ); + +// search API +app.get( '/search', require('./sanitiser/sanitise'), require('./controller/search') ); /** ----------------------- error middleware ----------------------- **/ diff --git a/query/search.js b/query/search.js new file mode 100644 index 00000000..db3fc737 --- /dev/null +++ b/query/search.js @@ -0,0 +1,22 @@ +var logger = require('../src/logger'); + +// Build pelias search query +function generate( params ){ + + var cmd = { + "query": { + "query_string": { + "query": params.input, + "fields": ['name.default'], + "default_operator": 'OR' + } + }, + "size": 30 + }; + + logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) ); + return cmd; + +} + +module.exports = generate; \ No newline at end of file diff --git a/sanitiser/suggest.js b/sanitiser/sanitise.js similarity index 100% rename from sanitiser/suggest.js rename to sanitiser/sanitise.js diff --git a/test/ciao/search/success.coffee b/test/ciao/search/success.coffee new file mode 100644 index 00000000..34249e00 --- /dev/null +++ b/test/ciao/search/success.coffee @@ -0,0 +1,14 @@ +#> valid search query +path: '/search?input=z&lat=0&lon=0' + +#? 200 ok +response.statusCode.should.equal 200 + +#? valid response +now = new Date().getTime() +should.exist json +should.not.exist json.error +should.exist json.date +json.date.should.be.within now-2000, now+2000 +should.exist json.body +json.body.should.be.instanceof Array \ No newline at end of file