-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1219 from pelias/max_character_count_layer_filter
feat(autocomplete): filter out address records for character counts below threshold
- Loading branch information
Showing
11 changed files
with
505 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const _ = require('lodash'); | ||
const peliasQuery = require('pelias-query'); | ||
const allLayers = require('../../helper/type_mapping').layers; | ||
|
||
/** | ||
Layer terms filter view which counts the length of 'input:name' and only | ||
applies the filter condition if the text is shorter than or equal to $maxCharCount. | ||
You must provide a list of $excludedLayers, all layers listed in the type mapping | ||
will be targeted, minus any listed in $excludedLayers. | ||
eg. to filter by 'layer=address' for all one & two digit inputs: | ||
view = filter(['address'],2) | ||
**/ | ||
|
||
// lowest and highest valid character count (enforced) | ||
const MIN_CHAR_COUNT = 1; | ||
const MAX_CHAR_COUNT = 99; | ||
|
||
module.exports = function( excludedLayers, maxCharCount ) { | ||
|
||
// validate args, return no-op view if invalid | ||
if( !_.isArray(excludedLayers) || _.isEmpty(excludedLayers) || | ||
!_.isNumber(maxCharCount) ){ | ||
return () => null; | ||
} | ||
|
||
// create an array containing all layers minus excluded layers | ||
let includedLayers = _.difference(allLayers, excludedLayers); | ||
|
||
// included layers is equal to all layers, return no-op view | ||
if( includedLayers.length === allLayers.length ){ | ||
return () => null; | ||
} | ||
|
||
// create a new VariableStore with only the layers property | ||
var vsWithOnlyIncludedLayers = new peliasQuery.Vars({ 'layers': includedLayers }); | ||
|
||
// ensure char count is within a reasonable range | ||
maxCharCount = _.clamp(maxCharCount, MIN_CHAR_COUNT, MAX_CHAR_COUNT); | ||
|
||
return function( vs ){ | ||
|
||
// validate required params | ||
if( !vs.isset('input:name') ){ | ||
return null; | ||
} | ||
|
||
// enforce maximum character length | ||
let charCount = vs.var('input:name').toString().length; | ||
if( !_.inRange(charCount, 1, maxCharCount+1) ){ | ||
return null; | ||
} | ||
|
||
// use existing 'layers' query | ||
return peliasQuery.view.layers(vsWithOnlyIncludedLayers); | ||
}; | ||
}; |
86 changes: 86 additions & 0 deletions
86
test/unit/fixture/autocomplete_linguistic_one_char_token.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module.exports = { | ||
'query': { | ||
'bool': { | ||
'must': [{ | ||
'constant_score': { | ||
'query': { | ||
'match': { | ||
'name.default': { | ||
'analyzer': 'peliasQueryPartialToken', | ||
'boost': 100, | ||
'query': 't', | ||
'type': 'phrase', | ||
'operator': 'and', | ||
'slop': 3 | ||
} | ||
} | ||
} | ||
} | ||
}], | ||
'should':[{ | ||
'function_score': { | ||
'query': { | ||
'match_all': {} | ||
}, | ||
'max_boost': 20, | ||
'score_mode': 'first', | ||
'boost_mode': 'replace', | ||
'functions': [{ | ||
'field_value_factor': { | ||
'modifier': 'log1p', | ||
'field': 'popularity', | ||
'missing': 1 | ||
}, | ||
'weight': 1 | ||
}] | ||
} | ||
},{ | ||
'function_score': { | ||
'query': { | ||
'match_all': {} | ||
}, | ||
'max_boost': 20, | ||
'score_mode': 'first', | ||
'boost_mode': 'replace', | ||
'functions': [{ | ||
'field_value_factor': { | ||
'modifier': 'log1p', | ||
'field': 'population', | ||
'missing': 1 | ||
}, | ||
'weight': 3 | ||
}] | ||
} | ||
}], | ||
'filter': [{ | ||
'terms': { | ||
'layer': [ | ||
'venue', | ||
'street', | ||
'country', | ||
'macroregion', | ||
'region', | ||
'county', | ||
'localadmin', | ||
'locality', | ||
'borough', | ||
'neighbourhood', | ||
'continent', | ||
'empire', | ||
'dependency', | ||
'macrocounty', | ||
'macrohood', | ||
'microhood', | ||
'disputed', | ||
'postalcode', | ||
'ocean', | ||
'marinearea' | ||
] | ||
} | ||
}] | ||
} | ||
}, | ||
'sort': [ '_score' ], | ||
'size': 20, | ||
'track_scores': true | ||
}; |
60 changes: 60 additions & 0 deletions
60
test/unit/fixture/autocomplete_linguistic_three_char_token.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module.exports = { | ||
'query': { | ||
'bool': { | ||
'must': [{ | ||
'constant_score': { | ||
'query': { | ||
'match': { | ||
'name.default': { | ||
'analyzer': 'peliasQueryPartialToken', | ||
'boost': 100, | ||
'query': 'tes', | ||
'type': 'phrase', | ||
'operator': 'and', | ||
'slop': 3 | ||
} | ||
} | ||
} | ||
} | ||
}], | ||
'should':[{ | ||
'function_score': { | ||
'query': { | ||
'match_all': {} | ||
}, | ||
'max_boost': 20, | ||
'score_mode': 'first', | ||
'boost_mode': 'replace', | ||
'functions': [{ | ||
'field_value_factor': { | ||
'modifier': 'log1p', | ||
'field': 'popularity', | ||
'missing': 1 | ||
}, | ||
'weight': 1 | ||
}] | ||
} | ||
},{ | ||
'function_score': { | ||
'query': { | ||
'match_all': {} | ||
}, | ||
'max_boost': 20, | ||
'score_mode': 'first', | ||
'boost_mode': 'replace', | ||
'functions': [{ | ||
'field_value_factor': { | ||
'modifier': 'log1p', | ||
'field': 'population', | ||
'missing': 1 | ||
}, | ||
'weight': 3 | ||
}] | ||
} | ||
}] | ||
} | ||
}, | ||
'sort': [ '_score' ], | ||
'size': 20, | ||
'track_scores': true | ||
}; |
86 changes: 86 additions & 0 deletions
86
test/unit/fixture/autocomplete_linguistic_two_char_token.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module.exports = { | ||
'query': { | ||
'bool': { | ||
'must': [{ | ||
'constant_score': { | ||
'query': { | ||
'match': { | ||
'name.default': { | ||
'analyzer': 'peliasQueryPartialToken', | ||
'boost': 100, | ||
'query': 'te', | ||
'type': 'phrase', | ||
'operator': 'and', | ||
'slop': 3 | ||
} | ||
} | ||
} | ||
} | ||
}], | ||
'should':[{ | ||
'function_score': { | ||
'query': { | ||
'match_all': {} | ||
}, | ||
'max_boost': 20, | ||
'score_mode': 'first', | ||
'boost_mode': 'replace', | ||
'functions': [{ | ||
'field_value_factor': { | ||
'modifier': 'log1p', | ||
'field': 'popularity', | ||
'missing': 1 | ||
}, | ||
'weight': 1 | ||
}] | ||
} | ||
},{ | ||
'function_score': { | ||
'query': { | ||
'match_all': {} | ||
}, | ||
'max_boost': 20, | ||
'score_mode': 'first', | ||
'boost_mode': 'replace', | ||
'functions': [{ | ||
'field_value_factor': { | ||
'modifier': 'log1p', | ||
'field': 'population', | ||
'missing': 1 | ||
}, | ||
'weight': 3 | ||
}] | ||
} | ||
}], | ||
'filter': [{ | ||
'terms': { | ||
'layer': [ | ||
'venue', | ||
'street', | ||
'country', | ||
'macroregion', | ||
'region', | ||
'county', | ||
'localadmin', | ||
'locality', | ||
'borough', | ||
'neighbourhood', | ||
'continent', | ||
'empire', | ||
'dependency', | ||
'macrocounty', | ||
'macrohood', | ||
'microhood', | ||
'disputed', | ||
'postalcode', | ||
'ocean', | ||
'marinearea' | ||
] | ||
} | ||
}] | ||
} | ||
}, | ||
'sort': [ '_score' ], | ||
'size': 20, | ||
'track_scores': true | ||
}; |
Oops, something went wrong.