Browse Source

perform mget during suggest. requires suggester output to be a gid

pull/38/head
Peter Johnson 10 years ago
parent
commit
6ddbe86ac5
  1. 46
      controller/suggest.js
  2. 1
      test/ciao/suggest/success.coffee
  3. 31
      test/unit/controller/suggest.js
  4. 31
      test/unit/mock/backend.js

46
controller/suggest.js

@ -1,6 +1,9 @@
var service = { suggest: require('../service/suggest') }; var service = {
var geojsonify = require('../helper/geojsonify').suggest; suggest: require('../service/suggest'),
mget: require('../service/mget')
};
var geojsonify = require('../helper/geojsonify').search;
function setup( backend, query ){ function setup( backend, query ){
@ -16,11 +19,8 @@ function setup( backend, query ){
body: query( req.clean ) body: query( req.clean )
}; };
// query backend // responder
service.suggest( backend, cmd, function( err, docs ){ function reply( docs ){
// error handler
if( err ){ return next( err ); }
// convert docs to geojson // convert docs to geojson
var geojson = geojsonify( docs ); var geojson = geojsonify( docs );
@ -30,6 +30,38 @@ function setup( backend, query ){
// respond // respond
return res.status(200).json( geojson ); return res.status(200).json( geojson );
}
// query backend
service.suggest( backend, cmd, function( err, suggested ){
// error handler
if( err ){ return next( err ); }
// no documents suggested, return empty array to avoid ActionRequestValidationException
if( !Array.isArray( suggested ) || !suggested.length ){
return reply([]);
}
// map suggester output to mget query
var query = suggested.map( function( doc ) {
var idParts = doc.text.split(':');
return {
_index: 'pelias',
_type: idParts[0],
_id: idParts[1]
};
});
service.mget( backend, query, function( err, docs ){
// error handler
if( err ){ return next( err ); }
// reply
return reply( docs );
});
}); });
} }

1
test/ciao/suggest/success.coffee

@ -12,5 +12,6 @@ should.not.exist json.error
json.date.should.be.within now-5000, now+5000 json.date.should.be.within now-5000, now+5000
#? valid geojson #? valid geojson
console.log( json );
json.type.should.equal 'FeatureCollection' json.type.should.equal 'FeatureCollection'
json.features.should.be.instanceof Array json.features.should.be.instanceof Array

31
test/unit/controller/suggest.js

@ -16,34 +16,42 @@ module.exports.tests.interface = function(test, common) {
// functionally test controller (backend success) // functionally test controller (backend success)
module.exports.tests.functional_success = function(test, common) { module.exports.tests.functional_success = function(test, common) {
// expected geojson features for 'client/suggest/ok/1' fixture // expected geojson features for 'client/mget/ok/1' fixture
var expected = [{ var expected = [{
type: 'Feature', type: 'Feature',
geometry: { geometry: {
type: 'Point', type: 'Point',
coordinates: [ 101, -10.1 ] coordinates: [ -50.5, 100.1 ]
}, },
properties: { properties: {
id: 'mockid', name: 'test name1',
type: 'mocktype', admin0: 'country1',
value: 1 admin1: 'state1',
admin2: 'city1'
} }
}, { }, {
type: 'Feature', type: 'Feature',
geometry: { geometry: {
type: 'Point', type: 'Point',
coordinates: [ 101, -10.1 ] coordinates: [ -51.5, 100.2 ]
}, },
properties: { properties: {
id: 'mockid', name: 'test name2',
type: 'mocktype', admin0: 'country2',
value: 2 admin1: 'state2',
admin2: 'city2'
} }
}]; }];
test('functional success', function(t) { test('functional success', function(t) {
var i = 0;
var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); // the backend executes 2 commands, so we check them both
if( ++i === 1 ){
t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct suggest command');
} else {
t.deepEqual(cmd, { body: { docs: [ { _id: 'mockid', _index: 'pelias', _type: 'mocktype' }, { _id: 'mockid', _index: 'pelias', _type: 'mocktype' } ] } }, 'correct mget command');
}
}); });
var controller = setup( backend, mockQuery() ); var controller = setup( backend, mockQuery() );
var res = { var res = {
@ -52,6 +60,9 @@ module.exports.tests.functional_success = function(test, common) {
return res; return res;
}, },
json: function( json ){ json: function( json ){
console.log( 'json', json );
t.equal(typeof json, 'object', 'returns json'); t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.date, 'number', 'date set'); t.equal(typeof json.date, 'number', 'date set');
t.equal(json.type, 'FeatureCollection', 'valid geojson'); t.equal(json.type, 'FeatureCollection', 'valid geojson');

31
test/unit/mock/backend.js

@ -1,12 +1,8 @@
var mockPayload = {
id: 'mocktype/mockid',
geo: '101,-10.1'
};
var responses = {}; var responses = {};
responses['client/suggest/ok/1'] = function( cmd, cb ){ responses['client/suggest/ok/1'] = function( cmd, cb ){
return cb( undefined, suggestEnvelope([ { value: 1, payload: mockPayload }, { value: 2, payload: mockPayload } ]) ); return cb( undefined, suggestEnvelope([ { score: 1, text: 'mocktype:mockid' }, { score: 2, text: 'mocktype:mockid' } ]) );
}; };
responses['client/suggest/fail/1'] = function( cmd, cb ){ responses['client/suggest/fail/1'] = function( cmd, cb ){
return cb( 'a backend error occurred' ); return cb( 'a backend error occurred' );
@ -28,6 +24,23 @@ responses['client/search/ok/1'] = function( cmd, cb ){
} }
}])); }]));
}; };
responses['client/mget/ok/1'] = function( cmd, cb ){
return cb( undefined, mgetEnvelope([{
_source: {
value: 1,
center_point: { lat: 100.1, lon: -50.5 },
name: { default: 'test name1' },
admin0: 'country1', admin1: 'state1', admin2: 'city1'
}
}, {
_source: {
value: 2,
center_point: { lat: 100.2, lon: -51.5 },
name: { default: 'test name2' },
admin0: 'country2', admin1: 'state2', admin2: 'city2'
}
}]));
};
responses['client/search/fail/1'] = function( cmd, cb ){ responses['client/search/fail/1'] = function( cmd, cb ){
return cb( 'a backend error occurred' ); return cb( 'a backend error occurred' );
}; };
@ -43,6 +56,10 @@ function setup( key, cmdCb ){
search: function( cmd, cb ){ search: function( cmd, cb ){
if( 'function' === typeof cmdCb ){ cmdCb( cmd ); } if( 'function' === typeof cmdCb ){ cmdCb( cmd ); }
return responses[key].apply( this, arguments ); return responses[key].apply( this, arguments );
},
mget: function( cmd, cb ){
if( 'function' === typeof cmdCb ){ cmdCb( cmd ); }
return responses['client/mget/ok/1'].apply( this, arguments );
} }
} }
}; };
@ -58,4 +75,8 @@ function searchEnvelope( options ){
return { hits: { total: options.length, hits: options } }; return { hits: { total: options.length, hits: options } };
} }
function mgetEnvelope( options ){
return { docs: options };
}
module.exports = setup; module.exports = setup;
Loading…
Cancel
Save