Skip to content

Commit a6bd72b

Browse files
committed
feat(javascript): guides
1 parent cb02bc1 commit a6bd72b

14 files changed

+276
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{{> snippets/import}}
2+
import type { MultipleBatchRequest } from 'algoliasearch';
3+
4+
try {
5+
// You need an API key with `deleteIndex`
6+
{{> snippets/init}}
7+
8+
// List all indices
9+
const indices = {{#dynamicSnippet}}listIndicesSimple{{/dynamicSnippet}};
10+
11+
// Primary indices don't have a `primary` key
12+
const primaryIndices = indices.items.filter(item => item.primary == null);
13+
const replicaIndices = indices.items.filter(item => item.primary != null);
14+
15+
// Delete primary indices first
16+
if (primaryIndices.length > 0) {
17+
const requests: MultipleBatchRequest[] = primaryIndices.map(index => ({
18+
action: 'delete',
19+
indexName: index.name
20+
}));
21+
{{#dynamicSnippet}}deleteMultipleIndicesPrimary{{/dynamicSnippet}};
22+
console.log("Deleted primary indices.");
23+
}
24+
25+
// Now, delete replica indices
26+
if (replicaIndices.length > 0) {
27+
const requests: MultipleBatchRequest[] = replicaIndices.map(index => ({
28+
action: 'delete',
29+
indexName: index.name
30+
}));
31+
{{#dynamicSnippet}}deleteMultipleIndicesReplica{{/dynamicSnippet}};
32+
console.log("Deleted replica indices.");
33+
}
34+
} catch (e) {
35+
console.error(e);
36+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{> snippets/import}}
2+
3+
try {
4+
{{> snippets/init}}
5+
6+
const { default: records } = await import('./actors.json');
7+
8+
const chunkSize = 10000
9+
10+
for (let beginIndex = 0; beginIndex < records.length; beginIndex += chunkSize) {
11+
const chunk = records.slice(beginIndex, beginIndex + chunkSize);
12+
{{#dynamicSnippet}}saveObjectsChunks{{/dynamicSnippet}}
13+
}
14+
} catch (e) {
15+
console.error(e);
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{> snippets/import}}
2+
3+
const getAllAppIDConfigurations = (): Record<string, string>[] => {
4+
return [/* A list of your MCM AppID/ApiKey pairs */]
5+
}
6+
7+
const playlists: Record<string, any>[] = [ /* Your records */ ]
8+
9+
// Fetch from your own data storage and with your own code
10+
// the list of application IDs and API keys to target each cluster
11+
const configurations = getAllAppIDConfigurations();
12+
13+
// Send the records to each cluster
14+
Object.keys(configurations).forEach(async (appID) => {
15+
try {
16+
const client = algoliasearch(appID, configurations[appID]);
17+
18+
{{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}}
19+
} catch (e) {
20+
console.error(e);
21+
}
22+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{> snippets/import}}
2+
3+
try {
4+
{{> snippets/init}}
5+
6+
const { default: products } = await import('./actors.json');
7+
8+
const records = products.map((product) => {
9+
const reference = product["product_reference"];
10+
const suffixes: string[] = []
11+
12+
for (let i = reference.length; i > 1; i--) {
13+
suffixes.unshift(reference.slice(i));
14+
}
15+
return { ...product, product_reference_suffixes: suffixes };
16+
});
17+
18+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
19+
} catch (e) {
20+
console.error(e);
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{> snippets/import}}
2+
3+
const playlists: Record<string, any>[] = [] // Your records
4+
5+
try {
6+
{{> snippets/init}}
7+
8+
{{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}}
9+
} catch (e) {
10+
console.error(e);
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{> snippets/import}}
2+
3+
try {
4+
{{> snippets/init}}
5+
6+
const records: Record<string, any> = [];
7+
8+
await client.browseObjects<Record<string, any>>({ indexName: "<YOUR_INDEX_NAME>", browseParams: undefined, aggregator: (res) =>
9+
records.push(
10+
res.hits.map((record) => ({
11+
...record,
12+
isPopular: record.nbFollowers >= 1_000_000,
13+
}))
14+
)
15+
});
16+
17+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
18+
} catch (e) {
19+
console.error(e);
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{> snippets/import}}
2+
import type { SearchParams } from 'algoliasearch';
3+
4+
try {
5+
{{> snippets/init}}
6+
7+
const dateTimestamp = Date.now();
8+
const searchParams: SearchParams = {
9+
query: "<YOUR_SEARCH_QUERY>",
10+
filters: `date_timestamp > ${dateTimestamp}`
11+
};
12+
13+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
14+
} catch (e) {
15+
console.error(e);
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{{> snippets/import}}
2+
import type { SearchParams } from 'algoliasearch';
3+
4+
const getGoogleAnalyticsUserIdFromBrowserCookie = (_: string) => {
5+
return ""; // Implement your logic here
6+
}
7+
8+
try {
9+
{{> snippets/init}}
10+
11+
const userToken = getGoogleAnalyticsUserIdFromBrowserCookie('_ga');
12+
let searchParams: SearchParams = {
13+
query: "<YOUR_SEARCH_QUERY>",
14+
userToken
15+
};
16+
17+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
18+
19+
const loggedInUser: string | undefined = undefined;
20+
searchParams.userToken = loggedInUser ?? userToken;
21+
22+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
23+
} catch (e) {
24+
console.error(e);
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{> snippets/import}}
2+
import type { OptionalFilters, SearchParams } from 'algoliasearch';
3+
4+
const labels: string[] = [] // A list of labels
5+
6+
const reduceLabelsToFilters = (_labels: string[]): OptionalFilters => {
7+
return []; // Implement your logic here
8+
}
9+
10+
try {
11+
{{> snippets/init}}
12+
13+
const optionalFilters = reduceLabelsToFilters(labels)
14+
const searchParams: SearchParams = {
15+
query: "<YOUR_SEARCH_QUERY>",
16+
optionalFilters: optionalFilters
17+
};
18+
19+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
20+
} catch (e) {
21+
console.error(e);
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{> snippets/import}}
2+
import type { SearchParams } from 'algoliasearch';
3+
4+
const getBuyerAccountId = () => {
5+
return ""; // Implement your logic here
6+
}
7+
8+
try {
9+
{{> snippets/init}}
10+
11+
// get the buyer account information
12+
const buyer = getBuyerAccountId();
13+
const searchParams: SearchParams = {
14+
query: "<YOUR_SEARCH_QUERY>",
15+
ruleContexts: [buyer]
16+
};
17+
18+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
19+
} catch (e) {
20+
console.error(e);
21+
}

0 commit comments

Comments
 (0)