@lableb/javascript-sdk
Version:
Lableb cloud search client for javascript
229 lines (150 loc) • 6.18 kB
Markdown
# Lableb's Client | Autocomplete API
After creating a new instance of Lableb's Client,
```js
const lablebClient = LablebClient({
APIKey: process.env.API_KEY,
platformName: process.env.PLATFORM_NAME,
});
```
You can use the autocomplete function as
```js
const { code, response, time } = await lablebClient.autocomplete(options);
const autocompleteResults = response.results;
```
----------------------
## Options
All options has types validation built in for you, in case you mis-typed something, And if you're using [typescript](https://github.com/microsoft/TypeScript) the code editor will tell you for any mis-typed argument instantly.
Options are of three categories:
- autocomplete specific
- globally shared, which can be used to override any global option for this autocomplete function call
- user related
----------------------
### autocomplete specific options
| field | type | description |
| ------- | ------ | ----------- |
| query* | string | usually your end users query |
| sort | string | sort your results by specific field and specific order |
| limit | number | limit your autocomplete results to some number |
- Sort syntax
You type the field name followed by the `asc` or `desc` to sort data by that field in an ascending or descending order respectively.
Example:
```js
await lablebClient.autocomplete({
sort: "date desc"
});
```
----------------------
### Globally shared options
Passing these options to the autocomplete function will override any globally passed options to the main `LablebClient` of the same name.
| field | type | description |
| ------------- | ------ | ----------- |
| platformName | string | your platform name in small-letters |
| indexName | string | the used index name for the autocomplete function |
| autocompleteHandler | string | the used autocomplete handler for the autocomplete function |
| APIKey | string | your autocomplete API Key copied from [Lableb Dashboard](https://dashboard.lableb.com) |
----------------------
### User related options
Useful to track your users behavior later on, because knowing your users can help you better develop your platform/product.
| field | type | description |
| ------------- | ------ | ----------- |
| userId | string | end user id |
| userIp | string | end user IP address |
| userCountry | string | end user country code(ISO-3166) |
| sessionId | string | uniques session Id for your end user |
| sessionIdGenerator | function | pass callback that generate unique session id string |
----------------------
#### Alternative syntax for async code
```js
let lablebClient = LablebClient({
APIKey: process.env.API_KEY,
platformName: process.env.PLATFORM_NAME,
})
lablebClient.autocomplete(options)
.then(autocompleteResponse => {
const { code, response, time } = autocompleteResponse;
const autocompleteResults = response.results;
})
.catch(console.error);;
```
----------------------
### Autocomplete Response
```js
const autocompleteResponse = await lablebClient.autocomplete(options);
```
| field | type | description |
| ---------------------------------- | ------ | ----------- |
| time | number | time spent for the request in milliseconds |
| code | number | http response code |
| [response](#autocomplete-results) | object | [autocomplete results](#autocomplete-results) |
### autocomplete Results
| field | type | description |
| ----------------------------- | ------ | ----------- |
| found_documents | number | documents count found for the given autocomplete request |
| [results](#results-example) | array | array of documents each has at least an `id` and a `feedback` object |
| [facets](#facets-example) | object | contains filters keywords that can be used to further filters the returned documents |
---------------------
#### Autocomplete Response Examples
##### Results Example
```json
[
{ id: "8112-cmb", feedback: { query: "water", userCountry: "AE", ... }, ...fields },
{ id: "9142-pam", feedback: { query: "water", userCountry: "AE", ... }, ...fields },
{ id: "2712-alq", feedback: { query: "water", userCountry: "AE", ... }, ...fields },
{ id: "2522-sgm", feedback: { query: "water", userCountry: "AE", ... }, ...fields },
{ id: "8156-baj", feedback: { query: "water", userCountry: "AE", ... }, ...fields }
]
```
Now you can use the `feedback` object returned for the future [Autocomplete Feedback](https://gitlab.com/lableb-cse-sdks/javascript-sdk/-/blob/master/documentation/autocomplete-feedback.md) request
```js
const { code, response, time } = await lablebClient.autocomplete(options);
const autocompleteResults = response.results;
// later on, the end user clicks on the first result
const userInteraction = autocompleteResults[0];
await lablebClient.feedback.autocomplete.single({
documentFeedback: userInteraction.feedback
});
```
##### Facets Example
For details on each property check [Facets Anatomy](https://gitlab.com/lableb-cse-sdks/javascript-sdk/-/blob/master/documentation/facets.md)
```json
{
"count": 3,
"language": {
"buckets": [
{
"value": "ar",
"count": 199
},
{
"value": "en",
"count": 199
}
]
},
"categories": {
"buckets": [
{
"value": "Accessories",
"count": 37
},
{
"value": "Hats ",
"count": 252
},
...
]
},
"status": {
"buckets": [
{
"value": "Out Of Stock",
"count": 194
},
{
"value": "In Stock",
"count": 5
}
]
}
}
```