js中的正则表达式 使用Docker和Elasticsearch搭建全文本搜索引(5)

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

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

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

    • 钟莹莹
      钟莹莹

      要什么大国形象

    • 你是主人我是仆
      你是主人我是仆

      马云忽悠了太做淘宝卖家亏得一踏糊涂

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