forked from i-am-alice/3rd-devs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgoliaService.ts
87 lines (72 loc) · 2.62 KB
/
AlgoliaService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { searchClient, type SearchClient, QueryType, RemoveWordsIfNoResults } from '@algolia/client-search';
export class AlgoliaService {
private client: SearchClient;
constructor(applicationId: string, apiKey: string) {
this.client = searchClient(applicationId, apiKey);
}
async searchSingleIndex(indexName: string, query: string, options?: {
headers?: Record<string, string>,
queryParameters?: Record<string, any>
}) {
const defaultParams = {
hitsPerPage: 20,
page: 0,
attributesToRetrieve: ['*'],
typoTolerance: true,
ignorePlurals: true,
removeStopWords: true,
queryType: 'prefixNone' as QueryType,
attributesToHighlight: ['*'],
highlightPreTag: '<em>',
highlightPostTag: '</em>',
analytics: true,
clickAnalytics: true,
enablePersonalization: false,
distinct: 1,
facets: ['*'],
minWordSizefor1Typo: 1,
minWordSizefor2Typos: 3,
advancedSyntax: true,
removeWordsIfNoResults: 'lastWords' as RemoveWordsIfNoResults
};
const mergedParams = {
...defaultParams,
query,
...options?.queryParameters,
getRankingInfo: true
};
return this.client.search([
{
indexName,
params: mergedParams,
},
], { headers: options?.headers });
}
async saveObject(indexName: string, object: Record<string, any>) {
return this.client.saveObject({ indexName, body: object });
}
async getObject(indexName: string, objectID: string, attributesToRetrieve?: string[]) {
return this.client.getObject({ indexName, objectID, attributesToRetrieve });
}
async addOrUpdateObject(indexName: string, objectID: string, object: Record<string, any>) {
return this.client.addOrUpdateObject({ indexName, objectID, body: object });
}
async deleteObject(indexName: string, objectID: string) {
return this.client.deleteObject({ indexName, objectID });
}
async deleteBy(indexName: string, filters: string) {
return this.client.deleteBy({ indexName, deleteByParams: { filters } });
}
async clearObjects(indexName: string) {
return this.client.clearObjects({ indexName });
}
async partialUpdateObject(indexName: string, objectID: string, attributes: Record<string, any>) {
return this.client.partialUpdateObject({ indexName, objectID, attributesToUpdate: attributes });
}
async getObjects(requests: Array<{ indexName: string, objectID: string, attributesToRetrieve?: string[] }>) {
return this.client.getObjects({ requests });
}
async listIndices() {
return this.client.listIndices();
}
}