Skip to content

Commit 0b9c2c0

Browse files
committed
feat(dictionaries): adds methods and tests
1 parent 677e902 commit 0b9c2c0

22 files changed

+592
-0
lines changed

packages/algoliasearch/src/builds/browser.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
browseSynonyms,
4646
ChunkedBatchResponse,
4747
ChunkOptions,
48+
clearDictionaryEntries,
4849
clearObjects,
4950
clearRules,
5051
ClearRulesOptions,
@@ -60,13 +61,16 @@ import {
6061
DeleteApiKeyResponse,
6162
deleteBy,
6263
DeleteByFiltersOptions,
64+
deleteDictionaryEntries,
6365
deleteIndex,
6466
deleteObject,
6567
deleteObjects,
6668
DeleteResponse,
6769
deleteRule,
6870
deleteSynonym,
6971
DeleteSynonymOptions,
72+
DictionaryEntry,
73+
DictionarySettings,
7074
exists,
7175
findAnswers,
7276
FindAnswersOptions,
@@ -76,6 +80,9 @@ import {
7680
FindObjectResponse,
7781
getApiKey,
7882
GetApiKeyResponse,
83+
getDictionarySettings,
84+
GetDictionarySettingsResponse,
85+
getDictionaryTask,
7986
getLogs,
8087
GetLogsResponse,
8188
getObject,
@@ -127,9 +134,13 @@ import {
127134
ReplaceAllObjectsOptions,
128135
replaceAllRules,
129136
replaceAllSynonyms,
137+
replaceDictionaryEntries,
130138
restoreApiKey,
131139
RestoreApiKeyResponse,
132140
Rule,
141+
saveDictionaryEntries,
142+
SaveDictionaryEntriesOptions,
143+
SaveDictionaryEntriesResponse,
133144
saveObject,
134145
SaveObjectResponse,
135146
saveObjects,
@@ -146,6 +157,8 @@ import {
146157
SaveSynonymsResponse,
147158
search,
148159
SearchClient as BaseSearchClient,
160+
searchDictionaryEntries,
161+
SearchDictionaryEntriesResponse,
149162
searchForFacetValues,
150163
SearchForFacetValuesQueryParams,
151164
SearchForFacetValuesResponse,
@@ -160,14 +173,18 @@ import {
160173
searchUserIDs,
161174
SearchUserIDsOptions,
162175
SearchUserIDsResponse,
176+
setDictionarySettings,
177+
SetDictionarySettingsResponse,
163178
setSettings,
164179
SetSettingsResponse,
165180
Settings,
166181
Synonym,
182+
TaskStatusResponse,
167183
updateApiKey,
168184
UpdateApiKeyOptions,
169185
UpdateApiKeyResponse,
170186
UserIDResponse,
187+
waitDictionaryTask,
171188
waitTask,
172189
} from '@algolia/client-search';
173190
import { LogLevelEnum } from '@algolia/logger-common';
@@ -235,6 +252,15 @@ export default function algoliasearch(
235252
getTopUserIDs,
236253
removeUserID,
237254
hasPendingMappings,
255+
clearDictionaryEntries,
256+
deleteDictionaryEntries,
257+
getDictionarySettings,
258+
getDictionaryTask,
259+
replaceDictionaryEntries,
260+
saveDictionaryEntries,
261+
searchDictionaryEntries,
262+
setDictionarySettings,
263+
waitDictionaryTask,
238264
initIndex: base => (indexName: string): SearchIndex => {
239265
return initIndex(base)(indexName, {
240266
methods: {
@@ -601,6 +627,41 @@ export type SearchClient = BaseSearchClient & {
601627
readonly hasPendingMappings: (
602628
requestOptions?: HasPendingMappingsOptions & RequestOptions
603629
) => Readonly<Promise<HasPendingMappingsResponse>>;
630+
readonly clearDictionaryEntries: (
631+
dictionary: string,
632+
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions
633+
) => Readonly<WaitablePromise<SaveDictionaryEntriesResponse>>;
634+
readonly deleteDictionaryEntries: (
635+
dictionary: string,
636+
objectIDs: readonly string[],
637+
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions
638+
) => Readonly<WaitablePromise<SaveDictionaryEntriesResponse>>;
639+
readonly replaceDictionaryEntries: (
640+
dictionary: string,
641+
entries: readonly DictionaryEntry[],
642+
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions
643+
) => Readonly<WaitablePromise<SaveDictionaryEntriesResponse>>;
644+
readonly saveDictionaryEntries: (
645+
dictionary: string,
646+
entries: readonly DictionaryEntry[],
647+
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions
648+
) => Readonly<WaitablePromise<SaveDictionaryEntriesResponse>>;
649+
readonly searchDictionaryEntries: (
650+
dictionary: string,
651+
query: string,
652+
requestOptions?: RequestOptions
653+
) => Readonly<Promise<SearchDictionaryEntriesResponse>>;
654+
readonly getDictionarySettings: (
655+
requestOptions?: RequestOptions
656+
) => Readonly<Promise<GetDictionarySettingsResponse>>;
657+
readonly setDictionarySettings: (
658+
settings: readonly DictionarySettings[],
659+
requestOptions?: RequestOptions
660+
) => Readonly<WaitablePromise<SetDictionarySettingsResponse>>;
661+
readonly getDictionaryTask: (
662+
taskID: number,
663+
requestOptions?: RequestOptions
664+
) => Readonly<Promise<TaskStatusResponse>>;
604665
readonly initAnalytics: (options?: InitAnalyticsOptions) => AnalyticsClient;
605666
readonly initRecommendation: (options?: InitRecommendationOptions) => RecommendationClient;
606667
};

packages/algoliasearch/src/builds/node.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
browseSynonyms,
4545
ChunkedBatchResponse,
4646
ChunkOptions,
47+
clearDictionaryEntries,
4748
clearObjects,
4849
clearRules,
4950
ClearRulesOptions,
@@ -59,13 +60,16 @@ import {
5960
DeleteApiKeyResponse,
6061
deleteBy,
6162
DeleteByFiltersOptions,
63+
deleteDictionaryEntries,
6264
deleteIndex,
6365
deleteObject,
6466
deleteObjects,
6567
DeleteResponse,
6668
deleteRule,
6769
deleteSynonym,
6870
DeleteSynonymOptions,
71+
DictionaryEntry,
72+
DictionarySettings,
6973
exists,
7074
findAnswers,
7175
FindAnswersOptions,
@@ -76,6 +80,9 @@ import {
7680
generateSecuredApiKey,
7781
getApiKey,
7882
GetApiKeyResponse,
83+
getDictionarySettings,
84+
GetDictionarySettingsResponse,
85+
getDictionaryTask,
7986
getLogs,
8087
GetLogsResponse,
8188
getObject,
@@ -128,9 +135,13 @@ import {
128135
ReplaceAllObjectsOptions,
129136
replaceAllRules,
130137
replaceAllSynonyms,
138+
replaceDictionaryEntries,
131139
restoreApiKey,
132140
RestoreApiKeyResponse,
133141
Rule,
142+
saveDictionaryEntries,
143+
SaveDictionaryEntriesOptions,
144+
SaveDictionaryEntriesResponse,
134145
saveObject,
135146
SaveObjectResponse,
136147
saveObjects,
@@ -147,6 +158,8 @@ import {
147158
SaveSynonymsResponse,
148159
search,
149160
SearchClient as BaseSearchClient,
161+
searchDictionaryEntries,
162+
SearchDictionaryEntriesResponse,
150163
searchForFacetValues,
151164
SearchForFacetValuesQueryParams,
152165
SearchForFacetValuesResponse,
@@ -162,14 +175,18 @@ import {
162175
SearchUserIDsOptions,
163176
SearchUserIDsResponse,
164177
SecuredApiKeyRestrictions,
178+
setDictionarySettings,
179+
SetDictionarySettingsResponse,
165180
setSettings,
166181
SetSettingsResponse,
167182
Settings,
168183
Synonym,
184+
TaskStatusResponse,
169185
updateApiKey,
170186
UpdateApiKeyOptions,
171187
UpdateApiKeyResponse,
172188
UserIDResponse,
189+
waitDictionaryTask,
173190
waitTask,
174191
} from '@algolia/client-search';
175192
import { createNullLogger } from '@algolia/logger-common';
@@ -238,6 +255,15 @@ export default function algoliasearch(
238255
generateSecuredApiKey,
239256
getSecuredApiKeyRemainingValidity,
240257
destroy,
258+
clearDictionaryEntries,
259+
deleteDictionaryEntries,
260+
getDictionarySettings,
261+
getDictionaryTask,
262+
replaceDictionaryEntries,
263+
saveDictionaryEntries,
264+
searchDictionaryEntries,
265+
setDictionarySettings,
266+
waitDictionaryTask,
241267
initIndex: base => (indexName: string): SearchIndex => {
242268
return initIndex(base)(indexName, {
243269
methods: {
@@ -609,6 +635,41 @@ export type SearchClient = BaseSearchClient & {
609635
restrictions: SecuredApiKeyRestrictions
610636
) => string;
611637
readonly getSecuredApiKeyRemainingValidity: (securedApiKey: string) => number;
638+
readonly clearDictionaryEntries: (
639+
dictionary: string,
640+
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions
641+
) => Readonly<WaitablePromise<SaveDictionaryEntriesResponse>>;
642+
readonly deleteDictionaryEntries: (
643+
dictionary: string,
644+
objectIDs: readonly string[],
645+
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions
646+
) => Readonly<WaitablePromise<SaveDictionaryEntriesResponse>>;
647+
readonly replaceDictionaryEntries: (
648+
dictionary: string,
649+
entries: readonly DictionaryEntry[],
650+
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions
651+
) => Readonly<WaitablePromise<SaveDictionaryEntriesResponse>>;
652+
readonly saveDictionaryEntries: (
653+
dictionary: string,
654+
entries: readonly DictionaryEntry[],
655+
requestOptions?: RequestOptions & SaveDictionaryEntriesOptions
656+
) => Readonly<WaitablePromise<SaveDictionaryEntriesResponse>>;
657+
readonly searchDictionaryEntries: (
658+
dictionary: string,
659+
query: string,
660+
requestOptions?: RequestOptions
661+
) => Readonly<Promise<SearchDictionaryEntriesResponse>>;
662+
readonly getDictionarySettings: (
663+
requestOptions?: RequestOptions
664+
) => Readonly<Promise<GetDictionarySettingsResponse>>;
665+
readonly setDictionarySettings: (
666+
settings: readonly DictionarySettings[],
667+
requestOptions?: RequestOptions
668+
) => Readonly<WaitablePromise<SetDictionarySettingsResponse>>;
669+
readonly getDictionaryTask: (
670+
taskID: number,
671+
requestOptions?: RequestOptions
672+
) => Readonly<Promise<TaskStatusResponse>>;
612673
readonly initAnalytics: (options?: InitAnalyticsOptions) => AnalyticsClient;
613674
readonly initRecommendation: (options?: InitRecommendationOptions) => RecommendationClient;
614675
} & Destroyable;

packages/client-common/src/__tests__/TestSuite.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ export class TestSuite {
6868
return client;
6969
}
7070

71+
public makeDictionarySearchClient(
72+
appIdEnv: string = 'ALGOLIA_APPLICATION_ID_2',
73+
apiKeyEnv: string = 'ALGOLIA_ADMIN_KEY_2'
74+
) {
75+
return this.makeSearchClient(appIdEnv, apiKeyEnv);
76+
}
77+
7178
public makeRecommendationClient(
7279
appIdEnv: string = 'ALGOLIA_APPLICATION_ID_1',
7380
apiKeyEnv: string = 'ALGOLIA_ADMIN_KEY_1'
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { TestSuite } from '../../../../client-common/src/__tests__/TestSuite';
2+
3+
const testSuite = new TestSuite('dictionary');
4+
5+
test(testSuite.testName, async () => {
6+
const client = testSuite.makeDictionarySearchClient();
7+
8+
// Stopwords
9+
const stopwordEntryId = Math.floor(Math.random() * 10000).toString();
10+
expect(await client.searchDictionaryEntries('stopwords', stopwordEntryId).nbHits).toEqual(0);
11+
12+
const stopwordEntry = {
13+
objectID: stopwordEntryId,
14+
language: 'en',
15+
word: 'down',
16+
};
17+
18+
await client.saveDictionaryEntries('stopwords', [stopwordEntry]);
19+
20+
const stopwords = await client.searchDictionaryEntries('stopwords', stopwordEntryId);
21+
expect(stopwords.nbHits).toEqual(1);
22+
expect(stopwords.hits[0].objectID).toEqual(stopwordEntry.objectID);
23+
expect(stopwords.hits[0].word).toEqual(stopwordEntry.word);
24+
25+
await client.deleteDictionaryEntries('stopwords', [stopwordEntryId]);
26+
expect(await client.searchDictionaryEntries('stopwords', stopwordEntryId).nbHits).toEqual(0);
27+
28+
const oldDictionaryState = await client.searchDictionaryEntries('stopwords', '');
29+
const oldDictionaryEntries = oldDictionaryState.hits.map(hit => {
30+
delete hit.type;
31+
32+
return hit;
33+
});
34+
35+
await client.saveDictionaryEntries('stopwords', [stopwordEntry]);
36+
expect(await client.searchDictionaryEntries('stopwords', stopwordEntryId).nbHits).toEqual(1);
37+
38+
await client.replaceDictionaryEntries('stopwords', oldDictionaryEntries);
39+
expect(await client.searchDictionaryEntries('stopwords', stopwordEntryId).nbHits).toEqual(0);
40+
41+
const stopwordsSettings = {
42+
disableStandardEntries: {
43+
stopwords: {
44+
en: true,
45+
},
46+
},
47+
};
48+
49+
await client.setDictionarySettings(stopwordsSettings);
50+
expect(await client.getDictionarySettings()).toEqual(stopwordsSettings);
51+
52+
// Plurals
53+
const pluralEntryId = Math.floor(Math.random() * 10000).toString();
54+
expect(await client.searchDictionaryEntries('plurals', pluralEntryId).nbHits).toEqual(0);
55+
56+
const pluralEntry = {
57+
objectID: pluralEntryId,
58+
language: 'fr',
59+
words: ['cheval', 'chevaux'],
60+
};
61+
62+
await client.saveDictionaryEntries('plurals', [pluralEntry]);
63+
64+
const plurals = await client.searchDictionaryEntries('plurals', pluralEntryId);
65+
expect(plurals.nbHits).toEqual(1);
66+
expect(plurals.hits[0].objectID).toEqual(pluralEntry.objectID);
67+
expect(plurals.hits[0].words).toEqual(pluralEntry.words);
68+
69+
await client.deleteDictionaryEntries('plurals', [pluralEntryId]);
70+
expect(await client.searchDictionaryEntries('plurals', pluralEntryId).nbHits).toEqual(0);
71+
72+
// Compounds
73+
const compoundEntryId = Math.floor(Math.random() * 10000).toString();
74+
expect(await client.searchDictionaryEntries('plurals', compoundEntryId).nbHits).toEqual(0);
75+
76+
const compoundEntry = {
77+
objectID: compoundEntryId,
78+
language: 'fr',
79+
word: 'kopfschmerztablette',
80+
decomposition: ['kopf', 'schmerz', 'tablette'],
81+
};
82+
83+
await client.saveDictionaryEntries('plurals', [compoundEntry]);
84+
85+
const compounds = await client.searchDictionaryEntries('plurals', compoundEntryId);
86+
expect(compounds.nbHits).toEqual(1);
87+
expect(compounds.hits[0].objectID).toEqual(compoundEntry.objectID);
88+
expect(compounds.hits[0].word).toEqual(compoundEntry.word);
89+
expect(compounds.hits[0].decomposition).toEqual(compoundEntry.decomposition);
90+
91+
await client.deleteDictionaryEntries('plurals', [compoundEntryId]);
92+
expect(await client.searchDictionaryEntries('plurals', compoundEntryId).nbHits).toEqual(0);
93+
});

0 commit comments

Comments
 (0)