-
Notifications
You must be signed in to change notification settings - Fork 2
/
algolia.js
48 lines (39 loc) · 1.41 KB
/
algolia.js
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
const algoliasearch = require("algoliasearch");
const config = require("./algolia.config.json");
const client = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_ADMIN_API_KEY);
const index = client.initIndex(process.env.ALGOLIA_INDEX);
module.exports.indexProduct = async (product, categories, id) => {
index.saveObject(
{
objectID: id,
...product,
category_slugs: categories.slugs,
categories: categories.names
}
)
};
module.exports.removerProductFromIndex = async (id) => {
index.deleteObject( id )
};
module.exports.setSettings = async () => {
// set parent settings
let replicas = config.rankings.map((ranking) => `${process.env.ALGOLIA_INDEX}_${ranking.suffix}`);
await index.setSettings({
replicas,
attributesForFaceting: config.attributesForFaceting,
searchableAttributes: config.searchableAttributes,
ranking: config.defaultRanking,
renderingContent: config.renderingContent
})
// update ranking on each of the replicas
for (const replica of replicas) {
let replicaIndex = client.initIndex(replica);
let suffixName = replica.replace(`${process.env.ALGOLIA_INDEX}_`, '')
await replicaIndex.setSettings({
attributesForFaceting: config.attributesForFaceting,
searchableAttributes: config.searchableAttributes,
ranking: config.rankings.find((ranking) => ranking.suffix == suffixName).ranking
})
};
return true;
};