Skip to content

Commit

Permalink
feat(multi-lang): add support to lang in ngrams_last_token_only_multi
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Oct 5, 2020
1 parent 4a776b9 commit 64b6210
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
6 changes: 5 additions & 1 deletion query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const defaults = require('./autocomplete_defaults');
const textParser = require('./text_parser_pelias');
const config = require('pelias-config').generate();
const placeTypes = require('../helper/placeTypes');
const toSingleField = require('./view/helper').toSingleField;

// additional views (these may be merged in to pelias/query at a later date)
var views = {
Expand Down Expand Up @@ -32,7 +33,7 @@ var adminFields = placeTypes.concat(['locality_a', 'region_a', 'country_a']);
// the name of the field to use.
// this functionality is not enabled unless the 'input:add_name_to_multimatch'
// variable is set to a non-empty value at query-time.
adminFields = adminFields.concat(['add_name_to_multimatch']);
adminFields = adminFields.concat(['add_name_to_multimatch', 'add_name_lang_to_multimatch']);

//------------------------------
// autocomplete query
Expand Down Expand Up @@ -185,6 +186,9 @@ function generateQuery( clean ){
// Search in the user lang
if(clean.lang && _.isString(clean.lang.iso6391)) {
vs.var('lang', clean.lang.iso6391);

const field = toSingleField(vs.var('admin:add_name_lang_to_multimatch:field').get(), clean.lang.iso6391);
vs.var('admin:add_name_lang_to_multimatch:field', field);
}

return {
Expand Down
2 changes: 2 additions & 0 deletions query/autocomplete_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ module.exports = _.merge({}, peliasQuery.defaults, {
// are unsure if the tokens represent admin or name components.
'admin:add_name_to_multimatch:field': 'name.default',
'admin:add_name_to_multimatch:boost': 1.5,
'admin:add_name_lang_to_multimatch:field': 'name.en',
'admin:add_name_lang_to_multimatch:boost': 1.5,

'popularity:field': 'popularity',
'popularity:modifier': 'log1p',
Expand Down
8 changes: 6 additions & 2 deletions query/view/helper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
function toMultiFields(baseField, suffix) {
return [baseField, toSingleField(baseField, suffix)];
}

function toSingleField(baseField, suffix) {
// baseField looks like phrase.default or name.default; suffix looks like en, fr....
const parts = baseField.split('.');
parts[parts.length - 1] = suffix;
return [baseField, parts.join('.')];
return parts.join('.');
}

module.exports = { toMultiFields };
module.exports = { toMultiFields, toSingleField };
3 changes: 2 additions & 1 deletion test/unit/fixture/autocomplete_linguistic_multiple_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ module.exports = {
'parent.locality_a.ngram^1',
'parent.region_a.ngram^1',
'parent.country_a.ngram^1',
'name.default^1.5'
'name.default^1.5',
'name.en^1.5'
],
'query': 'three',
'analyzer': 'peliasQuery',
Expand Down
3 changes: 2 additions & 1 deletion test/unit/fixture/autocomplete_linguistic_with_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = {
'parent.locality_a.ngram^1',
'parent.region_a.ngram^1',
'parent.country_a.ngram^1',
'name.default^1.5'
'name.default^1.5',
'name.en^1.5'
],
'query': 'three',
'analyzer': 'peliasAdmin',
Expand Down
3 changes: 2 additions & 1 deletion test/unit/fixture/autocomplete_single_character_street.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = {
'parent.locality_a.ngram^1',
'parent.region_a.ngram^1',
'parent.country_a.ngram^1',
'name.default^1.5'
'name.default^1.5',
'name.en^1.5'
],
'query': 'laird',
'analyzer': 'peliasAdmin',
Expand Down
30 changes: 29 additions & 1 deletion test/unit/query/autocomplete_token_matching_permutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const defaultPeliasConfig = {

// admin fields
const placeTypes = require('../../../helper/placeTypes');
var adminFields = placeTypes.concat(['locality_a', 'region_a', 'country_a', 'add_name_to_multimatch']);
var adminFields = placeTypes.concat(['locality_a', 'region_a', 'country_a', 'add_name_to_multimatch', 'add_name_lang_to_multimatch']);

var generate = proxyquire('../../../query/autocomplete', {
'pelias-config': defaultPeliasConfig
Expand Down Expand Up @@ -267,6 +267,34 @@ module.exports.tests.multiple_tokens = function(test, common) {
]
});
});

test('multiple tokens - with multi lang', function(t) {

var clean = {
text: 'test abc',
tokens: ['test', 'abc'],
tokens_complete: ['test'],
tokens_incomplete: ['abc'],
lang: { iso6391: 'fr' }
};

var vs = vars( clean );
vs.var('lang', 'fr');
vs.var('input:add_name_to_multimatch', 'enabled');
vs.var('admin:add_name_to_multimatch:field', 'name.default');
vs.var('admin:add_name_lang_to_multimatch:field', 'name.fr');

assert( t, generate( clean ), {
must: [
views.phrase_first_tokens_only( vs ),
views.ngrams_last_token_only_multi( vs )
],
should: [
peliasQuery.view.popularity( peliasQuery.view.leaf.match_all )( vs ),
peliasQuery.view.population( peliasQuery.view.leaf.match_all )( vs )
]
});
});
};


Expand Down

0 comments on commit 64b6210

Please sign in to comment.