| 14217 |
anikendra |
1 |
Bloodhound
|
|
|
2 |
==========
|
|
|
3 |
|
|
|
4 |
Bloodhound is the typeahead.js suggestion engine. Bloodhound is robust,
|
|
|
5 |
flexible, and offers advanced functionalities such as prefetching, intelligent
|
|
|
6 |
caching, fast lookups, and backfilling with remote data.
|
|
|
7 |
|
|
|
8 |
Table of Contents
|
|
|
9 |
-----------------
|
|
|
10 |
|
|
|
11 |
* [Features](#features)
|
|
|
12 |
* [Usage](#usage)
|
|
|
13 |
* [API](#api)
|
|
|
14 |
* [Options](#options)
|
|
|
15 |
* [Prefetch](#prefetch)
|
|
|
16 |
* [Remote](#remote)
|
|
|
17 |
* [Datums](#datums)
|
|
|
18 |
* [Tokens](#tokens)
|
|
|
19 |
|
|
|
20 |
Features
|
|
|
21 |
--------
|
|
|
22 |
|
|
|
23 |
* Works with hardcoded data
|
|
|
24 |
* Prefetches data on initialization to reduce suggestion latency
|
|
|
25 |
* Uses local storage intelligently to cut down on network requests
|
|
|
26 |
* Backfills suggestions from a remote source
|
|
|
27 |
* Rate-limits and caches network requests to remote sources to lighten the load
|
|
|
28 |
|
|
|
29 |
Usage
|
|
|
30 |
-----
|
|
|
31 |
|
|
|
32 |
### API
|
|
|
33 |
|
|
|
34 |
#### new Bloodhound(options)
|
|
|
35 |
|
|
|
36 |
The constructor function. It takes an [options hash](#options) as its only
|
|
|
37 |
argument.
|
|
|
38 |
|
|
|
39 |
```javascript
|
|
|
40 |
var engine = new Bloodhound({
|
|
|
41 |
name: 'animals',
|
|
|
42 |
local: [{ val: 'dog' }, { val: 'pig' }, { val: 'moose' }],
|
|
|
43 |
remote: 'http://example.com/animals?q=%QUERY',
|
|
|
44 |
datumTokenizer: function(d) {
|
|
|
45 |
return Bloodhound.tokenizers.whitespace(d.val);
|
|
|
46 |
},
|
|
|
47 |
queryTokenizer: Bloodhound.tokenizers.whitespace
|
|
|
48 |
});
|
|
|
49 |
```
|
|
|
50 |
|
|
|
51 |
#### Bloodhound#initialize(reinitialize)
|
|
|
52 |
|
|
|
53 |
Kicks off the initialization of the suggestion engine. This includes processing
|
|
|
54 |
the data provided through `local` and fetching/processing the data provided
|
|
|
55 |
through `prefetch`. Until initialized, all other methods will behave as no-ops.
|
|
|
56 |
Returns a [jQuery promise] which is resolved when engine has been initialized.
|
|
|
57 |
|
|
|
58 |
```javascript
|
|
|
59 |
var promise = engine.initialize();
|
|
|
60 |
|
|
|
61 |
promise
|
|
|
62 |
.done(function() { console.log('success!'); })
|
|
|
63 |
.fail(function() { console.log('err!'); });
|
|
|
64 |
```
|
|
|
65 |
|
|
|
66 |
After the initial call of `initialize`, how subsequent invocations of the method
|
|
|
67 |
behave depends on the `reinitialize` argument. If `reinitialize` is falsy, the
|
|
|
68 |
method will not execute the initialization logic and will just return the same
|
|
|
69 |
jQuery promise returned by the initial invocation. If `reinitialize` is truthy,
|
|
|
70 |
the method will behave as if it were being called for the first time.
|
|
|
71 |
|
|
|
72 |
```javascript
|
|
|
73 |
var promise1 = engine.initialize();
|
|
|
74 |
var promise2 = engine.initialize();
|
|
|
75 |
var promise3 = engine.initialize(true);
|
|
|
76 |
|
|
|
77 |
promise1 === promise2;
|
|
|
78 |
promise3 !== promise1 && promise3 !== promise2;
|
|
|
79 |
```
|
|
|
80 |
|
|
|
81 |
#### Bloodhound#add(datums)
|
|
|
82 |
|
|
|
83 |
Takes one argument, `datums`, which is expected to be an array of
|
|
|
84 |
[datums](#datums). The passed in datums will get added to the search index that
|
|
|
85 |
powers the suggestion engine.
|
|
|
86 |
|
|
|
87 |
```javascript
|
|
|
88 |
engine.add([{ val: 'one' }, { val: 'two' }]);
|
|
|
89 |
```
|
|
|
90 |
|
|
|
91 |
#### Bloodhound#clear()
|
|
|
92 |
|
|
|
93 |
Removes all suggestions from the search index.
|
|
|
94 |
|
|
|
95 |
```javascript
|
|
|
96 |
engine.clear();
|
|
|
97 |
```
|
|
|
98 |
|
|
|
99 |
#### Bloodhound#clearPrefetchCache()
|
|
|
100 |
|
|
|
101 |
If you're using `prefetch`, data gets cached in local storage in an effort to
|
|
|
102 |
cut down on unnecessary network requests. `clearPrefetchCache` offers a way to
|
|
|
103 |
programmatically clear said cache.
|
|
|
104 |
|
|
|
105 |
```javascript
|
|
|
106 |
engine.clearPrefetchCache();
|
|
|
107 |
```
|
|
|
108 |
|
|
|
109 |
#### Bloodhound#clearRemoteCache()
|
|
|
110 |
|
|
|
111 |
If you're using `remote`, Bloodhound will cache the 10 most recent responses
|
|
|
112 |
in an effort to provide a better user experience. `clearRemoteCache` offers a
|
|
|
113 |
way to programmatically clear said cache.
|
|
|
114 |
|
|
|
115 |
```javascript
|
|
|
116 |
engine.clearRemoteCache();
|
|
|
117 |
```
|
|
|
118 |
|
|
|
119 |
#### Bloodhound.noConflict()
|
|
|
120 |
|
|
|
121 |
Returns a reference to the Bloodhound constructor and reverts
|
|
|
122 |
`window.Bloodhound` to its previous value. Can be used to avoid naming
|
|
|
123 |
collisions.
|
|
|
124 |
|
|
|
125 |
```javascript
|
|
|
126 |
var Dachshund = Bloodhound.noConflict();
|
|
|
127 |
```
|
|
|
128 |
|
|
|
129 |
|
|
|
130 |
<!-- section links -->
|
|
|
131 |
|
|
|
132 |
[jQuery promise]: http://api.jquery.com/Types/#Promise
|
|
|
133 |
|
|
|
134 |
#### Bloodhound#get(query, cb)
|
|
|
135 |
|
|
|
136 |
Computes a set of suggestions for `query`. `cb` will be invoked with an array
|
|
|
137 |
of datums that represent said set. `cb` will always be invoked once
|
|
|
138 |
synchronously with suggestions that were available on the client. If those
|
|
|
139 |
suggestions are insufficient (# of suggestions is less than `limit`) and `remote` was configured, `cb` may also be
|
|
|
140 |
invoked asynchronously with the suggestions available on the client mixed with
|
|
|
141 |
suggestions from the `remote` source.
|
|
|
142 |
|
|
|
143 |
```javascript
|
|
|
144 |
bloodhound.get(myQuery, function(suggestions) {
|
|
|
145 |
suggestions.each(function(suggestion) { console.log(suggestion); });
|
|
|
146 |
});
|
|
|
147 |
```
|
|
|
148 |
|
|
|
149 |
### Options
|
|
|
150 |
|
|
|
151 |
When instantiating a Bloodhound suggestion engine, there are a number of
|
|
|
152 |
options you can configure.
|
|
|
153 |
|
|
|
154 |
* `datumTokenizer` – A function with the signature `(datum)` that transforms a
|
|
|
155 |
datum into an array of string tokens. **Required**.
|
|
|
156 |
|
|
|
157 |
* `queryTokenizer` – A function with the signature `(query)` that transforms a
|
|
|
158 |
query into an array of string tokens. **Required**.
|
|
|
159 |
|
|
|
160 |
* `limit` – The max number of suggestions to return from `Bloodhound#get`. If
|
|
|
161 |
not reached, the data source will attempt to backfill the suggestions from
|
|
|
162 |
`remote`. Defaults to `5`.
|
|
|
163 |
|
|
|
164 |
* `dupDetector` – If set, this is expected to be a function with the signature
|
|
|
165 |
`(remoteMatch, localMatch)` that returns `true` if the datums are duplicates or
|
|
|
166 |
`false` otherwise. If not set, duplicate detection will not be performed.
|
|
|
167 |
|
|
|
168 |
* `sorter` – A [compare function] used to sort matched datums for a given query.
|
|
|
169 |
|
|
|
170 |
* `local` – An array of [datums](#datums) or a function that returns an array of
|
|
|
171 |
datums.
|
|
|
172 |
|
|
|
173 |
* `prefetch` – Can be a URL to a JSON file containing an array of datums or, if
|
|
|
174 |
more configurability is needed, a [prefetch options hash](#prefetch).
|
|
|
175 |
|
|
|
176 |
* `remote` – Can be a URL to fetch suggestions from when the data provided by
|
|
|
177 |
`local` and `prefetch` is insufficient or, if more configurability is needed,
|
|
|
178 |
a [remote options hash](#remote).
|
|
|
179 |
|
|
|
180 |
<!-- section links -->
|
|
|
181 |
|
|
|
182 |
[compare function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
|
|
183 |
|
|
|
184 |
### Prefetch
|
|
|
185 |
|
|
|
186 |
Prefetched data is fetched and processed on initialization. If the browser
|
|
|
187 |
supports local storage, the processed data will be cached there to
|
|
|
188 |
prevent additional network requests on subsequent page loads.
|
|
|
189 |
|
|
|
190 |
**WARNING:** While it's possible to get away with it for smaller data sets,
|
|
|
191 |
prefetched data isn't meant to contain entire data sets. Rather, it should act
|
|
|
192 |
as a first-level cache for suggestions. If don't keep this warning in mind,
|
|
|
193 |
you run the risk of hitting [local storage limits].
|
|
|
194 |
|
|
|
195 |
When configuring `prefetch`, the following options are available.
|
|
|
196 |
|
|
|
197 |
* `url` – A URL to a JSON file containing an array of datums. **Required.**
|
|
|
198 |
|
|
|
199 |
* `cacheKey` – The key that data will be stored in local storage under.
|
|
|
200 |
Defaults to value of `url`.
|
|
|
201 |
|
|
|
202 |
* `ttl` – The time (in milliseconds) the prefetched data should be cached in
|
|
|
203 |
local storage. Defaults to `86400000` (1 day).
|
|
|
204 |
|
|
|
205 |
* `thumbprint` – A string used for thumbprinting prefetched data. If this
|
|
|
206 |
doesn't match what's stored in local storage, the data will be refetched.
|
|
|
207 |
|
|
|
208 |
* `filter` – A function with the signature `filter(parsedResponse)` that
|
|
|
209 |
transforms the response body into an array of datums. Expected to return an
|
|
|
210 |
array of datums.
|
|
|
211 |
|
|
|
212 |
* `ajax` – The [ajax settings object] passed to `jQuery.ajax`.
|
|
|
213 |
|
|
|
214 |
<!-- section links -->
|
|
|
215 |
|
|
|
216 |
[local storage limits]: http://stackoverflow.com/a/2989317
|
|
|
217 |
[ajax settings object]:http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
|
|
|
218 |
|
|
|
219 |
### Remote
|
|
|
220 |
|
|
|
221 |
Remote data is only used when the data provided by `local` and `prefetch` is
|
|
|
222 |
insufficient. In order to prevent an obscene number of requests being made to
|
|
|
223 |
the remote endpoint, requests are rate-limited.
|
|
|
224 |
|
|
|
225 |
When configuring `remote`, the following options are available.
|
|
|
226 |
|
|
|
227 |
* `url` – A URL to make requests to when when the data provided by `local` and
|
|
|
228 |
`prefetch` is insufficient. **Required.**
|
|
|
229 |
|
|
|
230 |
* `wildcard` - The pattern in `url` that will be replaced with the user's query
|
|
|
231 |
when a request is made. Defaults to `%QUERY`.
|
|
|
232 |
|
|
|
233 |
* `replace` – A function with the signature `replace(url, query)` that can be
|
|
|
234 |
used to override the request URL. Expected to return a valid URL. If set, no
|
|
|
235 |
wildcard substitution will be performed on `url`.
|
|
|
236 |
|
|
|
237 |
* `rateLimitBy` – The method used to rate-limit network requests. Can be either
|
|
|
238 |
`debounce` or `throttle`. Defaults to `debounce`.
|
|
|
239 |
|
|
|
240 |
* `rateLimitWait` – The time interval in milliseconds that will be used by
|
|
|
241 |
`rateLimitBy`. Defaults to `300`.
|
|
|
242 |
|
|
|
243 |
* `filter` – A function with the signature `filter(parsedResponse)` that
|
|
|
244 |
transforms the response body into an array of datums. Expected to return an
|
|
|
245 |
array of datums.
|
|
|
246 |
|
|
|
247 |
* `ajax` – The [ajax settings object] passed to `jQuery.ajax`.
|
|
|
248 |
|
|
|
249 |
<!-- section links -->
|
|
|
250 |
|
|
|
251 |
[ajax settings object]: http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
|
|
|
252 |
|
|
|
253 |
### Datums
|
|
|
254 |
|
|
|
255 |
Datums are JavaScript objects that hydrate the pool of possible suggestions.
|
|
|
256 |
Bloodhound doesn't expect datums to contain any specific properties as any
|
|
|
257 |
operations performed on datums are done using functions defined by the user i.e.
|
|
|
258 |
`datumTokenizer`, `dupDetector`, and `sorter`.
|
|
|
259 |
|
|
|
260 |
### Tokens
|
|
|
261 |
|
|
|
262 |
The algorithm used by bloodhounds for providing suggestions for a given query
|
|
|
263 |
is token-based. When `Bloodhound#get` is called, it tokenizes `query` using
|
|
|
264 |
`queryTokenizer` and then invokes `cb` with all of the datums that contain those
|
|
|
265 |
tokens.
|
|
|
266 |
|
|
|
267 |
For a quick example, if a datum was tokenized into the following set of
|
|
|
268 |
tokens...
|
|
|
269 |
|
|
|
270 |
```javascript
|
|
|
271 |
['typeahead.js', 'typeahead', 'autocomplete', 'javascript'];
|
|
|
272 |
```
|
|
|
273 |
|
|
|
274 |
...it would be a valid match for queries such as:
|
|
|
275 |
|
|
|
276 |
* `typehead`
|
|
|
277 |
* `typehead.js`
|
|
|
278 |
* `autoco`
|
|
|
279 |
* `java type`
|