Skip to content

Commit 02b81c4

Browse files
authored
Merge pull request #11 from AddSearch/field-stats
Add field statistics
2 parents da2458d + 2dd1bd0 commit 02b81c4

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# AddSearch Search API Client for JavaScript
22

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

77
## Quick Start
8-
The library is available on the global CDN [jsDelivr:](https://www.jsdelivr.com/)
8+
The library is available on the global CDN [jsDelivr:](https://www.jsdelivr.com/package/npm/addsearch-js-client)
99
```html
1010
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/addsearch-js-client.min.js"></script>
1111
```
12-
To install the library locally or to use it with Node.js:
12+
Or install the library locally to use it with Node.js:
1313
```sh
1414
npm install addsearch-js-client --save
1515
```
@@ -209,10 +209,11 @@ Use the following function to get more or less facets.
209209
client.setNumberOfFacets(20);
210210
```
211211

212-
#### Numeric range facets
212+
#### Numerical range facets
213213
Group numerical custom fields into range buckets.
214214
```js
215-
// Define ranges. E.g. products with price $0-$100, $100-$200, and over $200 (from value is inclusive, to value is exclusive)
215+
// Define ranges. E.g. products with price $0-$100, $100-$200, and over $200.
216+
// From value is inclusive, to value is exclusive
216217
var ranges = [
217218
{'to': 100},
218219
{'from': 100, 'to': 200},
@@ -223,6 +224,15 @@ var ranges = [
223224
client.addRangeFacet('custom_fields.price', ranges);
224225
```
225226

227+
### Field statistics
228+
Get minimum, maximum, and average values of a numerical or date-based custom field. The information
229+
is handy for applications like range filtering.
230+
```js
231+
// Search response will have a fieldStats element with information like
232+
// custom_fields.price: {min: 1230, max: 1590, avg: 1382}
233+
client.addStatsField('custom_fields.price');
234+
```
235+
226236
### Search analytics
227237
#### Send search event to analytics
228238
When search is executed, send the event to your AddSearch Analytics Dashboard.

src/apifetch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ var executeApiFetch = function(sitekey, type, settings, cb, fuzzyRetry) {
101101
}
102102

103103

104+
// Stats fields
105+
if (settings.statsFields) {
106+
for (var i = 0; i<settings.statsFields.length; i++) {
107+
qs = qs + '&fieldStat=' + settings.statsFields[i];
108+
}
109+
}
110+
111+
104112
// Personalization events
105113
if (settings.personalizationEvents && Array.isArray(settings.personalizationEvents)) {
106114
for (var i = 0; i<settings.personalizationEvents.length; i++) {

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ var client = function(sitekey) {
110110
this.setAutocompleteSize = function(size) { this.settings.setAutocompleteSize(size); }
111111
this.addFacetField = function(fieldName) { this.settings.addFacetField(fieldName); }
112112
this.addRangeFacet = function(field, ranges) { this.settings.addRangeFacet(field, ranges); }
113+
this.addStatsField = function(field) { this.settings.addStatsField(field); }
113114
this.setNumberOfFacets = function(numFacets) { this.settings.setNumberOfFacets(numFacets); }
114115
this.setResultType = function(type) { this.settings.setResultType(type); }
115116
this.setPersonalizationEvents = function(events) { this.settings.setPersonalizationEvents(events); }

src/settings.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ var settings = function() {
154154
});
155155
}
156156

157+
this.addStatsField = function(field) {
158+
if (!this.settings.statsFields) {
159+
this.settings.statsFields = [];
160+
}
161+
if (this.settings.statsFields.indexOf(field) === -1) {
162+
this.settings.statsFields.push(field);
163+
}
164+
}
165+
157166
this.setNumberOfFacets = function(numFacets) {
158167
this.settings.numFacets = numFacets;
159168
}

0 commit comments

Comments
 (0)