-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-elasticsearch.js
61 lines (56 loc) · 1.87 KB
/
update-elasticsearch.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
let magentoUtil = require('./utils/magento2');
let client = require('./utils/elasticclient');
const ALL_PRODUCT_URL = '/V1/products';
client.indices.exists({
index: 'ecommerce'
}, (existsError, existsResponse, existsStatus) => {
if (existsResponse === false) {
client.indices.create({
index: 'ecommerce'
}, (createError, createResponse, createStatus) => {
if (createStatus === 200) {
return populateElastic();
}
});
} else {
return populateElastic();
}
});
function populateElastic() {
magentoUtil.magentoGet(ALL_PRODUCT_URL, '?searchCriteria=', (response, err) => {
if (err) {
return console.log('Error: ', err);
}
const rawProducts = response.items;
const allProducts = [];
const finalProducts = [];
rawProducts.forEach(rawProduct => {
let product = {};
product['id'] = rawProduct['id'];
product['sku'] = rawProduct['sku'];
product['name'] = rawProduct['name'];
product['price'] = rawProduct['price'];
if (rawProduct['custom_attributes']) {
const customAttributes = rawProduct['custom_attributes'];
customAttributes.forEach(customAttribute => {
if (customAttribute['attribute_code'] === 'image')
product['image'] = customAttribute['value'];
if (customAttribute['attribute_code'] === 'description')
product['description'] = customAttribute['value'];
});
}
allProducts.push(product);
});
allProducts.forEach((product, index) => {
finalProducts.push({ index: { _index: 'ecommerce', _type: 'products', _id: index }});
finalProducts.push(product);
});
client.bulk({body: finalProducts}, (err, resp) => {
if (err) {
return console.log('Elastic Search Error: ', err);
} else {
return console.log('Elastic Search Response: ', resp);
}
});
});
}