constport = 9200
consthost = process.env.ES_HOST || 'localhost'
constclient = newelasticsearch.Client({ host: { host, port } })
/** Check the ES connection status */
asyncfunctioncheckConnection() {
letisConnected = false
while(!isConnected) {
console.log( 'Connecting to ES')
try{
consthealth = awaitclient.cluster.health({})
console.log(health)
isConnected = true
} catch(err) {
console.log( 'Connection Failed, Retrying...', err)
}
}
}
checkConnection()
下面用docker-compose来重建更改过的应用。之后运行docker-compose up -d重新启动后台进程。
应用启动后,命令行运行docker exec gs-api "node" "server/connection.js",在容器中运行脚本,应该可以看到如下输出:
{ cluster_name: 'docker-cluster',
status: 'yellow',
timed_out: false,
number_of_nodes: 1,
number_of_data_nodes: 1,
active_primary_shards: 1,
active_shards: 1,
relocating_shards: 0,
initializing_shards: 0,
unassigned_shards: 1,
delayed_unassigned_shards: 0,
number_of_pending_tasks: 0,
number_of_in_flight_fetch: 0,
task_max_waiting_in_queue_millis: 0,
active_shards_percent_as_number: 50}
如果一切顺利,就可以把最后一行的checkConnection()调用删掉,因为最终应用会从connection模块之外调用它。
3.1 给Reset Index添加Helper功能
在server/connection.js文件checkConnection之下添加如下内容, 以便更加方便重置索引。
/** Clear the index, recreate it, and add mappings */
asyncfunctionresetIndex() {
if( awaitclient.indices.exists({ index })) {
awaitclient.indices.delete({ index })
}
awaitclient.indices.create({ index })
awaitputBookMapping()
}
3.2 添加Book Schema
紧接resetIndex之后,添加如下功能:
/** Add book section schema mapping to ES */
async functionputBookMapping(){
const schema = {
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-88488-5.html
马云忽悠了太做淘宝卖家亏得一踏糊涂
要什么大国形象