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.

169 lines
4.3 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;
};
9 years ago
var ngrams_last_only = function( vs ){
9 years ago
// hack to disable ngrams when query parsing enabled
if( vs.var('parsed_text').get() ){
return null;
}
9 years ago
var name = vs.var('input:name').get();
var vs2 = new peliasQuery.Vars( vs.export() );
vs2.var('input:name').set( name.substr( name.lastIndexOf(' ')+1 ) );
var view = ngrams( vs2 );
9 years ago
view.match['name.default'].analyzer = 'peliasPhrase';
9 years ago
return view;
};
9 years ago
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;
};
9 years ago
var phrase_first_only = function( vs ){
9 years ago
// hack to disable substr when query parsing enabled
if( !vs.var('parsed_text').get() ){
9 years ago
9 years ago
var name = vs.var('input:name').get();
var s = name.split(' ');
9 years ago
9 years ago
// single token only, abort
if( s.length < 2 ){
return null;
}
9 years ago
9 years ago
var vs2 = new peliasQuery.Vars( vs.export() );
vs2.var('input:name').set( name.substr(0, name.lastIndexOf(' ') ) );
return phrase( vs2 );
}
return phrase( vs );
9 years ago
};
9 years ago
var simpleNgramsView = function( vs ){
9 years ago
9 years ago
var view = ngrams( vs );
9 years ago
view.match['name.default'].analyzer = 'peliasPhrase';
delete view.match['name.default'].type;
delete view.match['name.default'].boost;
9 years ago
// console.log( JSON.stringify( view, null, 2 ) );
return view;
};
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') ){
9 years ago
view.function_score.query.match['name.default'].analyzer = 'peliasPhrase';
9 years ago
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;
};
9 years ago
//------------------------------
// autocomplete query
//------------------------------
var query = new peliasQuery.layout.FilteredBooleanQuery();
9 years ago
// mandatory matches
query.score( phrase_first_only, 'must' );
query.score( ngrams_last_only, 'must' );
9 years ago
9 years ago
// address components
query.score( peliasQuery.view.address('housenumber') );
query.score( peliasQuery.view.address('street') );
query.score( peliasQuery.view.address('postcode') );
9 years ago
9 years ago
// 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') );
9 years ago
9 years ago
// scoring boost
// query.score( phrase );
9 years ago
9 years ago
// console.log( focus );
query.score( localView );
9 years ago
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
vs.var( 'parsed_text', false );
9 years ago
// remove single grams at end
9 years ago
var text = clean.text.replace(/( .$)/g,'').trim();
if( clean.hasOwnProperty('parsed_text') ){
if( clean.parsed_text.hasOwnProperty('name') ){
vs.var( 'parsed_text', true );
text = clean.parsed_text.name;
}
}
9 years ago
// input text
9 years ago
vs.var( 'input:name', text );
// 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 );
}
return query.render( vs );
}
module.exports = generateQuery;