Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,24 @@ callbackFn = function (response) {
client.putSentimentClick('conversation_id', 'sentiment_value');
```


#### Set AI-answers filtering object

Set complex filtering object that can contain nested _and_, _or_, _not_.
Key filterable properties include: _category_, _custom_fields.<your_field_name>_, _language_, _doc_date_

```js
// Find results where region is en-us, color is not white
var aiAnswersFilter = {
and: [
{ 'custom_fields.region': 'en-us' },
{ not: { 'custom_fields.color': 'white' } }
]
};

client.setAiAnswersFilterObject(aiAnswersFilter);
```

## POST API
:exclamation: POST API is not fully supported. If you need to use some methods in the library, please contact our support.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "addsearch-js-client",
"version": "1.1.3",
"version": "1.1.4",
"description": "AddSearch API JavaScript client",
"repository": {
"type": "git",
Expand Down
63 changes: 32 additions & 31 deletions src/apifetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface ApiFetchCallback<T = any> {

interface SourceDocuments {
page: number;
hits: Document[];
hits: SearchResponseDocument[];
total_hits: number;
}

Expand Down Expand Up @@ -84,6 +84,18 @@ export type ExecuteApiFetch = (
) => void;
/* eslint-enable @typescript-eslint/no-explicit-any */

/**
* Helper function to convert a setting to a query parameter string
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
const settingToQueryParam = function (setting: any, key: string): string {
if (setting !== null && setting !== undefined && setting !== '') {
return '&' + key + '=' + setting;
}
return '';
};
/* eslint-enable @typescript-eslint/no-explicit-any */

/**
* Fetch search results of search suggestions from the Addsearch API
*/
Expand All @@ -97,15 +109,6 @@ const executeApiFetch: ExecuteApiFetch = function (
customFilterObject,
recommendOptions
) {
/* eslint-disable @typescript-eslint/no-explicit-any */
const settingToQueryParam = function (setting: any, key: string) {
if (setting || setting === false) {
return '&' + key + '=' + setting;
}
return '';
};
/* eslint-enable @typescript-eslint/no-explicit-any */

// Validate query type
if (
type !== 'search' &&
Expand Down Expand Up @@ -138,23 +141,19 @@ const executeApiFetch: ExecuteApiFetch = function (

// Boolean operators (AND, OR, NOT) uppercase
keyword = settings?.enableLogicalOperators
? keyword.replace(/ and /g, ' AND ').replace(/ or /g, ' OR ').replace(/ not /g, ' NOT ')
: keyword.replace(/ AND /g, ' and ').replace(/ OR /g, ' or ').replace(/ NOT /g, ' not ');
? keyword.replaceAll(' and ', ' AND ').replaceAll(' or ', ' OR ').replaceAll(' not ', ' NOT ')
: keyword
.replaceAll(' AND ', ' and ')
.replaceAll(' OR ', ' or ')
.replaceAll(' NOT ', ' not ');

// Escape
keyword = encodeURIComponent(keyword);

// Fuzzy
let fuzzy = settings?.fuzzy;
if (fuzzy === 'retry') {
// First call, non fuzzy
if (fuzzyRetry !== true) {
fuzzy = false;
}
// Second call, fuzzy
else {
fuzzy = true;
}
fuzzy = fuzzyRetry === true; // true on retry (second call), false on initial call
}

// GET Parameters
Expand Down Expand Up @@ -188,8 +187,12 @@ const executeApiFetch: ExecuteApiFetch = function (
collectAnalytics: settings?.collectAnalytics,
postfixWildcard: settings?.postfixWildcard,
categories: settings?.categories ? settings?.categories.split(',') : undefined,
priceFromCents: settings?.priceFromCents ? parseInt(settings?.priceFromCents, 10) : undefined,
priceToCents: settings?.priceToCents ? parseInt(settings?.priceToCents, 10) : undefined,
priceFromCents: settings?.priceFromCents
? Number.parseInt(settings?.priceFromCents, 10)
: undefined,
priceToCents: settings?.priceToCents
? Number.parseInt(settings?.priceToCents, 10)
: undefined,
dateFrom: settings?.dateFrom,
dateTo: settings?.dateTo,
paging: {
Expand All @@ -210,15 +213,13 @@ const executeApiFetch: ExecuteApiFetch = function (

// Add sortBy and sortOrder
if (Array.isArray(settings?.paging.sortBy) && settings?.paging.sortBy.length > 1) {
settings?.paging.sortBy.forEach(function (value, index) {
queryParamsString =
queryParamsString +
for (const [index, value] of settings.paging.sortBy.entries()) {
queryParamsString +=
settingToQueryParam(value, 'sort') +
settingToQueryParam(settings?.paging.sortOrder[index], 'order');
});
settingToQueryParam(settings.paging.sortOrder[index], 'order');
}
} else {
queryParamsString =
queryParamsString +
queryParamsString +=
settingToQueryParam(settings?.paging.sortBy, 'sort') +
settingToQueryParam(settings?.paging.sortOrder, 'order');
}
Expand Down Expand Up @@ -339,10 +340,10 @@ const executeApiFetch: ExecuteApiFetch = function (

// Ai Answers
else if (type === 'ai-answers') {
// TODO use apiHostname instead of hardcoded URL
apiInstance
.post(`https://${apiHostname}/v2/indices/${sitekey}/conversations`, {
question: settings?.keyword
question: settings?.keyword,
filter: settings?.aiAnswersFilterObject
})
.then(function (response: AxiosResponse<ConversationsApiResponse>) {
if (response.data.response) {
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ class AddSearchClient {
this.settings.setFilterObject(filter);
}

setAiAnswersFilterObject(filter: object): void {
this.settings.setAiAnswersFilterObject(filter);
}

setShuffleAndLimitTo(shuffleAndLimitTo: number): void {
this.settings.setShuffleAndLimitTo(shuffleAndLimitTo);
}
Expand Down
5 changes: 5 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type Settings = {
analyticsTag?: string;
categories?: string;
filterObject?: object;
aiAnswersFilterObject?: object;
priceFromCents?: string;
priceToCents?: string;
dateFrom?: string;
Expand Down Expand Up @@ -189,6 +190,10 @@ class SettingsManager {
this.settings.filterObject = filter;
}

setAiAnswersFilterObject(filter: object): void {
this.settings.aiAnswersFilterObject = filter;
}

setPriceRangeFilter(minCents: string, maxCents: string): void {
this.settings.priceFromCents = minCents;
this.settings.priceToCents = maxCents;
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "ES6",
"lib": ["ES6", "ES2021.String", "DOM"],
"module": "CommonJS",
"outDir": "./dist",
"declaration": true,
Expand Down