diff --git a/doc/bloodhound.md b/doc/bloodhound.md index 5d1b10b792..645ce188f0 100644 --- a/doc/bloodhound.md +++ b/doc/bloodhound.md @@ -169,6 +169,10 @@ options you can configure. * `queryTokenizer` – A function with the signature `(query)` that transforms a query into an array of string tokens. **Required**. +* `matchAnyQueryToken` - By default a search result must match ALL query-tokens. + Instead, this option returns results that match ANY query-tokens. Defaults to + `false`. + * `initialize` – If set to `false`, the Bloodhound instance will not be implicitly initialized by the constructor function. Defaults to `true`. diff --git a/src/bloodhound/options_parser.js b/src/bloodhound/options_parser.js index d08462b33d..70895a29f7 100644 --- a/src/bloodhound/options_parser.js +++ b/src/bloodhound/options_parser.js @@ -15,6 +15,7 @@ var oParser = (function() { identify: _.stringify, datumTokenizer: null, queryTokenizer: null, + matchAnyQueryToken: false, sufficient: 5, indexRemote: false, sorter: null, diff --git a/src/bloodhound/search_index.js b/src/bloodhound/search_index.js index 84b8c6a0c7..0577c4fbf2 100644 --- a/src/bloodhound/search_index.js +++ b/src/bloodhound/search_index.js @@ -22,6 +22,7 @@ var SearchIndex = window.SearchIndex = (function() { this.identify = o.identify || _.stringify; this.datumTokenizer = o.datumTokenizer; this.queryTokenizer = o.queryTokenizer; + this.matchAnyQueryToken = o.matchAnyQueryToken; this.reset(); } @@ -78,7 +79,7 @@ var SearchIndex = window.SearchIndex = (function() { var node, chars, ch, ids; // previous tokens didn't share any matches - if (matches && matches.length === 0) { + if (matches && matches.length === 0 && !that.matchAnyQueryToken) { return false; } @@ -96,8 +97,10 @@ var SearchIndex = window.SearchIndex = (function() { // break early if we find out there are no possible matches else { - matches = []; - return false; + if (!that.matchAnyQueryToken) { + matches = []; + return false; + } } }); diff --git a/test/bloodhound/search_index_spec.js b/test/bloodhound/search_index_spec.js index 7c577cca68..2ea3365a1f 100644 --- a/test/bloodhound/search_index_spec.js +++ b/test/bloodhound/search_index_spec.js @@ -54,11 +54,18 @@ describe('SearchIndex', function() { expect(this.index.search('wtf')).toEqual([]); }); - it('#serach should handle multi-token queries', function() { + it('#search should handle multi-token queries', function() { this.index.add({ value: 'foo bar' }); expect(this.index.search('foo b')).toEqual([{ value: 'foo bar' }]); }); + it('#search should return results that match ANY query-token when options.matchAnyQueryToken', function() { + this.index = build({matchAnyQueryToken:true}); + this.index.add({ value: 'foo bar' }); + expect(this.index.search('blah bar')).toEqual([{ value: 'foo bar' }]); + expect(this.index.search('food bark')).toEqual([]); + }); + it('#all should return all datums', function() { expect(this.index.all()).toEqual(fixtures.data.simple); });