access官方下载 免费完整版 使用Docker和Elasticsearch搭建全文本搜索引(7)

电脑杂谈  发布时间:2018-02-22 15:22:17  来源:网络整理

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 */

官方网站下载免费_access官方下载 免费完整版_excel官方免费完整版

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

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...