A fast and simple JavaScript client library for searchHubs smartQuery and smartSuggest modules
npm i -S searchhub-js-client
Checkout the examples folder for a backend and frontend integration demo
recommended browser example
import {BrowserClientFactory} from "searchhub-js-client";
const {smartSuggestClient, smartQueryClient, abTestManager} = BrowserClientFactory({
tenant: "{YOUR}.{TENANT}",
abTestActive: false
});
if (abTestManager.isSearchhubActive()) {
// use our client
const suggestions = await smartSuggestClient.getSuggestions("jeannss");
} else {
// use your own as you type suggest endpoint
}
// automatically respects ab test assignment + caching
const mapping = await smartQueryClient.getMapping("jeanss");
Recommended backend example (expressJs)
const express = require('express')
const cookieParser = require('cookie-parser');
const {ExpressJsClientFactory, ExpressCookieAccess} = require("searchhub-js-client");
const path = require('path');
const port = 3000;
const app = express();
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
const tenant = "{YOUR}.{TENANT}";
const abTestActive = false;
app.get('/smartquery', (req, res) => {
/**
* For the AB test to work we need to create a client per request.
*/
const {smartQueryClient} = ExpressJsClientFactory({
tenant,
abTestActive,
cookieAccess: new ExpressCookieAccess(req, res)
});
smartQueryClient.getMapping(req.query.userQuery)
.then((result) => {
res.send(result);
});
});