Skip to content

Commit

Permalink
Merge pull request #2 from chip-miller/integration-0.11.2
Browse files Browse the repository at this point in the history
Option: matchAnyQueryToken (default false)
  • Loading branch information
Apfeluser committed Sep 25, 2015
2 parents 312d481 + 9722aeb commit 5e62960
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions doc/bloodhound.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
1 change: 1 addition & 0 deletions src/bloodhound/options_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var oParser = (function() {
identify: _.stringify,
datumTokenizer: null,
queryTokenizer: null,
matchAnyQueryToken: false,
sufficient: 5,
indexRemote: false,
sorter: null,
Expand Down
9 changes: 6 additions & 3 deletions src/bloodhound/search_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
}
});

Expand Down
9 changes: 8 additions & 1 deletion test/bloodhound/search_index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 5e62960

Please sign in to comment.