感谢FlashLight https://github.com/firebase/flashlight的教程,使用Firebase进行全文搜索很容易。
但是,如果您保留免费的ES实例,那么它在并发访问方面受到限制,并且当您启动节点应用程序时,您会在日志中看到以下消息:
无法索引firebase / xxx / -KHLhdwGplb3lHWjm8RS:错误:超出了并发请求限制。请考虑分批处理您的请求,或联系[email protected]寻求帮助。
如何解决呢?
如果您要索引一堆数据,则Flashlight应用程序将要求ES动态索引每个对象,而没有任何资源访问限制。您必须使用信号量来控制/限制对该共享资源的访问。
安装信号量库
npm i --save semaphore
编辑PathMonitor.js文件,并将对ES资源的访问限制为1
PathMonitor.js
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)); }); }, ... }
对于付费计划,可能不需要这样做。