Skip to content

Commit 4bea4ce

Browse files
committed
Add indexing API docs
1 parent 4b08aae commit 4bea4ce

File tree

1 file changed

+168
-4
lines changed

1 file changed

+168
-4
lines changed

README.md

Lines changed: 168 additions & 4 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) section.
4647

4748
#### Fetch search results
4849
```js
@@ -305,14 +306,177 @@ client.setJWT(token);
305306
client.setThrottleTime(500);
306307
```
307308

309+
## Indexing API
310+
The Indexing API allows for retrieving documents, creating documents, updating single documents and batches of
311+
documents, as well as deleting single documents and batches of documents.
312+
313+
Following indexing API functions are meant to be used in Node.js. Never expose your
314+
secret key in your website code.
315+
316+
```js
317+
// Create client with your 32-character 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+
Indexing API functions return promises.
325+
326+
327+
### Document ID
328+
329+
Document ID can be defined by the user, or it can be autogenerated when a document is added to the index.
330+
331+
```js
332+
// ID defined by the user
333+
const doc = {
334+
id: '1234',
335+
...
336+
}
337+
338+
// ID created from the URL field (md5 sum)
339+
const doc = {
340+
url: 'https://..', // id will be md5('https://..')
341+
...
342+
}
343+
344+
// ID will be generated automatically
345+
const doc = {
346+
// No id or url fields
347+
...
348+
}
349+
```
350+
351+
352+
### Document structure
353+
Documents have schemaless data structures. Data fields should be defined under custom_fields key.
354+
355+
```js
356+
// Create client with your 32-character keys
357+
const doc = {
358+
id: '1234',
359+
custom_fields: {
360+
'name': 'Example product',
361+
'description': 'Description for example product',
362+
'price_cents': 599,
363+
'average_customer_rating': 4.5,
364+
'release_date': 1589200255
365+
}
366+
}
367+
```
368+
369+
Data types for custom fields are automatically detected from the content. Supported data types are:
370+
371+
- text
372+
- integer
373+
- double
374+
375+
Dates should be defined as UNIX timestamps with integer values.
376+
377+
378+
### Save document
379+
Add or update a document in the index.
380+
381+
```js
382+
const doc = {
383+
id: '1234',
384+
custom_fields: {
385+
'name': 'Example product'
386+
}
387+
};
388+
389+
// Save document
390+
client.saveDocument(doc)
391+
.then(response => {
392+
console.log(response);
393+
})
394+
.catch(error => {
395+
console.log(error);
396+
});
397+
```
398+
399+
400+
### Get document by ID
401+
```js
402+
client.getDocument(id)
403+
.then(response => {
404+
console.log(response);
405+
})
406+
.catch(error => {
407+
console.log(error);
408+
});
409+
```
410+
411+
412+
### Delete document by ID
413+
```js
414+
client.deleteDocument(id)
415+
.then(response => {
416+
console.log(response);
417+
})
418+
.catch(error => {
419+
console.log(error);
420+
});
421+
```
422+
423+
424+
### Save batch of documents
425+
```js
426+
const batch = {
427+
documents: [
428+
{
429+
id: '1234',
430+
custom_fields: {
431+
'name': 'Product 1'
432+
}
433+
},
434+
{
435+
id: '5678',
436+
custom_fields: {
437+
'name': 'Product 2'
438+
}
439+
}
440+
]
441+
};
442+
443+
// Save batch of documents
444+
client.saveDocumentsBatch(batch)
445+
.then(response => {
446+
console.log(response);
447+
})
448+
.catch(error => {
449+
console.log(error);
450+
});
451+
```
452+
453+
454+
### Delete batch of documents
455+
```js
456+
// Array of document IDs
457+
const batch = {
458+
documents: ["1234", "5678"]
459+
};
460+
461+
// Delete batch of documents
462+
client.deleteDocumentsBatch(batch)
463+
.then(response => {
464+
console.log(response);
465+
})
466+
.catch(error => {
467+
console.log(error);
468+
});
469+
```
470+
471+
308472
## Supported web browsers and node.js versions
309473
The client is tested on
310474
- Chrome
311475
- Firefox
312476
- Edge
313477
- Safari 6.1+
314478
- Internet Explorer 10+
315-
- Node.js 4+
479+
- Node.js
316480

317481

318482
## Development

0 commit comments

Comments
 (0)