You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.2 KiB

var peliasQuery = require('pelias-query'),
defaults = require('./autocomplete_defaults'),
textParser = require('./text_parser'),
check = require('check-types');
9 years ago
var ngrams = function( vs ){
var view = peliasQuery.view.ngrams( vs );
view.match['name.default'].type = 'phrase';
view.match['name.default'].operator = 'and';
// console.log( JSON.stringify( view, null, 2 ) );
return view;
};
var phrase = function( vs ){
var view = peliasQuery.view.phrase( vs );
view.match['phrase.default'].type = 'phrase';
// console.log( JSON.stringify( view, null, 2 ) );
return view;
};
//------------------------------
// autocomplete query
//------------------------------
var query = new peliasQuery.layout.FilteredBooleanQuery();
// mandatory matches
9 years ago
query.score( ngrams, 'must' );
// address components
query.score( peliasQuery.view.address('housenumber') );
query.score( peliasQuery.view.address('street') );
query.score( peliasQuery.view.address('postcode') );
// admin components
query.score( peliasQuery.view.admin('alpha3') );
query.score( peliasQuery.view.admin('admin0') );
query.score( peliasQuery.view.admin('admin1') );
query.score( peliasQuery.view.admin('admin1_abbr') );
query.score( peliasQuery.view.admin('admin2') );
query.score( peliasQuery.view.admin('local_admin') );
query.score( peliasQuery.view.admin('locality') );
query.score( peliasQuery.view.admin('neighborhood') );
// scoring boost
9 years ago
query.score( phrase );
9 years ago
var focus = peliasQuery.view.focus( ngrams );
var localView = function( vs ){
var view = focus( vs );
9 years ago
if( view && view.hasOwnProperty('function_score') ){
view.function_score.filter = {
'or': [
{ 'type': { 'value': 'osmnode' } },
{ 'type': { 'value': 'osmway' } },
{ 'type': { 'value': 'osmaddress' } },
9 years ago
{ 'type': { 'value': 'openaddresses' } }
9 years ago
]
};
}
// console.log( JSON.stringify( view, null, 2 ) );
return view;
};
// console.log( focus );
9 years ago
query.score( localView );
var simpleNgramsView = function( vs ){
var view = ngrams( vs );
delete view.match['name.default'].type;
delete view.match['name.default'].boost;
// console.log( JSON.stringify( view, null, 2 ) );
return view;
};
query.score( peliasQuery.view.popularity( simpleNgramsView ) );
query.score( peliasQuery.view.population( simpleNgramsView ) );
// --------------------------------
/**
map request variables to query variables for all inputs
provided by this HTTP request.
**/
function generateQuery( clean ){
var vs = new peliasQuery.Vars( defaults );
9 years ago
// remove single grams at end
clean.text = clean.text.replace(/( .$)/g,'');
// input text
vs.var( 'input:name', clean.text );
// always 10 (not user definable due to caching)
vs.var( 'size', 10 );
// focus point
9 years ago
if( check.number(clean['focus.point.lat']) &&
check.number(clean['focus.point.lon']) ){
vs.set({
'focus:point:lat': clean['focus.point.lat'],
'focus:point:lon': clean['focus.point.lon']
});
}
// run the address parser
if( clean.parsed_text ){
textParser( clean.parsed_text, vs );
}
var q = query.render( vs );
console.log( JSON.stringify( q, null, 2 ) );
return q;
}
module.exports = generateQuery;