Skip to content

Commit

Permalink
feat(autocomplete): focus point distance filter
Browse files Browse the repository at this point in the history
This change uses an extra filter clause that takes into account a focus
point (if used) and the input text length to filter out very far away
street and adress records, improving performance.

Replaces #1215
  • Loading branch information
orangejulius committed Jul 19, 2019
1 parent 6c5db8d commit 17e2275
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
4 changes: 3 additions & 1 deletion query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var views = {
phrase_first_tokens_only: require('./view/phrase_first_tokens_only'),
pop_subquery: require('./view/pop_subquery'),
boost_exact_matches: require('./view/boost_exact_matches'),
max_character_count_layer_filter: require('./view/max_character_count_layer_filter')
max_character_count_layer_filter: require('./view/max_character_count_layer_filter'),
focus_point_filter: require('./view/focus_point_distance_filter')
};

//------------------------------
Expand Down Expand Up @@ -57,6 +58,7 @@ query.filter( peliasQuery.view.boundary_circle );
query.filter( peliasQuery.view.boundary_country );
query.filter( peliasQuery.view.categories );
query.filter( peliasQuery.view.boundary_gid );
query.filter( views.focus_point_filter );

// --------------------------------

Expand Down
55 changes: 55 additions & 0 deletions query/view/focus_point_distance_filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const _ = require('lodash');

const config = require('pelias-config');
const type_mapping = require('../../helper/type_mapping');

module.exports = function( vs ) {

if ( !vs.isset('input:name') ||
!vs.isset('centroid:field') ||
!vs.isset('focus:point:lat') ||
!vs.isset('focus:point:lon') ) {
return null;
}

const text_length = vs.var('input:name').get().length;

if (text_length > 8) {
return null;
}

const all_layers_except_address_and_street = _.without(type_mapping.layers, 'address', 'street');

const length_to_distance_mapping = {
1: '100km',
2: '200km',
3: '400km',
4: '600km',
5: '900km',
6: '1200km',
7: '1600km',
8: '2000km'
};

const query = {
bool: {
minimum_should_match: 1,
should: [{
terms: {
layer: all_layers_except_address_and_street
}
},{
geo_distance: {
distance: length_to_distance_mapping[text_length],
distance_type: 'plane',
[vs.var('centroid:field')]: {
lat: vs.var('focus:point:lat'),
lon: vs.var('focus:point:lon')
}
}
}]
}
};

return query;
};
42 changes: 42 additions & 0 deletions test/unit/fixture/autocomplete_linguistic_focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,48 @@ module.exports = {
'weight': 3
}]
}
}],
'filter': [{
'bool': {
'minimum_should_match': 1,
'should': [
{
'terms': {
'layer': [
'venue',
'country',
'macroregion',
'region',
'county',
'localadmin',
'locality',
'borough',
'neighbourhood',
'continent',
'empire',
'dependency',
'macrocounty',
'macrohood',
'microhood',
'disputed',
'postalcode',
'ocean',
'marinearea'
]
}
},
{
'geo_distance': {
'distance': '600km',
'distance_type': 'plane',
'center_point': {
'lat': 29.49136,
'lon': -82.50622
}
}
}
]
}
}]
}
},
Expand Down
40 changes: 40 additions & 0 deletions test/unit/fixture/autocomplete_linguistic_focus_null_island.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,46 @@ module.exports = {
'weight': 3
}]
}
}],
'filter': [{
'bool': {
'minimum_should_match': 1,
'should': [{
'terms': {
'layer': [
'venue',
'country',
'macroregion',
'region',
'county',
'localadmin',
'locality',
'borough',
'neighbourhood',
'continent',
'empire',
'dependency',
'macrocounty',
'macrohood',
'microhood',
'disputed',
'postalcode',
'ocean',
'marinearea'
]
}
},
{
'geo_distance': {
'distance': '600km',
'distance_type': 'plane',
'center_point': {
'lat': 0,
'lon': 0
}
}
}]
}
}]
}
},
Expand Down

0 comments on commit 17e2275

Please sign in to comment.