@@ -7,7 +7,7 @@ with JavaScript on web browsers or from Node.js.
77## Quick Start
88The 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```
1212Or install the library locally to use it with Node.js:
1313``` sh
@@ -40,9 +40,10 @@ var cb = function(res) {
4040client .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.
305306client .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
309486The 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
0 commit comments