This repository has been archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
77 lines (64 loc) · 1.87 KB
/
index.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
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
const algoliasearch = require('algoliasearch');
const fetch = require('node-fetch');
const chunk = require('lodash/chunk');
require('dotenv').config();
const appId = process.env.REACT_APP_ALGOLIA_APP_ID;
const apiKey = process.env.ALGOLIA_ADMIN_API_KEY;
const indexName = process.env.REACT_APP_ALGOLIA_INDEX_NAME;
async function main() {
const client = algoliasearch(appId, apiKey);
const index = client.initIndex(indexName);
const source =
'https://cdn.rawgit.com/algolia/datasets/master/ecommerce/bestbuy_seo.json';
const data = await fetch(source).then(res => res.json());
console.log('data fetched', data.length);
const chunks = chunk(data);
await Promise.all(chunks.map(chunk => index.saveObjects(chunk)));
console.log('data indexed');
await index.setSettings({
searchableAttributes: [
'name',
'shortDescription',
'manufacturer',
'categories',
],
attributesForFaceting: ['categories', 'type', 'manufacturer', 'salePrice'],
customRanking: ['desc(bestSellingRank)'],
});
console.log('basic settings set');
const dynamicFacets = [
{
query: 'iphone',
facets: [
{ attribute: 'categories', widgetName: 'RefinementList' },
{ attribute: 'type', widgetName: 'Menu' },
],
objectID: 'dynamic_query_iphone',
},
{
query: 'smartphone',
facets: [
{ attribute: 'manufacturer', widgetName: 'Menu' },
{ attribute: 'salePrice', widgetName: 'RangeInput' },
],
objectID: 'dynamic_query_smartphone',
},
];
await index.batchRules(
dynamicFacets.map(({ objectID, query, facets }) => ({
condition: {
pattern: query,
anchoring: 'contains',
},
consequence: {
userData: {
type: 'dynamic_facets',
facets,
},
},
objectID,
}))
);
console.log('rules added');
}
main();