Browse Source

simplifying the query building

pull/59/head
Harish Krishna 10 years ago
parent
commit
4644ed1ad6
  1. 92
      query/suggest_multiple.js

92
query/suggest_multiple.js

@ -4,7 +4,15 @@ var logger = require('../src/logger');
// Build pelias suggest query // Build pelias suggest query
function generate( params, precision ){ function generate( params, precision ){
var getPrecision = function(zoom) { var CmdGenerator = function(params){
this.params = params;
this.cmd = {
'text': params.input
};
};
CmdGenerator.prototype.get_precision = function() {
var zoom = this.params.zoom;
switch (true) { switch (true) {
case (zoom > 15): case (zoom > 15):
return 5; // zoom: >= 16 return 5; // zoom: >= 16
@ -19,78 +27,34 @@ function generate( params, precision ){
} }
}; };
var cmd = { CmdGenerator.prototype.add_suggester = function(name, precision, layers, fuzzy) {
'text' : params.input, this.cmd[name] = {
'pelias_1' : {
'completion' : {
'size' : params.size,
'field' : 'suggest',
'context': {
'dataset': params.layers,
'location': {
'value': [ params.lon, params.lat ],
'precision': 5
}
}
}
},
'pelias_2' : {
'completion' : { 'completion' : {
'size' : params.size, 'size' : this.params.size,
'field' : 'suggest', 'field' : 'suggest',
'context': { 'context': {
'dataset': params.layers, 'dataset': layers || this.params.layers,
'location': { 'location': {
'value': [ params.lon, params.lat ], 'value': [ this.params.lon, this.params.lat ],
'precision': 3 'precision': precision || this.get_precision()
}
}
}
},
'pelias_3' : {
'completion' : {
'size' : params.size,
'field' : 'suggest',
'context': {
'dataset': params.layers,
'location': {
'value': [ params.lon, params.lat ],
'precision': 1
}
}
}
},
'pelias_4' : {
'completion' : {
'size' : params.size,
'field' : 'suggest',
'context': {
'dataset': ['admin0', 'admin1', 'admin2'],
'location': {
'value': [ params.lon, params.lat ],
'precision': precision || getPrecision(params.zoom)
}
}
}
},
'pelias_5' : {
'completion' : {
'size' : params.size,
'field' : 'suggest',
'context': {
'dataset': params.layers,
'location': {
'value': [ params.lon, params.lat ],
'precision': 3
} }
}, },
'fuzzy': {} 'fuzzy': {
'fuzziness': fuzzy || 0
}
} }
} };
}; };
// logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) ); var cmd = new CmdGenerator(params);
return cmd; 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');
// logger.log( 'cmd', JSON.stringify( cmd.cmd, null, 2 ) );
return cmd.cmd;
} }

Loading…
Cancel
Save