asyncfunctionreadAndInsertBooks() {
try{
// Clear previous ES index
awaitesConnection.resetIndex()
// Read books directory
letfiles = fs.readdirSync( './books').filter( file=>file.slice( -4) === '.txt')
console.log( `Found ${files.length}Files`)
// Read each book file, and index each paragraph in elasticsearch
for( letfile offiles) {
console.log( `Reading File - ${file}`)
constfilePath = path.join( './books', file)
const{ title, author, paragraphs } = parseBookFile(filePath)
awaitinsertBookData(title, author, paragraphs)
}
} catch(err) {
console.error(err)
}
}
readAndInsertBooks()
运行docker-compose -d --build重建镜像更新应用。

运行docker exec gs-api "node" "server/load_data.js"调用包含load_data脚本应用,应该看到Elasticsearch输出如下。随后,脚本会因为错误退出,原因是调用了一本目前还不存在的helper函数(parseBookFile)。access官方下载 免费完整版

4.4 读取数据文件
创建server/load_data.js文件,读取每本书元数据和内容:
/** Read an individual book text file, and extract the title, author, and paragraphs */

functionparseBookFile(filePath) {
// Read text file
constbook = fs.readFileSync(filePath, 'utf8')
// Find book title and author
consttitle = book.match( /^Title:s(.+)$/m)[ 1]
constauthorMatch = book.match( /^Author:s(.+)$/m)
constauthor = (!authorMatch || authorMatch[ 1].trim() === '') ? 'Unknown Author': authorMatch[ 1]
console.log( `Reading Book - ${title}By ${author}`)
// Find Guttenberg metadata header and footer
conststartOfBookMatch = book.match( /^*{3}s*START OF (THIS|THE) PROJECT GUTENBERG EBOOK.+*{3}$/m)
conststartOfBookIndex = startOfBookMatch.index + startOfBookMatch[ 0].length
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-88976-7.html
战机一起上岛
与日本就差更远了
垃圾
期盼节目的来到