Skip to content

Commit

Permalink
feat(autocomplete_boost_exact_matches): improved boosting for exact m…
Browse files Browse the repository at this point in the history
…atches
  • Loading branch information
missinglink authored and orangejulius committed Oct 15, 2018
1 parent a6fc61b commit 20764b8
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 16 deletions.
3 changes: 2 additions & 1 deletion query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ query.score( peliasQuery.view.admin('locality') );
query.score( peliasQuery.view.admin('neighbourhood') );

// scoring boost
query.score( views.boost_exact_matches );
query.score( views.boost_exact_matches(false) );
query.score( views.boost_exact_matches(true) );
query.score( peliasQuery.view.focus( views.ngrams_strict ) );
query.score( peliasQuery.view.popularity( views.pop_subquery ) );
query.score( peliasQuery.view.population( views.pop_subquery ) );
Expand Down
60 changes: 46 additions & 14 deletions query/view/boost_exact_matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,58 @@ var peliasQuery = require('pelias-query'),
the view uses some of the values from the 'search_defaults.js' file to add an
additional 'SHOULD' condition which scores exact matches slighly higher
than partial matches.
the 'includePartialTokens' variable was introduced in order to allow the view
to be reused as an additional boost for tokens which are in fact complete,
despite us not knowing for sure whether they are complete or not.
an example is 'Stop 2', without partial tokens the boost will only apply to
documents matching 'stop', with an additional view we can further boost
documents matching 'stop 2'.
note: it is most likely insufficent to include a version of this view in your
query which has includePartialTokens=true without also having a copy with
includePartialTokens=false. One view will boost the tokens that are known to
be complete and the other will additionally boost tokens which may or may not be
complete, as per the example above.
note: a clause has been included in the code which disables the view for
includePartialTokens=true if it would generate the exact same view as for
includePartialTokens=false.
**/

module.exports = function( vs ){
module.exports = function( includePartialTokens ){
return function( vs ){

// make a copy of the variables so we don't interfere with the values
// passed to other views.
var vsCopy = new peliasQuery.Vars( vs.export() );

// copy phrase:* values from search defaults
vsCopy.var('phrase:analyzer').set(searchDefaults['phrase:analyzer']);
vsCopy.var('phrase:field').set(searchDefaults['phrase:field']);

// get a copy of only the *complete* tokens produced from the input:name
var tokens = vs.var('input:name:tokens_complete').get();

// make a copy of the variables so we don't interfere with the values
// passed to other views.
var vsCopy = new peliasQuery.Vars( vs.export() );
if( includePartialTokens ){
// get a copy of *all* tokens produced from the input:name (including partial tokens)
var allTokens = vs.var('input:name:tokens').get();

// copy phrase:* values from search defaults
vsCopy.var('phrase:analyzer').set(searchDefaults['phrase:analyzer']);
vsCopy.var('phrase:field').set(searchDefaults['phrase:field']);
// a duplicate view would be generated, fail now, don't render this view.
// see file comments for more info
if( allTokens.join(' ') === tokens.join(' ') ){ return null; }

// get a copy of the *complete* tokens produced from the input:name
var tokens = vs.var('input:name:tokens_complete').get();
// use *all* the tokens for this view instead of only the complete tokens.
tokens = allTokens;
}

// no valid tokens to use, fail now, don't render this view.
if( !tokens || tokens.length < 1 ){ return null; }
// no valid tokens to use, fail now, don't render this view.
if( !tokens || tokens.length < 1 ){ return null; }

// set 'input:name' to be only the fully completed characters
vsCopy.var('input:name').set( tokens.join(' ') );
// set 'input:name' to be only the fully completed characters
vsCopy.var('input:name').set( tokens.join(' ') );

return peliasQuery.view.phrase( vsCopy );
return peliasQuery.view.phrase( vsCopy );
};
};
10 changes: 10 additions & 0 deletions test/unit/fixture/autocomplete_boundary_country.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ module.exports = {
}
}],
'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': {
'query': {
'match_all': {}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ module.exports = {
}
}],
'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'type': 'phrase',
'boost': 1,
'slop': 3,
'query': 'test'
}
}
},{
'function_score': {
'query': {
'match_all': {}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/fixture/autocomplete_linguistic_final_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ module.exports = {
'type': 'phrase'
}
}
},{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'one t',
'type': 'phrase'
}
}
},{
'function_score': {
'query': {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/fixture/autocomplete_linguistic_focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ module.exports = {
}
}],
'should': [{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': {
'query': {
'match': {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/fixture/autocomplete_linguistic_focus_null_island.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ module.exports = {
}
}],
'should': [{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': {
'query': {
'match': {
Expand Down
11 changes: 11 additions & 0 deletions test/unit/fixture/autocomplete_linguistic_multiple_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ module.exports = {
}
}
},
{
'match': {
'phrase.default': {
'analyzer' : 'peliasPhrase',
'type' : 'phrase',
'boost' : 1,
'slop' : 3,
'query' : 'one two three'
}
}
},
{
'function_score': {
'query': {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/fixture/autocomplete_linguistic_only.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ module.exports = {
}
}],
'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'type': 'phrase',
'boost': 1,
'slop': 3,
'query': 'test'
}
}
},{
'function_score': {
'query': {
'match_all': {}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/fixture/autocomplete_with_layer_filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ module.exports = {
}
}],
'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': {
'query': {
'match_all': {}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/fixture/autocomplete_with_source_filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ module.exports = {
}
}],
'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': {
'query': {
'match_all': {}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ module.exports.tests.query = function(test, common) {
var expected = require('../fixture/autocomplete_linguistic_bbox_san_francisco');

t.deepEqual(compiled.type, 'autocomplete', 'query type set');
t.deepEqual(compiled.body, expected, 'autocomplete_linguistic_focus_null_island');
t.deepEqual(compiled.body, expected, 'autocomplete_linguistic_bbox_san_francisco');
t.end();
});
};
Expand Down

0 comments on commit 20764b8

Please sign in to comment.