Skip to content

Commit 1011aeb

Browse files
authored
Merge pull request #3 from AddSearch/autocomplete
Autocomplete
2 parents 7bab037 + f7e7ce8 commit 1011aeb

File tree

5 files changed

+85
-12
lines changed

5 files changed

+85
-12
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,33 @@ client.search();
5757
```
5858

5959
#### Fetch search suggestions
60+
Search suggestions are keywords and search phrases that real users have used in your search. Configure Search
61+
suggestions on AddSearch Dashboard before using this function.
6062
```js
61-
// Configure Search suggestions on AddSearch Dashboard first
6263
// Get suggestions starting with a specific prefix
6364
client.suggestions('a', callback);
6465
```
6566

66-
#### Number of search suggestions
67+
#### Set the number of search suggestions to fetch
6768
```js
6869
// Number of search suggestions to fetch (default 10)
6970
client.setSuggestionsSize(20);
7071
```
7172

73+
#### Fetch custom field autocompletion
74+
Custom fields autocomplete can be used for predictive search. For example, product names or categories can be
75+
suggested as the keyword is being typed in.
76+
```js
77+
// Fetch custom field values starting with a specific prefix. In this example, results could be "adidas, apple, azure"
78+
client.autocomplete('custom_fields.brand', 'a', callback);
79+
```
80+
81+
#### Set the number of custom field autocompletion results to fetch
82+
```js
83+
// Number of autocompletion results to fetch (default 10)
84+
client.setAutocompleteSize(20);
85+
```
86+
7287
#### Search with fuzzy matching
7388
```js
7489
// Enable/disable fuzzy matching. Possible values true/false/"auto" (default: "auto")
@@ -163,6 +178,22 @@ client.removeCustomFieldFilter('city','paris');
163178
client.removeCustomFieldFilter('city');
164179
```
165180

181+
#### Set filtering object
182+
Set complex filtering object that can contain nested *and*, *or*, *not*, and *range* filters.
183+
184+
```js
185+
// Find results where brand is apple, color is not white, and price is between 200 and 500
186+
var filter = {
187+
'and':[
188+
{'custom_fields.brand': 'apple'},
189+
{'not': {'custom_fields.color': 'white'}},
190+
{'range': {'custom_fields.price': {'gt': 200, 'lt':500}}}
191+
]
192+
};
193+
194+
client.setFilterObject(filter);
195+
```
196+
166197
#### Manage paging
167198
Set page number, page size and sorting parameters. It's possible to order results by:
168199
- relevance (descending)

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@
4040
"isomorphic-fetch": "^2.2.1"
4141
},
4242
"devDependencies": {
43-
"@babel/cli": "^7.8.3",
44-
"@babel/core": "^7.8.3",
45-
"@babel/preset-env": "^7.8.3",
46-
"@babel/register": "^7.8.3",
43+
"@babel/cli": "^7.8.4",
44+
"@babel/core": "^7.8.4",
45+
"@babel/preset-env": "^7.8.4",
46+
"@babel/register": "^7.7.7",
4747
"babel-loader": "^8.0.6",
4848
"esm": "^3.2.25",
49-
"fetch-mock": "^8.3.1",
50-
"mocha": "^7.0.0",
49+
"fetch-mock": "^8.3.2",
50+
"mocha": "^6.2.2",
5151
"node-fetch": "^2.6.0",
52-
"uglify-js": "^3.7.6",
52+
"uglify-js": "^3.7.7",
5353
"webpack": "^4.41.5",
5454
"webpack-cli": "^3.3.10"
5555
}

src/apifetch.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
2020

2121

2222
// Validate query type
23-
if (type !== 'search' && type !== 'suggest') {
23+
if (type !== 'search' && type !== 'suggest' && type !== 'autocomplete') {
2424
cb({error: {response: RESPONSE_BAD_REQUEST, message: 'invalid query type'}});
2525
return;
2626
}
@@ -29,8 +29,14 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
2929
var kw = '';
3030
var qs = '';
3131

32+
// API Path (eq. /search, /suggest, /autocomplete/document-field)
33+
var apiPath = null;
34+
3235
// Search
3336
if (type === 'search') {
37+
// Path
38+
apiPath = type;
39+
3440
// Keyword
3541
kw = settings.keyword;
3642

@@ -93,13 +99,22 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
9399

94100
// Suggest
95101
else if (type === 'suggest') {
102+
apiPath = type;
96103
qs = settingToQueryParam(settings.suggestionsSize, 'size');
97104
kw = settings.suggestionsPrefix;
98105
}
99106

107+
// Autocomplete
108+
else if (type === 'autocomplete') {
109+
apiPath = 'autocomplete/document-field';
110+
qs = settingToQueryParam(settings.autocomplete.field, 'source') +
111+
settingToQueryParam(settings.autocomplete.size, 'size');
112+
kw = settings.autocomplete.prefix;
113+
}
114+
100115

101116
// Execute API call
102-
fetch('https://api.addsearch.com/v1/' + type + '/' + sitekey + '?term=' + kw + qs)
117+
fetch('https://api.addsearch.com/v1/' + apiPath + '/' + sitekey + '?term=' + kw + qs)
103118
.then(function(response) {
104119
return response.json();
105120
}).then(function(json) {

src/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ var client = function(sitekey) {
6262
}
6363

6464

65+
/**
66+
* Fetch field autocompletes
67+
*
68+
* @param keyword
69+
*/
70+
this.autocomplete = function(field, prefix, callback) {
71+
if (!field || !prefix || !callback || !util.isFunction(callback)) {
72+
throw "Illegal autocomplete parameters. Should be (field, prefix, callbackFunction)";
73+
}
74+
this.settings.setAutocompleteParams(field, prefix);
75+
executeApiFetch(this.sitekey, 'autocomplete', this.settings.getSettings(), callback);
76+
}
77+
78+
6579
/**
6680
* Public functions
6781
*/
@@ -78,6 +92,7 @@ var client = function(sitekey) {
7892
this.nextPage = function() { this.settings.nextPage(); }
7993
this.previousPage = function() { this.settings.previousPage(); }
8094
this.setSuggestionsSize = function(size) { this.settings.setSuggestionsSize(size); }
95+
this.setAutocompleteSize = function(size) { this.settings.setAutocompleteSize(size); }
8196
this.addFacetField = function(fieldName) { this.settings.addFacetField(fieldName); }
8297
this.setNumberOfFacets = function(numFacets) { this.settings.setNumberOfFacets(numFacets); }
8398
this.setResultType = function(type) { this.settings.setResultType(type); }

src/settings.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ var settings = function() {
1414
customFieldFilters: [],
1515
userToken: null,
1616
suggestionsSize: 10,
17-
facetFields: []
17+
facetFields: [],
18+
autocomplete: {
19+
size: 10
20+
}
1821
};
1922

2023
this.getSettings = function() {
@@ -37,6 +40,15 @@ var settings = function() {
3740
this.settings.suggestionsSize = size;
3841
}
3942

43+
this.setAutocompleteSize = function(size) {
44+
this.settings.autocomplete.size = size;
45+
}
46+
47+
this.setAutocompleteParams = function(field, prefix) {
48+
this.settings.autocomplete.field = field;
49+
this.settings.autocomplete.prefix = prefix;
50+
}
51+
4052
this.setLanguage = function(language) {
4153
if (language && language.length !== 2) {
4254
throw "use 2-char language code (e.g. \"en\")";

0 commit comments

Comments
 (0)