Skip to content

Commit be0768d

Browse files
authored
Merge pull request #93 from AddSearch/sc-12720/support-filter-parameter-in-ai-answers-endpoint
[sc-12720] add "aiAnswersFilterObject" to settings and a method to se…
2 parents f5f04df + d487cdc commit be0768d

File tree

6 files changed

+61
-32
lines changed

6 files changed

+61
-32
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,24 @@ callbackFn = function (response) {
532532
client.putSentimentClick('conversation_id', 'sentiment_value');
533533
```
534534

535+
536+
#### Set AI-answers filtering object
537+
538+
Set complex filtering object that can contain nested _and_, _or_, _not_.
539+
Key filterable properties include: _category_, _custom_fields.<your_field_name>_, _language_, _doc_date_
540+
541+
```js
542+
// Find results where region is en-us, color is not white
543+
var aiAnswersFilter = {
544+
and: [
545+
{ 'custom_fields.region': 'en-us' },
546+
{ not: { 'custom_fields.color': 'white' } }
547+
]
548+
};
549+
550+
client.setAiAnswersFilterObject(aiAnswersFilter);
551+
```
552+
535553
## POST API
536554
:exclamation: POST API is not fully supported. If you need to use some methods in the library, please contact our support.
537555

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "addsearch-js-client",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
44
"description": "AddSearch API JavaScript client",
55
"repository": {
66
"type": "git",

src/apifetch.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export interface ApiFetchCallback<T = any> {
5252

5353
interface SourceDocuments {
5454
page: number;
55-
hits: Document[];
55+
hits: SearchResponseDocument[];
5656
total_hits: number;
5757
}
5858

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

87+
/**
88+
* Helper function to convert a setting to a query parameter string
89+
*/
90+
/* eslint-disable @typescript-eslint/no-explicit-any */
91+
const settingToQueryParam = function (setting: any, key: string): string {
92+
if (setting !== null && setting !== undefined && setting !== '') {
93+
return '&' + key + '=' + setting;
94+
}
95+
return '';
96+
};
97+
/* eslint-enable @typescript-eslint/no-explicit-any */
98+
8799
/**
88100
* Fetch search results of search suggestions from the Addsearch API
89101
*/
@@ -97,15 +109,6 @@ const executeApiFetch: ExecuteApiFetch = function (
97109
customFilterObject,
98110
recommendOptions
99111
) {
100-
/* eslint-disable @typescript-eslint/no-explicit-any */
101-
const settingToQueryParam = function (setting: any, key: string) {
102-
if (setting || setting === false) {
103-
return '&' + key + '=' + setting;
104-
}
105-
return '';
106-
};
107-
/* eslint-enable @typescript-eslint/no-explicit-any */
108-
109112
// Validate query type
110113
if (
111114
type !== 'search' &&
@@ -138,23 +141,19 @@ const executeApiFetch: ExecuteApiFetch = function (
138141

139142
// Boolean operators (AND, OR, NOT) uppercase
140143
keyword = settings?.enableLogicalOperators
141-
? keyword.replace(/ and /g, ' AND ').replace(/ or /g, ' OR ').replace(/ not /g, ' NOT ')
142-
: keyword.replace(/ AND /g, ' and ').replace(/ OR /g, ' or ').replace(/ NOT /g, ' not ');
144+
? keyword.replaceAll(' and ', ' AND ').replaceAll(' or ', ' OR ').replaceAll(' not ', ' NOT ')
145+
: keyword
146+
.replaceAll(' AND ', ' and ')
147+
.replaceAll(' OR ', ' or ')
148+
.replaceAll(' NOT ', ' not ');
143149

144150
// Escape
145151
keyword = encodeURIComponent(keyword);
146152

147153
// Fuzzy
148154
let fuzzy = settings?.fuzzy;
149155
if (fuzzy === 'retry') {
150-
// First call, non fuzzy
151-
if (fuzzyRetry !== true) {
152-
fuzzy = false;
153-
}
154-
// Second call, fuzzy
155-
else {
156-
fuzzy = true;
157-
}
156+
fuzzy = fuzzyRetry === true; // true on retry (second call), false on initial call
158157
}
159158

160159
// GET Parameters
@@ -188,8 +187,12 @@ const executeApiFetch: ExecuteApiFetch = function (
188187
collectAnalytics: settings?.collectAnalytics,
189188
postfixWildcard: settings?.postfixWildcard,
190189
categories: settings?.categories ? settings?.categories.split(',') : undefined,
191-
priceFromCents: settings?.priceFromCents ? parseInt(settings?.priceFromCents, 10) : undefined,
192-
priceToCents: settings?.priceToCents ? parseInt(settings?.priceToCents, 10) : undefined,
190+
priceFromCents: settings?.priceFromCents
191+
? Number.parseInt(settings?.priceFromCents, 10)
192+
: undefined,
193+
priceToCents: settings?.priceToCents
194+
? Number.parseInt(settings?.priceToCents, 10)
195+
: undefined,
193196
dateFrom: settings?.dateFrom,
194197
dateTo: settings?.dateTo,
195198
paging: {
@@ -210,15 +213,13 @@ const executeApiFetch: ExecuteApiFetch = function (
210213

211214
// Add sortBy and sortOrder
212215
if (Array.isArray(settings?.paging.sortBy) && settings?.paging.sortBy.length > 1) {
213-
settings?.paging.sortBy.forEach(function (value, index) {
214-
queryParamsString =
215-
queryParamsString +
216+
for (const [index, value] of settings.paging.sortBy.entries()) {
217+
queryParamsString +=
216218
settingToQueryParam(value, 'sort') +
217-
settingToQueryParam(settings?.paging.sortOrder[index], 'order');
218-
});
219+
settingToQueryParam(settings.paging.sortOrder[index], 'order');
220+
}
219221
} else {
220-
queryParamsString =
221-
queryParamsString +
222+
queryParamsString +=
222223
settingToQueryParam(settings?.paging.sortBy, 'sort') +
223224
settingToQueryParam(settings?.paging.sortOrder, 'order');
224225
}
@@ -339,10 +340,10 @@ const executeApiFetch: ExecuteApiFetch = function (
339340

340341
// Ai Answers
341342
else if (type === 'ai-answers') {
342-
// TODO use apiHostname instead of hardcoded URL
343343
apiInstance
344344
.post(`https://${apiHostname}/v2/indices/${sitekey}/conversations`, {
345-
question: settings?.keyword
345+
question: settings?.keyword,
346+
filter: settings?.aiAnswersFilterObject
346347
})
347348
.then(function (response: AxiosResponse<ConversationsApiResponse>) {
348349
if (response.data.response) {

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ class AddSearchClient {
384384
this.settings.setFilterObject(filter);
385385
}
386386

387+
setAiAnswersFilterObject(filter: object): void {
388+
this.settings.setAiAnswersFilterObject(filter);
389+
}
390+
387391
setShuffleAndLimitTo(shuffleAndLimitTo: number): void {
388392
this.settings.setShuffleAndLimitTo(shuffleAndLimitTo);
389393
}

src/settings.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type Settings = {
5454
analyticsTag?: string;
5555
categories?: string;
5656
filterObject?: object;
57+
aiAnswersFilterObject?: object;
5758
priceFromCents?: string;
5859
priceToCents?: string;
5960
dateFrom?: string;
@@ -189,6 +190,10 @@ class SettingsManager {
189190
this.settings.filterObject = filter;
190191
}
191192

193+
setAiAnswersFilterObject(filter: object): void {
194+
this.settings.aiAnswersFilterObject = filter;
195+
}
196+
192197
setPriceRangeFilter(minCents: string, maxCents: string): void {
193198
this.settings.priceFromCents = minCents;
194199
this.settings.priceToCents = maxCents;

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "ES6",
4+
"lib": ["ES6", "ES2021.String", "DOM"],
45
"module": "CommonJS",
56
"outDir": "./dist",
67
"declaration": true,

0 commit comments

Comments
 (0)