Skip to content

Commit f802ffb

Browse files
committed
Add category and custom fields filters
1 parent f5aaad6 commit f802ffb

File tree

4 files changed

+91
-21
lines changed

4 files changed

+91
-21
lines changed

README.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[AddSearch](https://www.addsearch.com) is a hosted search platform for all your web content. This API
44
Client lets you easily use the [AddSearch Search API](https://www.addsearch.com/support/api-reference/)
5-
from your JavaScript code. This client works on web browsers and with Node.js.
5+
from your JavaScript code on web browsers or with Node.js.
66

77
## Quick Start
88
```js
@@ -17,9 +17,6 @@ var cb = function(res) {
1717

1818
// Execute search. Callback function cb will be called with search results
1919
client.search('keyword', cb);
20-
21-
// Get search suggestions for a keyword
22-
client.suggest('api', cb);
2320
```
2421

2522
## Publicly accessible functions
@@ -53,6 +50,32 @@ client.setLanguage('en');
5350
client.setDateFilter('2019-01-01', '2019-01-31');
5451
```
5552

53+
#### Define category filters
54+
Filter by URL patterns, document types or *addsearch-category* meta tag values.
55+
See [full documentation.](https://www.addsearch.com/support/documentation/ranking-relevance-filters/filters/#category-filters)
56+
57+
```js
58+
// Only PDF files or products
59+
client.setCategoryFilters('doctype_pdf,products');
60+
```
61+
62+
#### Custom field filters
63+
Filter by custom fields. Custon fields can be defined in meta tags or AddSearch crawler can pick them up from your HTML or JSON data.
64+
See [full documentation.](https://www.addsearch.com/support/documentation/ranking-relevance-filters/custom-field/)
65+
66+
```js
67+
// Search by specific city (Berlin, Paris or Boston)
68+
client.addCustomFieldFilter('city','berlin');
69+
client.addCustomFieldFilter('city','paris');
70+
client.addCustomFieldFilter('city','boston');
71+
72+
// Remove paris (Berlin and Boston remaining)
73+
client.removeCustomFieldFilter('city','paris');
74+
75+
// Remove all cities
76+
client.removeCustomFieldFilter('city');
77+
```
78+
5679
#### Manage paging
5780
```js
5881
// Defaults: page "1", pageSize "10", sortBy "relevance", sortOrder "desc"
@@ -65,31 +88,35 @@ client.nextPage();
6588
client.previousPage();
6689
```
6790

68-
## Browser support
69-
The client is tested on following browsers
91+
## Supported web browsers and node.js versions
92+
The client is tested on
7093
- Chrome
7194
- Firefox
7295
- Edge
7396
- Safari 6.1+
7497
- Internet Explorer 10+
98+
- Node.js 4+
99+
75100

76101
## Development
77102
To modify this client library, clone this repository to your computer and execute following commands.
78-
### Install dependencies
103+
#### Install dependencies
79104
```sh
80105
npm install
81106
```
82107

83-
### Run tests
108+
#### Run tests
84109
```sh
85110
npm test
86111
```
87112

88-
### Build
89-
To build the client, run:
90-
113+
#### Build
91114
```sh
92115
npm run build
93116
```
94117

95-
Built bundle is saved under the *dist/* folder
118+
Built bundle is saved under the *dist/* folder
119+
120+
## Support
121+
122+
Feel free to semd any questions, ideas and suggestions at [support@addsearch.com](support@addsearch.com).

src/apifetch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,20 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
3939
if (type === 'search') {
4040
qs = settingToQueryParam(settings.lang, 'lang') +
4141
settingToQueryParam(settings.fuzzy, 'fuzzy') +
42+
settingToQueryParam(settings.categories, 'categories') +
4243
settingToQueryParam(settings.dateFrom, 'dateFrom') +
4344
settingToQueryParam(settings.dateTo, 'dateTo') +
4445
settingToQueryParam(settings.paging.page, 'page') +
4546
settingToQueryParam(settings.paging.pageSize, 'limit') +
4647
settingToQueryParam(settings.paging.sortBy, 'sort') +
4748
settingToQueryParam(settings.paging.sortOrder, 'order');
49+
50+
// Add custom field filters
51+
if (settings.customFieldFilters) {
52+
for (var i=0; i<settings.customFieldFilters.length; i++) {
53+
qs = qs + '&customField=' + settings.customFieldFilters[i];
54+
}
55+
}
4856
}
4957

5058

src/index.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var client = function(sitekey) {
2929

3030
this.settings.setKeyword(keyword);
3131
executeApiFetch(this.sitekey, 'search', this.settings.getSettings(), callback);
32-
};
32+
}
3333

3434

3535
/**
@@ -39,19 +39,22 @@ var client = function(sitekey) {
3939
*/
4040
this.suggest = function(keyword, callback) {
4141
executeApiFetch(this.sitekey, 'suggest', this.settings.getSettings(), callback);
42-
};
42+
}
4343

4444

4545
/**
4646
* Public functions
4747
*/
4848
this.getSettings = function() { return this.settings.getSettings(); }
49-
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); };
52-
this.setPaging = function(page, pageSize, sortBy, sortOder) { this.settings.setPaging(page, pageSize, sortBy, sortOder); };
53-
this.nextPage = function() { this.settings.nextPage(); };
54-
this.previousPage = function() { this.settings.previousPage(); };
49+
this.setLanguage = function(lang) { this.settings.setLanguage(lang); }
50+
this.useFuzzyMatch = function(use) { this.settings.useFuzzyMatch(use); }
51+
this.setCategoryFilters = function(categories) { this.settings.setCategoryFilters(categories); }
52+
this.addCustomFieldFilter = function(fieldName, value) { this.settings.addCustomFieldFilter(fieldName, value); }
53+
this.removeCustomFieldFilter = function(fieldName, value) { this.settings.removeCustomFieldFilter(fieldName, value); }
54+
this.setDateFilter = function(dateFrom, dateTo) { this.settings.setDateFilter(dateFrom, dateTo); }
55+
this.setPaging = function(page, pageSize, sortBy, sortOder) { this.settings.setPaging(page, pageSize, sortBy, sortOder); }
56+
this.nextPage = function() { this.settings.nextPage(); }
57+
this.previousPage = function() { this.settings.previousPage(); }
5558

5659
}
5760

src/settings.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var settings = function() {
99
pageSize: 10,
1010
sortBy: 'relevance',
1111
sortOrder: 'desc'
12-
}
12+
},
13+
customFieldFilters: []
1314
};
1415

1516
this.getSettings = function() {
@@ -31,6 +32,37 @@ var settings = function() {
3132
this.settings.fuzzy = fuzzy;
3233
}
3334

35+
this.setCategoryFilters = function(categories) {
36+
this.settings.categories = categories;
37+
}
38+
39+
this.addCustomFieldFilter = function(fieldName, value) {
40+
var filter = encodeURIComponent(fieldName + '=' + value);
41+
this.settings.customFieldFilters.push(filter);
42+
}
43+
44+
this.removeCustomFieldFilter = function(fieldName, value) {
45+
var removeAll = false;
46+
var filter = encodeURIComponent(fieldName + '=' + value);
47+
48+
// Remove all by fieldName
49+
if (!value) {
50+
removeAll = true;
51+
filter = encodeURIComponent(fieldName + '=');
52+
}
53+
54+
for (var i=this.settings.customFieldFilters.length; i>0; i--) {
55+
var v = this.settings.customFieldFilters[i-1];
56+
57+
if (removeAll && v.indexOf(filter) === 0) {
58+
this.settings.customFieldFilters.splice(i-1, 1);
59+
}
60+
else if (v === filter) {
61+
this.settings.customFieldFilters.splice(i-1, 1);
62+
}
63+
}
64+
}
65+
3466
this.setDateFilter = function(dateFrom, dateTo) {
3567
this.settings.dateFrom = dateFrom;
3668
this.settings.dateTo = dateTo;

0 commit comments

Comments
 (0)