Created
August 19, 2021 21:29
-
-
Save megancooper/bf95a6bfe45752b0f1e4b21937ca9e5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const { Client } = require('@elastic/elasticsearch'); | |
const esClient = new Client({ node: 'http://localhost:9200' }); | |
const productMapping = require('./productMapping'); | |
const categoryMapping = require('./categoryMapping'); | |
const products = require('./products.json'); | |
const categories = require('./categories.json'); | |
const INDEXES = [ | |
{ | |
name : 'products', | |
schema : productMapping, | |
documents: products.map((doc) => doc._source) | |
}, | |
{ | |
name : 'categories', | |
schema : categoryMapping, | |
documents: categories.map((doc) => doc._source) | |
} | |
]; | |
const createIndices = () => Promise.all( | |
INDEXES.map(({name, schema}) => esClient.indices.create({ | |
index: name, | |
body : schema | |
})) | |
).catch((err) => console.warn( | |
'Failed to create local es index\n', | |
err?.meta?.body?.error?.root_cause[0]?.reason | |
)); | |
const attemptIndexDocuments = ({name, documents}) => Promise.all( | |
documents.map((doc) => esClient.index({ | |
index: name, | |
body : doc | |
})) | |
).catch((err) => | |
console.log( | |
`Failed to index ${name} document\n`, | |
err?.meta?.body?.error?.root_cause[0]?.reason | |
) | |
); | |
const seed = async() => { | |
await createIndices(); | |
await INDEXES.forEach(attemptIndexDocuments); | |
}; | |
seed(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment