Browse Source

feat(autocomplete_boost_exact_matches): improved boosting for exact matches

autocomplete_boost_exact_matches
Peter Johnson 6 years ago committed by Julian Simioni
parent
commit
20764b8f95
No known key found for this signature in database
GPG Key ID: B9EEB0C6EE0910A1
  1. 3
      query/autocomplete.js
  2. 60
      query/view/boost_exact_matches.js
  3. 10
      test/unit/fixture/autocomplete_boundary_country.js
  4. 10
      test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js
  5. 10
      test/unit/fixture/autocomplete_linguistic_final_token.js
  6. 10
      test/unit/fixture/autocomplete_linguistic_focus.js
  7. 10
      test/unit/fixture/autocomplete_linguistic_focus_null_island.js
  8. 11
      test/unit/fixture/autocomplete_linguistic_multiple_tokens.js
  9. 10
      test/unit/fixture/autocomplete_linguistic_only.js
  10. 10
      test/unit/fixture/autocomplete_with_layer_filtering.js
  11. 10
      test/unit/fixture/autocomplete_with_source_filtering.js
  12. 2
      test/unit/query/autocomplete.js

3
query/autocomplete.js

@ -40,7 +40,8 @@ query.score( peliasQuery.view.admin('locality') );
query.score( peliasQuery.view.admin('neighbourhood') ); query.score( peliasQuery.view.admin('neighbourhood') );
// scoring boost // 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.focus( views.ngrams_strict ) );
query.score( peliasQuery.view.popularity( views.pop_subquery ) ); query.score( peliasQuery.view.popularity( views.pop_subquery ) );
query.score( peliasQuery.view.population( views.pop_subquery ) ); query.score( peliasQuery.view.population( views.pop_subquery ) );

60
query/view/boost_exact_matches.js

@ -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 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 additional 'SHOULD' condition which scores exact matches slighly higher
than partial matches. 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 if( includePartialTokens ){
// passed to other views. // get a copy of *all* tokens produced from the input:name (including partial tokens)
var vsCopy = new peliasQuery.Vars( vs.export() ); var allTokens = vs.var('input:name:tokens').get();
// copy phrase:* values from search defaults // a duplicate view would be generated, fail now, don't render this view.
vsCopy.var('phrase:analyzer').set(searchDefaults['phrase:analyzer']); // see file comments for more info
vsCopy.var('phrase:field').set(searchDefaults['phrase:field']); if( allTokens.join(' ') === tokens.join(' ') ){ return null; }
// get a copy of the *complete* tokens produced from the input:name // use *all* the tokens for this view instead of only the complete tokens.
var tokens = vs.var('input:name:tokens_complete').get(); tokens = allTokens;
}
// no valid tokens to use, fail now, don't render this view. // no valid tokens to use, fail now, don't render this view.
if( !tokens || tokens.length < 1 ){ return null; } if( !tokens || tokens.length < 1 ){ return null; }
// set 'input:name' to be only the fully completed characters // set 'input:name' to be only the fully completed characters
vsCopy.var('input:name').set( tokens.join(' ') ); vsCopy.var('input:name').set( tokens.join(' ') );
return peliasQuery.view.phrase( vsCopy ); return peliasQuery.view.phrase( vsCopy );
};
}; };

10
test/unit/fixture/autocomplete_boundary_country.js

@ -25,6 +25,16 @@ module.exports = {
} }
}], }],
'should':[{ 'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': { 'function_score': {
'query': { 'query': {
'match_all': {} 'match_all': {}

10
test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js

@ -18,6 +18,16 @@ module.exports = {
} }
}], }],
'should':[{ 'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'type': 'phrase',
'boost': 1,
'slop': 3,
'query': 'test'
}
}
},{
'function_score': { 'function_score': {
'query': { 'query': {
'match_all': {} 'match_all': {}

10
test/unit/fixture/autocomplete_linguistic_final_token.js

@ -22,6 +22,16 @@ module.exports = {
'type': 'phrase' 'type': 'phrase'
} }
} }
},{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'one t',
'type': 'phrase'
}
}
},{ },{
'function_score': { 'function_score': {
'query': { 'query': {

10
test/unit/fixture/autocomplete_linguistic_focus.js

@ -18,6 +18,16 @@ module.exports = {
} }
}], }],
'should': [{ 'should': [{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': { 'function_score': {
'query': { 'query': {
'match': { 'match': {

10
test/unit/fixture/autocomplete_linguistic_focus_null_island.js

@ -18,6 +18,16 @@ module.exports = {
} }
}], }],
'should': [{ 'should': [{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': { 'function_score': {
'query': { 'query': {
'match': { 'match': {

11
test/unit/fixture/autocomplete_linguistic_multiple_tokens.js

@ -40,6 +40,17 @@ module.exports = {
} }
} }
}, },
{
'match': {
'phrase.default': {
'analyzer' : 'peliasPhrase',
'type' : 'phrase',
'boost' : 1,
'slop' : 3,
'query' : 'one two three'
}
}
},
{ {
'function_score': { 'function_score': {
'query': { 'query': {

10
test/unit/fixture/autocomplete_linguistic_only.js

@ -18,6 +18,16 @@ module.exports = {
} }
}], }],
'should':[{ 'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'type': 'phrase',
'boost': 1,
'slop': 3,
'query': 'test'
}
}
},{
'function_score': { 'function_score': {
'query': { 'query': {
'match_all': {} 'match_all': {}

10
test/unit/fixture/autocomplete_with_layer_filtering.js

@ -18,6 +18,16 @@ module.exports = {
} }
}], }],
'should':[{ 'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': { 'function_score': {
'query': { 'query': {
'match_all': {} 'match_all': {}

10
test/unit/fixture/autocomplete_with_source_filtering.js

@ -18,6 +18,16 @@ module.exports = {
} }
}], }],
'should':[{ 'should':[{
'match': {
'phrase.default': {
'analyzer': 'peliasPhrase',
'boost': 1,
'slop': 3,
'query': 'test',
'type': 'phrase'
}
}
},{
'function_score': { 'function_score': {
'query': { 'query': {
'match_all': {} 'match_all': {}

2
test/unit/query/autocomplete.js

@ -206,7 +206,7 @@ module.exports.tests.query = function(test, common) {
var expected = require('../fixture/autocomplete_linguistic_bbox_san_francisco'); var expected = require('../fixture/autocomplete_linguistic_bbox_san_francisco');
t.deepEqual(compiled.type, 'autocomplete', 'query type set'); 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(); t.end();
}); });
}; };

Loading…
Cancel
Save