Browse Source

adding query mixer for suggest and suggest/nearby

pull/59/head
Harish Krishna 10 years ago
parent
commit
9afed3caef
  1. 4
      app.js
  2. 5
      controller/suggest_nearby.js
  3. 28
      helper/queryMixer.json
  4. 16
      query/suggest_multiple.js
  5. 2
      service/suggest_multiple.js
  6. 80
      test/unit/helper/queryMixer.js
  7. 3
      test/unit/run.js

4
app.js

@ -35,7 +35,9 @@ app.get( '/doc', sanitisers.doc.middleware, controllers.doc() );
// suggest API
app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() );
app.get( '/suggest/nearby', sanitisers.suggest.middleware, controllers.suggest_nearby() );
app.get( '/suggest/nearby',
sanitisers.suggest.middleware,
controllers.suggest_nearby(undefined, undefined, require('./helper/queryMixer').suggest_nearby) );
// search API
app.get( '/search', sanitisers.search.middleware, controllers.search() );

5
controller/suggest_nearby.js

@ -6,18 +6,19 @@ var service = {
var geojsonify = require('../helper/geojsonify').search;
var resultsHelper = require('../helper/results');
function setup( backend, query ){
function setup( backend, query, query_mixer ){
// allow overriding of dependencies
backend = backend || require('../src/backend');
query = query || require('../query/suggest_multiple');
query_mixer = query_mixer || require('../helper/queryMixer').suggest;
function controller( req, res, next ){
// backend command
var cmd = {
index: 'pelias',
body: query( req.clean )
body: query( req.clean, query_mixer )
};
// responder

28
helper/queryMixer.json

@ -0,0 +1,28 @@
{
"suggest": [
{
"layers": ["geoname","osmnode","osmway","admin0","admin1","admin2","neighborhood"],
"precision": [5, 3, 1]
},
{
"layers": ["admin0","admin1","admin2","neighborhood"],
"precision": []
},
{
"layers": ["geoname","osmnode","osmway","admin0","admin1","admin2","neighborhood"],
"precision": [3],
"fuzzy": "AUTO"
}
],
"suggest_nearby": [
{
"layers": ["geoname","osmnode","osmway","admin0","admin1","admin2","neighborhood"],
"precision": []
},
{
"layers": ["geoname","osmnode","osmway","admin0","admin1","admin2","neighborhood"],
"precision": [],
"fuzzy": "AUTO"
}
]
}

16
query/suggest_multiple.js

@ -2,7 +2,7 @@
var logger = require('../src/logger');
// Build pelias suggest query
function generate( params, precision ){
function generate( params, query_mixer ){
var CmdGenerator = function(params){
this.params = params;
@ -47,11 +47,15 @@ function generate( params, precision ){
};
var cmd = new CmdGenerator(params);
cmd.add_suggester('pelias_1', 5);
cmd.add_suggester('pelias_2', 3);
cmd.add_suggester('pelias_3', 1);
cmd.add_suggester('pelias_4', undefined, ['admin0', 'admin1', 'admin2']);
cmd.add_suggester('pelias_5', 3, undefined, 'AUTO');
query_mixer.forEach(function(item, index){
if (item.precision && Array.isArray( item.precision ) && item.precision.length ) {
item.precision.forEach(function(precision) {
cmd.add_suggester('pelias_'+index, precision, item.layers, item.fuzzy);
});
} else {
cmd.add_suggester('pelias_'+index, undefined, item.layers, item.fuzzy);
}
});
// logger.log( 'cmd', JSON.stringify( cmd.cmd, null, 2 ) );
return cmd.cmd;

2
service/suggest_multiple.js

@ -19,7 +19,7 @@ function service( backend, cmd, cb ){
var has_docs = function(obj) {
return Array.isArray( obj ) && obj.length && obj[0].options && obj[0].options.length;
};
for (var i=1, j=0; i<num_keys && j<num_keys; i++) {
for (var i=0, j=0; i<num_keys && j<num_keys; i++) {
var keys = 'pelias_'+i;
if ( has_docs(data[keys]) ){
docs[i] = docs[i] || [];

80
test/unit/helper/queryMixer.js

@ -0,0 +1,80 @@
var query_mixer = require('../../../helper/queryMixer.json');
var indeces = require('../../../query/indeces');
module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('interface', function(t) {
t.equal(typeof query_mixer, 'object', 'valid object');
t.equal(query_mixer.hasOwnProperty('suggest'), true, 'has suggest defined');
t.equal(query_mixer.hasOwnProperty('suggest_nearby'), true, 'has suggest_nearby defined');
t.end();
});
};
module.exports.tests.valid = function(test, common) {
var valid_keys = ['layers', 'precision', 'fuzzy'];
var valid_fuzzy_vals = ['AUTO', 0, 1, 2];
var valid_layer_vals = indeces;
var isValidPrecision = function(t, precisionArr) {
precisionArr.forEach(function(precision) {
t.notEqual(isNaN(precision), true, precision + ' is a valid precision value');
});
};
var isValidLayer = function(t, layerArr) {
layerArr.forEach(function(this_layer) {
t.notEqual(valid_layer_vals.indexOf(this_layer), -1, 'layer value ' + this_layer + ' is valid');
});
};
var isValid = function(key, mix) {
test('valid mix (' + key + ')' , function(t) {
t.equal(keys.length > 0, true, 'valid key');
t.equal(Array.isArray( mix ), true, 'is an array');
t.equal(mix.length > 0, true, 'is not an empty array');
mix.forEach( function(this_mix) {
t.notEqual(Object.getOwnPropertyNames(this_mix).length, 0, 'object not empty');
for (var keys in this_mix) {
t.notEqual(valid_keys.indexOf(keys), -1, keys + ' is valid');
switch(keys) {
case 'fuzzy':
t.notEqual(valid_fuzzy_vals.indexOf(this_mix[keys]), -1, 'fuzzy value ' + this_mix[keys] + ' is valid');
break;
case 'layers':
t.equal(Array.isArray(this_mix[keys]), true, 'layers is an array');
t.equal(this_mix[keys].length > 0, true, 'layers is not an empty array');
isValidLayer(t, this_mix[keys]);
break;
case 'precision':
t.equal(Array.isArray( this_mix[keys] ), true, keys + ' is an array');
if (this_mix[keys].length > 0) {
isValidPrecision(t, this_mix[keys]);
}
break;
default:
break;
}
}
});
t.end();
});
};
for (var keys in query_mixer) {
isValid(keys, query_mixer[keys]);
}
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('query_mixer: ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

3
test/unit/run.js

@ -18,7 +18,8 @@ var tests = [
require('./query/search'),
require('./query/reverse'),
require('./helper/geojsonify'),
require('./helper/outputSchema')
require('./helper/outputSchema'),
require('./helper/queryMixer')
];
tests.map(function(t) {

Loading…
Cancel
Save