Skip to content

Instantly share code, notes, and snippets.

@megancooper
Created August 19, 2021 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megancooper/bf95a6bfe45752b0f1e4b21937ca9e5e to your computer and use it in GitHub Desktop.
Save megancooper/bf95a6bfe45752b0f1e4b21937ca9e5e to your computer and use it in GitHub Desktop.
'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