AddSearch Search API Client for JavaScript
AddSearch is a hosted search platform for all your web content. This API
Client lets you easily use the AddSearch Search API
from your JavaScript code on web browsers or with Node.js.
Quick Start
The library is available on the global CDN jsDelivr:
<script src="https://cdn.jsdelivr.net/npm/addsearch-js-client@0.1/dist/addsearch-js-client.min.js"></script>
To install the library locally or to use it with Node.js:
npm install addsearch-js-client --save
After installation, add the library to your JS code
var AddSearchClient = require('addsearch-js-client');
Or use import in ES6
import AddSearchClient from 'addsearch-js-client';
Execute the first search query
var client = new AddSearchClient('YOUR PUBLIC SITEKEY');
var cb = function(res) {
console.log(res);
}
client.search('keyword', cb);
Publicly accessible functions
The client provides the following functions.
Fetch search results
client.search('keyword', callback);
client.search(callback);
client.search();
Fetch search suggestions
client.suggestions('a', callback);
Number of search suggestions
client.setSuggestionsSize(20);
Search with fuzzy matching
client.setFuzzyMatch(false);
Set JSON Web Token (for authentication)
client.setJWT(token);
Set user token (for personalized search results)
client.setUserToken('uuid');
Send personalization events with search query
In personalized search, user events are typically sent to AddSearch via API and a user token
is passed with the search query (see setUserToken function).
An alternative way is to send user events needed for personalization with the search query.
var events = [
{favorite_genre: 'rock'},
{favorite_band: 'Red Hot Chili Peppers'},
{least_favorite_genre: 'country'}
];
client.setPersonalizationEvents(events);
Define language filter
client.setLanguage('en');
Define publishing date filter
client.setDateFilter('2019-01-01', '2019-01-31');
Define price range filter
client.setPriceRangeFilter('10000', '20000');
Define category filters
Filter by URL patterns, document types or addsearch-category meta tag values.
See the full documentation.
client.setCategoryFilters('doctype_pdf,products');
Custom field filters
Filter by custom fields. Custon fields can be defined in meta tags or AddSearch crawler can pick them up from your HTML or JSON data.
See the full documentation.
client.addCustomFieldFilter('city','berlin');
client.addCustomFieldFilter('city','paris');
client.addCustomFieldFilter('city','boston');
client.removeCustomFieldFilter('city','paris');
client.removeCustomFieldFilter('city');
Manage paging
Set page number, page size and sorting parameters. It's possible to order results by:
- relevance (descending)
- date (ascending or descending)
- custom field value (ascending or descending. E.g. custom_fields.price)
client.setPaging(page, pageSize, sortBy, sortOrder);
Other functions.
client.nextPage();
client.previousPage();
Set result type
client.setResultType('organic');
Facets
client.addFacetField('category');
client.addFacetField('custom_fields.genre');
By default, 10 facets with most hits are returned per field.
Use the following function to get more or less facets.
client.setNumberOfFacets(20);
Supported web browsers and node.js versions
The client is tested on
- Chrome
- Firefox
- Edge
- Safari 6.1+
- Internet Explorer 10+
- Node.js 4+
Development
To modify this client library, clone this repository to your computer and execute following commands.
Install dependencies
npm install
Run tests
npm test
Build
npm run build
Built bundle is saved under the dist/ folder
Support
Feel free to send any questions, ideas and suggestions at support@addsearch.com.