Skip to content

Commit

Permalink
Use match instead of match_phrase query for autocomplete
Browse files Browse the repository at this point in the history
`match_phrase` is now a should query
  • Loading branch information
orangejulius committed May 11, 2020
1 parent c12589a commit af5c822
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 26 deletions.
4 changes: 3 additions & 1 deletion query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var views = {
admin_multi_match_first: require('./view/admin_multi_match_first'),
admin_multi_match_last: require('./view/admin_multi_match_last'),
phrase_first_tokens_only: require('./view/phrase_first_tokens_only'),
match_first_tokens_only: require('./view/match_first_tokens_only'),
boost_exact_matches: require('./view/boost_exact_matches'),
max_character_count_layer_filter: require('./view/max_character_count_layer_filter'),
focus_point_filter: require('./view/focus_point_distance_filter')
Expand All @@ -40,7 +41,7 @@ adminFields = adminFields.concat(['add_name_to_multimatch']);
var query = new peliasQuery.layout.FilteredBooleanQuery();

// mandatory matches
query.score( views.phrase_first_tokens_only, 'must' );
query.score( views.match_first_tokens_only, 'must' );
query.score( views.ngrams_last_token_only_multi( adminFields ), 'must' );

// admin components
Expand All @@ -54,6 +55,7 @@ query.score( peliasQuery.view.address('cross_street') );
query.score( peliasQuery.view.address('postcode') );

// scoring boost
query.score( views.phrase_first_tokens_only, 'should' );
query.score( peliasQuery.view.focus( views.ngrams_strict ) );
query.score( peliasQuery.view.popularity( peliasQuery.view.leaf.match_all ) );
query.score( peliasQuery.view.population( peliasQuery.view.leaf.match_all ) );
Expand Down
1 change: 1 addition & 0 deletions query/autocomplete_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = _.merge({}, peliasQuery.defaults, {
'ngram:field': 'name.default',
'ngram:boost': 100,
'ngram:cutoff_frequency': 0.01,
'ngram:minimum_should_match': '1<-1 3<-25%',

'phrase:analyzer': 'peliasQuery',
'phrase:field': 'phrase.default',
Expand Down
28 changes: 28 additions & 0 deletions query/view/match_first_tokens_only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const peliasQuery = require('pelias-query');

/**
Phrase view which trims the 'input:name' and uses ALL BUT the last token.
eg. if the input was "100 foo str", then 'input:name' would only be '100 foo'
note: it is assumed that the rest of the input is matched using another view.
**/

module.exports = function( vs ){
const view_name = 'match_first_tokens_only';

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

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

// set the 'input' variable to all but the last token
vs.var(`match:${view_name}:input`).set( tokens.join(' ') );
vs.var(`match:${view_name}:field`).set(vs.var('phrase:field').get());

vs.var(`match:${view_name}:analyzer`).set(vs.var('phrase:analyzer').get());
vs.var(`match:${view_name}:boost`).set(vs.var('phrase:boost').get());
vs.var(`match:${view_name}:minimum_should_match`).set(vs.var('ngram:minimum_should_match').get());

return peliasQuery.view.leaf.match(view_name)( vs );
};
15 changes: 12 additions & 3 deletions test/unit/fixture/autocomplete_custom_boosts.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
"bool": {
"must": [
{
"match_phrase": {
"match": {
"phrase.default": {
"analyzer": "peliasQuery",
"boost": 1,
"slop": 3,
"query": "foo"
"query": "foo",
"minimum_should_match": "1<-1 3<-25%"
}
}
}
],
"should": [
{
"match_phrase": {
"phrase.default": {
"analyzer": "peliasQuery",
"boost": 1,
"slop": 3,
"query": "foo"
}
}
}, {
"function_score": {
"query": {
"match_all": {}
Expand Down
13 changes: 11 additions & 2 deletions test/unit/fixture/autocomplete_linguistic_final_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ module.exports = {
'query': {
'bool': {
'must': [{
'match_phrase': {
'match': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'minimum_should_match': '1<-1 3<-25%',
'query': 'one'
}
}
}],
'should':[{
'match_phrase': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'query': 'one'
}
}
}, {
'function_score': {
'query': {
'match_all': {}
Expand Down
17 changes: 13 additions & 4 deletions test/unit/fixture/autocomplete_linguistic_multiple_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module.exports = {
'query': {
'bool': {
'must': [{
'match_phrase': {
'match': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'minimum_should_match': '1<-1 3<-25%',
'query': 'one two'
}
}
Expand Down Expand Up @@ -37,9 +37,18 @@ module.exports = {
}
}
}],
'should':[
'should':[{
'match_phrase': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'query': 'one two'
}
}
},
{
'function_score': {
'function_score': {
'query': {
'match_all': {}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module.exports = {
'query': {
'bool': {
'must': [{
'match_phrase': {
'match': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'minimum_should_match': '1<-1 3<-25%',
'query': '1 2'
}
}
Expand All @@ -25,7 +25,16 @@ module.exports = {
}
}
}],
'should': [
'should': [{
'match_phrase': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'query': '1 2'
}
}
},
{
'function_score': {
'query': {
Expand Down
14 changes: 12 additions & 2 deletions test/unit/fixture/autocomplete_linguistic_with_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module.exports = {
'bool': {
'must': [
{
'match_phrase': {
'match': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'minimum_should_match': '1<-1 3<-25%',
'query': 'one two'
}
}
Expand Down Expand Up @@ -36,6 +36,16 @@ module.exports = {
}
],
'should': [
{
'match_phrase': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'query': 'one two'
}
}
},
{
'function_score': {
'query': {
Expand Down
16 changes: 12 additions & 4 deletions test/unit/fixture/autocomplete_single_character_street.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module.exports = {
'query': {
'bool': {
'must': [{
'match_phrase': {
'match': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'minimum_should_match': '1<-1 3<-25%',
'query': 'k road'
}
}
Expand All @@ -32,8 +32,7 @@ module.exports = {
'type': 'cross_fields'
}
}],
'should':[
{
'should':[{
'match': {
'address_parts.street': {
'query': 'k road',
Expand All @@ -44,6 +43,15 @@ module.exports = {
}
},
{
'match_phrase': {
'phrase.default': {
'analyzer': 'peliasQuery',
'boost': 1,
'slop': 3,
'query': 'k road'
}
}
}, {
'function_score': {
'query': {
'match_all': {}
Expand Down
22 changes: 15 additions & 7 deletions test/unit/query/autocomplete_token_matching_permutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const defaults = new peliasQuery.Vars( require('../../../query/autocomplete_defa
const views = {
ngrams_last_token_only: require('../../../query/view/ngrams_last_token_only'),
ngrams_last_token_only_multi: require('../../../query/view/ngrams_last_token_only_multi')(adminFields),
match_first_tokens_only: require('../../../query/view/match_first_tokens_only'),
phrase_first_tokens_only: require('../../../query/view/phrase_first_tokens_only'),
};

Expand Down Expand Up @@ -84,8 +85,9 @@ module.exports.tests.single_token = function(test, common) {
var vs = vars( clean );

assert( t, generate( clean ), {
must: [ views.phrase_first_tokens_only( vs ) ],
must: [ views.match_first_tokens_only( vs ) ],
should: [
views.phrase_first_tokens_only( vs ),
peliasQuery.view.popularity( peliasQuery.view.leaf.match_all )( vs ),
peliasQuery.view.population( peliasQuery.view.leaf.match_all )( vs )
]
Expand Down Expand Up @@ -124,8 +126,9 @@ module.exports.tests.single_token = function(test, common) {
var vs = vars( clean );

assert( t, generate( clean ), {
must: [ views.phrase_first_tokens_only( vs ) ],
must: [ views.match_first_tokens_only( vs ) ],
should: [
views.phrase_first_tokens_only( vs ),
peliasQuery.view.popularity( peliasQuery.view.leaf.match_all )( vs ),
peliasQuery.view.population( peliasQuery.view.leaf.match_all )( vs )
]
Expand Down Expand Up @@ -164,8 +167,9 @@ module.exports.tests.single_token = function(test, common) {
var vs = vars( clean );

assert( t, generate( clean ), {
must: [ views.phrase_first_tokens_only( vs ) ],
must: [ views.match_first_tokens_only( vs ) ],
should: [
views.phrase_first_tokens_only( vs ),
peliasQuery.view.popularity( peliasQuery.view.leaf.match_all )( vs ),
peliasQuery.view.population( peliasQuery.view.leaf.match_all )( vs )
]
Expand All @@ -189,10 +193,11 @@ module.exports.tests.multiple_tokens = function(test, common) {

assert( t, generate( clean ), {
must: [
views.phrase_first_tokens_only( vs ),
views.match_first_tokens_only( vs ),
views.ngrams_last_token_only_multi( vs )
],
should: [
views.phrase_first_tokens_only( vs ),
peliasQuery.view.popularity( peliasQuery.view.leaf.match_all )( vs ),
peliasQuery.view.population( peliasQuery.view.leaf.match_all )( vs )
]
Expand All @@ -212,9 +217,10 @@ module.exports.tests.multiple_tokens = function(test, common) {

assert( t, generate( clean ), {
must: [
views.phrase_first_tokens_only( vs )
views.match_first_tokens_only( vs )
],
should: [
views.phrase_first_tokens_only( vs ),
peliasQuery.view.popularity( peliasQuery.view.leaf.match_all )( vs ),
peliasQuery.view.population( peliasQuery.view.leaf.match_all )( vs )
]
Expand All @@ -236,10 +242,11 @@ module.exports.tests.multiple_tokens = function(test, common) {

assert( t, generate( clean ), {
must: [
views.phrase_first_tokens_only( vs ),
views.match_first_tokens_only( vs ),
views.ngrams_last_token_only_multi( vs )
],
should: [
views.phrase_first_tokens_only( vs ),
peliasQuery.view.popularity( peliasQuery.view.leaf.match_all )( vs ),
peliasQuery.view.population( peliasQuery.view.leaf.match_all )( vs )
]
Expand All @@ -259,9 +266,10 @@ module.exports.tests.multiple_tokens = function(test, common) {

assert( t, generate( clean ), {
must: [
views.phrase_first_tokens_only( vs )
views.match_first_tokens_only( vs )
],
should: [
views.phrase_first_tokens_only( vs ),
peliasQuery.view.popularity( peliasQuery.view.leaf.match_all )( vs ),
peliasQuery.view.population( peliasQuery.view.leaf.match_all )( vs )
]
Expand Down

0 comments on commit af5c822

Please sign in to comment.