diff --git a/query/autocomplete.js b/query/autocomplete.js index 354504c7..416b255b 100644 --- a/query/autocomplete.js +++ b/query/autocomplete.js @@ -5,8 +5,10 @@ var peliasQuery = require('pelias-query'), check = require('check-types'); // additional views (these may be merged in to pelias/query at a later date) -var views = {}; -views.ngrams_strict = require('./view/ngrams_strict'); +var views = { + ngrams_strict: require('./view/ngrams_strict'), + focus_selected_layers: require('./view/focus_selected_layers') +}; var ngrams_last_only = function( vs ){ @@ -47,26 +49,6 @@ var phrase_first_only = function( vs ){ return peliasQuery.view.phrase( vs ); }; -var focus = peliasQuery.view.focus( views.ngrams_strict ); -var localView = function( vs ){ - - var view = focus( vs ); - - if( view && view.hasOwnProperty('function_score') ){ - view.function_score.filter = { - 'or': [ - { 'type': { 'value': 'osmnode' } }, - { 'type': { 'value': 'osmway' } }, - { 'type': { 'value': 'osmaddress' } }, - { 'type': { 'value': 'openaddresses' } } - ] - }; - } - - // console.log( JSON.stringify( view, null, 2 ) ); - return view; -}; - //------------------------------ // autocomplete query //------------------------------ @@ -92,8 +74,7 @@ query.score( peliasQuery.view.admin('locality') ); query.score( peliasQuery.view.admin('neighborhood') ); // scoring boost -query.score( localView ); - +query.score( views.focus_selected_layers( views.ngrams_strict ) ); query.score( peliasQuery.view.popularity( views.ngrams_strict ) ); query.score( peliasQuery.view.population( views.ngrams_strict ) ); diff --git a/query/view/focus_selected_layers.js b/query/view/focus_selected_layers.js new file mode 100644 index 00000000..038d7ffa --- /dev/null +++ b/query/view/focus_selected_layers.js @@ -0,0 +1,35 @@ + +var peliasQuery = require('pelias-query'); + +/** + This view is the same as `peliasQuery.view.focus` with one exception: + + if the view is generated successfully, we add a 'filter' clause which + restricts the targeted '_type' to be in the list specified below. + + documents which are not in the '_type' list below will simply score 0 for + this section of the query. +**/ + +module.exports = function( subview ){ + return function( vs ){ + + if( !subview ){ return null; } // subview validation failed + var macroView = peliasQuery.view.focus( subview ); + if( !macroView ){ return null; } // macroView validation failed + var view = macroView( vs ); + + if( view && view.hasOwnProperty('function_score') ){ + view.function_score.filter = { + 'or': [ + { 'type': { 'value': 'osmnode' } }, + { 'type': { 'value': 'osmway' } }, + { 'type': { 'value': 'osmaddress' } }, + { 'type': { 'value': 'openaddresses' } } + ] + }; + } + + return view; + }; +};