@lableb/javascript-sdk
Version:
Lableb cloud search client for javascript
168 lines (123 loc) • 5.04 kB
Markdown
# Lableb's Client | Search Feedback 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 access the search feedback functions at `lablebClient.feedback.search`, where you have two types of feedbacks functions: `single` and `batch`
Use `lablebClient.feedback.search.single` to send one feedback at a time to Lableb, and use `lablebClient.feedback.search.batch` to send many feedbacks at the same time to Lableb.
# Options
## Single Feedback
| field | type | description |
| ------------------ | ------ | ----------- |
| documentFeedback* | object | feedback object(see below for details) |
| platformName | string | your platform name in small-letters |
| indexName | string | the used index name for the search function |
| searchHandler | string | the used search handler for the search function |
| APIKey | string | your search API Key copied from [Lableb Dashboard](https://dashboard.lableb.com) |
## Content of the feedback object: `documentFeedback`
| field | type | description |
| --------- | ------ | ----------- |
| query* | string | query used in the search function |
| itemId | string | document"s id clicked on or chosen by the user |
| itemOrder | string | document"s order clicked on or chosen by the user |
| url | string | document"s url clicked on or chosen by the user |
| sessionId | string | uniques session id of the end user |
Example
```js
await lablebClient.feedback.search.single({
documentFeedback: {
query: "water",
itemId: "37",
itemOrder: "2",
sessionId: "3mn6baLA",
url: "https://example.com/items/37"
}
});
```
## Batch Feedback
Its options basically the same as `single` feedback options except using `documentsFeedbacks` as an array instead of `documentFeedback` as an object.
| field | type | description |
| ------------------- | ------ | ----------- |
| documentsFeedbacks* | array | array of feedback object(see below for details) |
| platformName | string | your platform name in small-letters |
| indexName | string | the used index name for the search function |
| searchHandler | string | the used search handler for the search function |
| APIKey | string | your search API Key copied from [Lableb Dashboard](https://dashboard.lableb.com) |
Example
```js
await lablebClient.feedback.search.batch({
documentsFeedbacks: [
{
query: "water",
itemId: "37",
itemOrder: "2",
sessionId: "3mn6baL",
url: "https://example.com/items/37"
},
{
query: "food",
itemId: "421",
itemOrder: "8",
sessionId: "vA2ml2a",
url: "https://example.com/items/421"
},
{
query: "cold",
itemId: "687",
itemOrder: "5",
sessionId: "gavz@1m",
url: "https://example.com/items/687"
}
]
});
```
----------------------
#### Alternative syntax for async code
```js
let lablebClient = LablebClient({
APIKey: process.env.API_KEY,
platformName: process.env.PLATFORM_NAME,
})
lablebClient.feedback.search.single({
documentFeedback: {
query: "water",
itemId: "37",
itemOrder: "2",
sessionId: "3mn6baLA",
url: "https://example.com/items/37"
}
})
.then(() => {
console.log("Search feedback has been sent.");
})
.catch(console.error);
```
------------------------
#### Real Usage Example
```js
const { code, response, time } = await lablebClient.search(options);
const searchResults = response.results;
```
Search results is an array of objects representing your platform documents(data), each document automatically has a `feedback` object in it that contains everything you need to send within the feedback request.
```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 to be sent with the search feedback request
```js
const { code, response, time } = await lablebClient.search(options);
const searchResults = response.results;
// later on, the end user clicks on the first result
const userInteraction = searchResults[0];
await lablebClient.feedback.search.single({
documentFeedback: userInteraction.feedback
});
```