如何确保在ElasticSearch盆景免费实例中使用Firebase FlashLight为每个对象build立索引

感谢FlashLight https://github.com/firebase/flashlight教程,这对于使用Firebase进行全文search非常简单。

但是,如果保留免费的ES实例,并发访问将受到限制,当您启动节点应用程序时,会在日志中看到以下消息:

无法索引firebase / xxx / -KHLhdwGplb3lHWjm8RS:错误:超出并发请求限制。 请考虑批量处理您的请求,或联系support@bonsai.io寻求帮助。

如何解决这个问题?

如果你有一堆数据需要索引,那么Flashlight应用会要求ES在运行中索引每个对象,而没有任何资源访问限制。 你必须通过信号量控制/限制访问这个共享资源。

安装信号量库

npm i --save semaphore 

编辑PathMonitor.js文件,并限制对ES资源的访问权限为1

 PathMonitor.prototype = { _init: function () { this.sem = require('semaphore')(1); this.addMonitor = this.ref.on('child_added', this._process.bind(this, this._childAdded)); this.changeMonitor = this.ref.on('child_changed', this._process.bind(this, this._childChanged)); this.removeMonitor = this.ref.on('child_removed', this._process.bind(this, this._childRemoved)); }, ... _index: function (key, data, callback) { var that = this; that.sem.take(function () { that.esc.index({ index: that.index, type : that.type, id : key, body : data }, function (error, response) { that.sem.leave(); if (callback) { callback(error, response); } }.bind(that)); }); }, ... } 

付费scheme可能不需要。