Skip to content

Commit 4e5ec56

Browse files
authored
Merge pull request #14 from AddSearch/indexing-api
Indexing api
2 parents acae570 + e6fcb6d commit 4e5ec56

File tree

7 files changed

+416
-24
lines changed

7 files changed

+416
-24
lines changed

README.md

Lines changed: 183 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ with JavaScript on web browsers or from Node.js.
77
## Quick Start
88
The library is available on the global CDN [jsDelivr:](https://www.jsdelivr.com/package/npm/addsearch-js-client)
99
```html
10-
<script src="https://cdn.jsdelivr.net/npm/addsearch-js-client@0.4/dist/addsearch-js-client.min.js"></script>
10+
<script src="https://cdn.jsdelivr.net/npm/addsearch-js-client@0.5/dist/addsearch-js-client.min.js"></script>
1111
```
1212
Or install the library locally to use it with Node.js:
1313
```sh
@@ -40,9 +40,10 @@ var cb = function(res) {
4040
client.search('keyword', cb);
4141
```
4242

43-
## Publicly accessible functions
43+
## Search API
4444

45-
The client provides the following functions.
45+
The client provides following functions to execute search queries. To use the client library for indexing,
46+
see [Indexing API](https://github.com/AddSearch/js-client-library#indexing-api).
4647

4748
#### Fetch search results
4849
```js
@@ -301,18 +302,194 @@ client.setJWT(token);
301302

302303
#### Set API throttling
303304
```js
304-
// Set API call throttle time in milliseconds. Default is 200.
305+
// Set Search API throttle time in milliseconds. Default is 200.
305306
client.setThrottleTime(500);
306307
```
307308

308-
## Supported web browsers and node.js versions
309+
## Indexing API
310+
With the Indexing API, you can fetch, create, update, and delete single documents or
311+
batches of documents.
312+
313+
Indexing API functions are meant to be used with Node.js. Never expose secret key in your
314+
website code.
315+
316+
```js
317+
// Create client with your keys
318+
var client = new AddSearchClient('YOUR PUBLIC SITEKEY', 'YOUR SECRET KEY');
319+
```
320+
321+
The secret key can be found from AddSearch Dashboard's "Setup" > "Keys and installation" page.
322+
Always keep the key secret.
323+
324+
All Indexing API functions are Promise-based.
325+
326+
### Document structure
327+
Documents can contain a set of pre-defined fields, as well as any number of custom fields
328+
defined under the **custom_fields** key.
329+
330+
Using pre-defined fields is optional, but default [Search UI](https://github.com/AddSearch/search-ui) components
331+
display them by default, so pre-defined field give you visible results a bit faster.
332+
333+
Pre-defined fields are: url, title, and main_content.
334+
335+
Example document:
336+
```js
337+
const doc = {
338+
id: '1234',
339+
url: 'https://www.example-store.com/product-x',
340+
title: 'Example product',
341+
main_content: 'Lorem ipsum',
342+
custom_fields: {
343+
'name': 'Example product',
344+
'description': 'Description for the example product',
345+
'price_cents': 599,
346+
'average_customer_rating': 4.5,
347+
'release_date': 1589200255
348+
}
349+
}
350+
```
351+
352+
Data types for custom fields are automatically detected from the content. Supported data types are:
353+
354+
- text
355+
- integer
356+
- double
357+
358+
Dates should be defined as UNIX timestamps with integer values.
359+
360+
### Document ID
361+
362+
If the **id** is not defined in the document at indexing time, it is generated automatically either randomly
363+
or from the **url** field.
364+
365+
```js
366+
// ID defined by the user
367+
const docWithDefinedId = {
368+
id: '1234',
369+
custom_fields: {}
370+
}
371+
```
372+
```js
373+
// ID created from the URL field (md5 of the url)
374+
const docWithURL= {
375+
url: 'https://..',
376+
custom_fields: {}
377+
}
378+
```
379+
```js
380+
// ID generated randomly
381+
const docWithAutogeneratedId = {
382+
// No id or url fields
383+
custom_fields: {}
384+
}
385+
```
386+
387+
### Save document
388+
Add a document to the index, or update a document.
389+
390+
```js
391+
const doc = {
392+
id: '1234',
393+
custom_fields: {
394+
'name': 'Example product'
395+
}
396+
};
397+
398+
// Save document
399+
client.saveDocument(doc)
400+
.then(response => {
401+
console.log(response);
402+
})
403+
.catch(error => {
404+
console.log(error);
405+
});
406+
```
407+
408+
409+
### Get document by ID
410+
Fetch a specific document by ID.
411+
```js
412+
client.getDocument(id)
413+
.then(response => {
414+
console.log(response);
415+
})
416+
.catch(error => {
417+
console.log(error);
418+
});
419+
```
420+
421+
422+
### Delete document by ID
423+
Delete a specific document by ID.
424+
```js
425+
client.deleteDocument(id)
426+
.then(response => {
427+
console.log(response);
428+
})
429+
.catch(error => {
430+
console.log(error);
431+
});
432+
```
433+
434+
435+
### Save batch of documents
436+
Add or update bunch of documents defined in an array.
437+
```js
438+
const batch = {
439+
documents: [
440+
{
441+
id: '1234',
442+
custom_fields: {
443+
'name': 'Product 1'
444+
}
445+
},
446+
{
447+
id: '5678',
448+
custom_fields: {
449+
'name': 'Product 2'
450+
}
451+
}
452+
]
453+
};
454+
455+
// Save batch of documents
456+
client.saveDocumentsBatch(batch)
457+
.then(response => {
458+
console.log(response);
459+
})
460+
.catch(error => {
461+
console.log(error);
462+
});
463+
```
464+
465+
466+
### Delete batch of documents
467+
Delete multiple documents with an array of document IDs.
468+
```js
469+
// Array of document IDs
470+
const batch = {
471+
documents: ["1234", "5678"]
472+
};
473+
474+
// Delete batch of documents
475+
client.deleteDocumentsBatch(batch)
476+
.then(response => {
477+
console.log(response);
478+
})
479+
.catch(error => {
480+
console.log(error);
481+
});
482+
```
483+
484+
485+
## Supported browsers
309486
The client is tested on
310487
- Chrome
311488
- Firefox
312489
- Edge
313490
- Safari 6.1+
314491
- Internet Explorer 10+
315-
- Node.js 4+
492+
- Node.js
316493

317494

318495
## Development

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,21 @@
3737
"license": "MIT",
3838
"dependencies": {
3939
"es6-promise": "^4.2.8",
40-
"isomorphic-fetch": "^2.2.1"
40+
"isomorphic-fetch": "^2.2.1",
41+
"js-base64": "^3.4.5"
4142
},
4243
"devDependencies": {
43-
"@babel/cli": "^7.8.4",
44-
"@babel/core": "^7.8.6",
45-
"@babel/preset-env": "^7.8.6",
46-
"@babel/register": "^7.8.6",
47-
"babel-loader": "^8.0.6",
44+
"@babel/cli": "^7.10.5",
45+
"@babel/core": "^7.11.1",
46+
"@babel/preset-env": "^7.11.0",
47+
"@babel/register": "^7.10.5",
48+
"babel-loader": "^8.1.0",
4849
"esm": "^3.2.25",
49-
"fetch-mock": "^9.0.0",
50-
"mocha": "^7.1.0",
50+
"fetch-mock": "^9.10.6",
51+
"mocha": "^8.1.1",
5152
"node-fetch": "^2.6.0",
52-
"uglify-js": "^3.8.0",
53-
"webpack": "^4.42.0",
54-
"webpack-cli": "^3.3.11"
53+
"uglify-js": "^3.10.1",
54+
"webpack": "^4.44.1",
55+
"webpack-cli": "^3.3.12"
5556
}
5657
}

src/index.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
'use strict';
22

33
var executeApiFetch = require('./apifetch');
4+
var indexingapi = require('./indexingapi');
45
var sendStats = require('./stats');
56
var Settings = require('./settings');
67
var util = require('./util');
78
var throttle = require('./throttle');
89

910
var API_THROTTLE_TIME_MS = 200;
1011

11-
var client = function(sitekey) {
12+
var client = function(sitekey, privatekey) {
1213
this.sitekey = sitekey;
14+
this.privatekey = privatekey;
1315
this.settings = new Settings();
1416
this.sessionId = ('a-' + (Math.random() * 100000000)).substring(0, 10);
1517

@@ -91,6 +93,38 @@ var client = function(sitekey) {
9193
}
9294

9395

96+
/**
97+
* Indexing API functions
98+
*/
99+
this.getDocument = function(id) {
100+
return indexingapi.getDocument(this.sitekey, this.privatekey, id);
101+
}
102+
103+
this.saveDocument = function(document) {
104+
return indexingapi.saveDocument(this.sitekey, this.privatekey, document);
105+
}
106+
107+
this.saveDocumentsBatch = function(batch) {
108+
if (!batch || !batch.documents || !Array.isArray(batch.documents)) {
109+
throw "Please provide an array of documents: {documents: []}";
110+
}
111+
return indexingapi.saveDocumentsBatch(this.sitekey, this.privatekey, batch);
112+
}
113+
114+
this.deleteDocument = function(id) {
115+
return indexingapi.deleteDocument(this.sitekey, this.privatekey, id);
116+
}
117+
118+
this.deleteDocumentsBatch = function(batch) {
119+
if (!batch || !batch.documents || !Array.isArray(batch.documents)) {
120+
throw "Please provide an array of document ids: {documents: []}";
121+
}
122+
return indexingapi.deleteDocumentsBatch(this.sitekey, this.privatekey, batch);
123+
}
124+
125+
126+
127+
94128
/**
95129
* Public functions
96130
*/

0 commit comments

Comments
 (0)