forked from beastsaber/bsaber
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-indices.js
42 lines (37 loc) · 1001 Bytes
/
create-indices.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
import dotenv from 'dotenv'
if (process.env.NODE_ENV !== 'production') {
dotenv.config()
}
import { Client } from '@opensearch-project/opensearch'
import yaml from 'js-yaml'
import fs from 'fs'
let client = new Client({
node: process.env.OPENSEARCH_URL || 'http://localhost:9200',
})
async function createIndices() {
try {
await client.indices.delete({ index: 'posts' })
} catch (error) {
console.log(error)
}
await client.indices.create({
index: 'posts',
})
let fileNames = fs.readdirSync('./src/routes/posts')
for (let fileName of fileNames) {
let data = fs.readFileSync(`./src/routes/posts/${fileName}`, 'utf8')
let slug = fileName.replace('.md', '')
let metadata = yaml.load(data.split('---')[1])
let body = data.split('---')[2].trim()
await client.index({
index: 'posts',
body: {
slug: slug,
title: metadata.title,
text: body,
},
refresh: true,
})
}
}
createIndices().catch(console.log)