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. 48
      controller/suggest.js
  2. 1
      test/ciao/suggest/success.coffee
  3. 31
      test/unit/controller/suggest.js
  4. 31
      test/unit/mock/backend.js

48
controller/suggest.js

@ -1,6 +1,9 @@
var service = { suggest: require('../service/suggest') };
var geojsonify = require('../helper/geojsonify').suggest;
var service = {
suggest: require('../service/suggest'),
mget: require('../service/mget')
};
var geojsonify = require('../helper/geojsonify').search;
function setup( backend, query ){
@ -16,12 +19,9 @@ function setup( backend, query ){
body: query( req.clean )
};
// query backend
service.suggest( backend, cmd, function( err, docs ){
// error handler
if( err ){ return next( err ); }
// responder
function reply( docs ){
// convert docs to geojson
var geojson = geojsonify( docs );
@ -30,6 +30,38 @@ function setup( backend, query ){
// respond
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
#? valid geojson
console.log( json );
json.type.should.equal 'FeatureCollection'
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)
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 = [{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ 101, -10.1 ]
coordinates: [ -50.5, 100.1 ]
},
properties: {
id: 'mockid',
type: 'mocktype',
value: 1
name: 'test name1',
admin0: 'country1',
admin1: 'state1',
admin2: 'city1'
}
}, {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [ 101, -10.1 ]
coordinates: [ -51.5, 100.2 ]
},
properties: {
id: 'mockid',
type: 'mocktype',
value: 2
name: 'test name2',
admin0: 'country2',
admin1: 'state2',
admin2: 'city2'
}
}];
test('functional success', function(t) {
var i = 0;
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 res = {
@ -52,6 +60,9 @@ module.exports.tests.functional_success = function(test, common) {
return res;
},
json: function( json ){
console.log( 'json', json );
t.equal(typeof json, 'object', 'returns json');
t.equal(typeof json.date, 'number', 'date set');
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 = {};
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 ){
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 ){
return cb( 'a backend error occurred' );
};
@ -43,6 +56,10 @@ function setup( key, cmdCb ){
search: function( cmd, cb ){
if( 'function' === typeof cmdCb ){ cmdCb( cmd ); }
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 } };
}
function mgetEnvelope( options ){
return { docs: options };
}
module.exports = setup;
Loading…
Cancel
Save