Skip to content

Commit b677a21

Browse files
committed
Add facets
1 parent c85bfa8 commit b677a21

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ The client provides the following functions.
4141
// Search with a specific keyword
4242
client.search('keyword', callback);
4343

44-
// Match all query
45-
client.search('*', callback);
46-
4744
// Search with the previously used keyword or execute a "match all" query
4845
client.search(callback);
46+
47+
// Search with the previously used keyword and callback (e.g. after modifying filters)
48+
client.search();
4949
```
5050

5151
#### Fetch search suggestions
@@ -55,9 +55,15 @@ client.search(callback);
5555
client.suggestions('a', callback);
5656
```
5757

58-
#### Use fuzzy matching
58+
#### Number of search suggestions
59+
```js
60+
// Number of search suggestions to fetch (default 10)
61+
client.setSuggestionsSize(20);
62+
```
63+
64+
#### Search with fuzzy matching
5965
```js
60-
// Enable/disable fuzzy matching used in typo tolerance (default "true")
66+
// Enable/disable fuzzy matching for typo tolerance (default "true")
6167
client.useFuzzyMatch(false);
6268
```
6369

@@ -129,6 +135,14 @@ client.nextPage();
129135
client.previousPage();
130136
```
131137

138+
#### Facets
139+
```js
140+
// Declare fields for faceting. Number of hits found from
141+
// these fields will be returned
142+
client.addFacetField('category');
143+
client.addFacetField('custom_fields.genre');
144+
```
145+
132146
## Supported web browsers and node.js versions
133147
The client is tested on
134148
- Chrome

src/apifetch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,19 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
6262
qs = qs + '&customField=' + settings.customFieldFilters[i];
6363
}
6464
}
65+
66+
// Add facet fields
67+
if (settings.facetFields) {
68+
for (var i = 0; i < settings.facetFields.length; i++) {
69+
qs = qs + '&facet=' + settings.facetFields[i];
70+
}
71+
}
6572
}
6673
}
6774

6875
// Suggest
6976
else if (type === 'suggest') {
77+
qs = settingToQueryParam(settings.suggestionsSize, 'size');
7078
kw = settings.suggestionsPrefix;
7179
}
7280

src/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ var client = function(sitekey) {
3232
keyword = this.settings.getSettings().keyword;
3333
callback = a1;
3434
}
35+
// Use previous keyword and callback
36+
else if (this.settings.getSettings().callback) {
37+
keyword = this.settings.getSettings().keyword;
38+
callback = this.settings.getSettings().callback;
39+
}
3540
else {
36-
throw "Illegal search parameters. Should be (keyword, callbackFunction) or just (callbackFunction)";
41+
throw "Illegal search parameters. Should be (keyword, callbackFunction) or (callbackFunction)";
3742
}
3843

44+
this.settings.setCallback(callback);
3945
this.settings.setKeyword(keyword);
4046
executeApiFetch(this.sitekey, 'search', this.settings.getSettings(), callback);
4147
}
@@ -71,6 +77,8 @@ var client = function(sitekey) {
7177
this.setPaging = function(page, pageSize, sortBy, sortOder) { this.settings.setPaging(page, pageSize, sortBy, sortOder); }
7278
this.nextPage = function() { this.settings.nextPage(); }
7379
this.previousPage = function() { this.settings.previousPage(); }
80+
this.setSuggestionsSize = function(size) { this.settings.setSuggestionsSize(size); }
81+
this.addFacetField = function(fieldName) { this.settings.addFacetField(fieldName); }
7482
//this.hitClicked = function(docid, position) { sendClickHit(this.sitekey, this.settings.getSettings().keyword, docid, position); }
7583
}
7684

src/settings.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var settings = function() {
44
this.settings = {
55
keyword: '*',
6+
callback: null,
67
fuzzy: true,
78
paging: {
89
page: 1,
@@ -11,7 +12,9 @@ var settings = function() {
1112
sortOrder: 'desc'
1213
},
1314
customFieldFilters: [],
14-
userToken: null
15+
userToken: null,
16+
suggestionsSize: 10,
17+
facetFields: []
1518
};
1619

1720
this.getSettings = function() {
@@ -22,10 +25,18 @@ var settings = function() {
2225
this.settings.keyword = keyword || '*';
2326
}
2427

28+
this.setCallback = function(cb) {
29+
this.settings.callback = cb;
30+
}
31+
2532
this.setSuggestionsPrefix = function(prefix) {
2633
this.settings.suggestionsPrefix = prefix;
2734
}
2835

36+
this.setSuggestionsSize = function(size) {
37+
this.settings.suggestionsSize = size;
38+
}
39+
2940
this.setLanguage = function(language) {
3041
if (language && language.length !== 2) {
3142
throw "use 2-char language code (e.g. \"en\")";
@@ -90,6 +101,10 @@ var settings = function() {
90101
this.settings.userToken = token;
91102
}
92103

104+
this.addFacetField = function(field) {
105+
this.settings.facetFields.push(field);
106+
}
107+
93108
this.setPaging = function(page, pageSize, sortBy, sortOrder) {
94109
// Validate
95110
if (page < 1) {

0 commit comments

Comments
 (0)