Skip to content

Commit 6bf2893

Browse files
committed
Add date filters and fuzzy matching
1 parent 39051cf commit 6bf2893

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,24 @@ client.search('keyword', callback);
3535
client.search(callback);
3636
```
3737

38-
#### Define language filter (e.g. "en" or "de")
38+
#### Use fuzzy matching
3939
```js
40+
// Enable fuzzy matching for typo tolerance (default "false")
41+
client.useFuzzyMatch(true);
42+
```
43+
44+
#### Define language filter
45+
```js
46+
// Documents in specific language (e.g. "en" or "de")
4047
client.setLanguage('en');
4148
```
4249

50+
#### Define publishing date filters
51+
```js
52+
// Documents published between specific date range
53+
client.setDateFilter('2019-01-01', '2019-01-31');
54+
```
55+
4356
#### Manage paging
4457
```js
4558
// Defaults: page "1", pageSize "10", sortBy "relevance", sortOrder "desc"

src/apifetch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
3838
var qs = '';
3939
if (type === 'search') {
4040
qs = settingToQueryParam(settings.lang, 'lang') +
41+
settingToQueryParam(settings.fuzzy, 'fuzzy') +
42+
settingToQueryParam(settings.dateFrom, 'dateFrom') +
43+
settingToQueryParam(settings.dateTo, 'dateTo') +
4144
settingToQueryParam(settings.paging.page, 'page') +
4245
settingToQueryParam(settings.paging.pageSize, 'limit') +
4346
settingToQueryParam(settings.paging.sortBy, 'sort') +

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ var client = function(sitekey) {
4747
*/
4848
this.getSettings = function() { return this.settings.getSettings(); }
4949
this.setLanguage = function(lang) { this.settings.setLanguage(lang); };
50+
this.useFuzzyMatch = function(use) { this.settings.useFuzzyMatch(use); };
51+
this.setDateFilter = function(dateFrom, dateTo) { this.settings.setDateFilter(dateFrom, dateTo); };
5052
this.setPaging = function(page, pageSize, sortBy, sortOder) { this.settings.setPaging(page, pageSize, sortBy, sortOder); };
5153
this.nextPage = function() { this.settings.nextPage(); };
5254
this.previousPage = function() { this.settings.previousPage(); };

src/settings.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,57 @@ var settings = function() {
1111
}
1212
};
1313

14-
this.getSettings = function () {
14+
this.getSettings = function() {
1515
return this.settings;
1616
}
1717

18-
this.setKeyword = function (keyword) {
18+
this.setKeyword = function(keyword) {
1919
this.settings.keyword = keyword || '*';
2020
}
2121

22-
this.setLanguage = function (language) {
23-
console.log('LANG SET TO ' + language);
22+
this.setLanguage = function(language) {
23+
if (language && language.length !== 2) {
24+
throw "use 2-char language code (e.g. \"en\")";
25+
}
2426
this.settings.lang = language;
2527
}
2628

27-
this.setPaging = function (page, pageSize, sortBy, sortOder) {
29+
this.useFuzzyMatch = function(fuzzy) {
30+
this.settings.fuzzy = fuzzy;
31+
}
32+
33+
this.setDateFilter = function(dateFrom, dateTo) {
34+
this.settings.dateFrom = dateFrom;
35+
this.settings.dateTo = dateTo;
36+
}
37+
38+
this.setKeyword = function(keyword) {
39+
this.settings.keyword = keyword || '*';
40+
}
41+
42+
this.setPaging = function(page, pageSize, sortBy, sortOder) {
43+
// Validate
44+
if (page < 1) {
45+
throw "page must be 1 or bigger";
46+
}
47+
if (pageSize < 1 || pageSize > 50) {
48+
throw "pageSize must be 1-50";
49+
}
50+
if (sortOrder !== 'asc' && sortOrder !== 'desc') {
51+
throw "sortOrder must be asc or desc";
52+
}
53+
2854
this.settings.paging.page = page;
2955
this.settings.paging.pageSize = pageSize;
3056
this.settings.paging.sortBy = sortBy;
3157
this.settings.paging.sortOder = sortOder;
3258
}
33-
this.nextPage = function () {
59+
60+
this.nextPage = function() {
3461
this.settings.paging.page = this.settings.paging.page + 1;
3562
}
36-
this.previousPage = function () {
63+
64+
this.previousPage = function() {
3765
if (this.settings.paging.page > 0) {
3866
this.settings.paging.page = this.settings.paging.page - 1;
3967
}

0 commit comments

Comments
 (0)